Bird
Raised Fist0
Cprogramming~20 mins

Why conditional logic is needed - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Conditional Logic Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this C code with conditional logic?

Look at this C program. What will it print when run?

C
#include <stdio.h>

int main() {
    int temperature = 30;
    if (temperature > 25) {
        printf("It's hot today!\n");
    } else {
        printf("It's cool today!\n");
    }
    return 0;
}
AIt's hot today!
BIt's cool today!
CCompilation error
DNo output
Attempts:
2 left
💡 Hint

Check the value of temperature and the condition in the if statement.

🧠 Conceptual
intermediate
1:30remaining
Why do we need conditional logic in programs?

Which of these best explains why conditional logic is important in programming?

AIt automatically fixes errors in the code.
BIt makes programs run faster by skipping code.
CIt stores data permanently for later use.
DIt allows programs to make decisions and choose different actions based on data.
Attempts:
2 left
💡 Hint

Think about how a program can behave differently depending on what it sees.

🔧 Debug
advanced
2:00remaining
What error does this C code cause?

Look at this code snippet. What error will it cause when compiled?

C
#include <stdio.h>

int main() {
    int x = 10;
    if x > 5 {
        printf("x is greater than 5\n");
    }
    return 0;
}
ARuntime error: division by zero
BSyntax error: missing parentheses around condition
CNo error, prints 'x is greater than 5'
DSyntax error: missing semicolon after if
Attempts:
2 left
💡 Hint

Check the syntax of the if statement condition.

❓ Predict Output
advanced
2:00remaining
What is the output of nested if-else in this C code?

What will this program print?

C
#include <stdio.h>

int main() {
    int score = 75;
    if (score >= 90) {
        printf("Grade A\n");
    } else if (score >= 70) {
        printf("Grade B\n");
    } else {
        printf("Grade C\n");
    }
    return 0;
}
AGrade B
BGrade A
CGrade C
DNo output
Attempts:
2 left
💡 Hint

Check which condition matches the value of score.

🧠 Conceptual
expert
1:30remaining
How does conditional logic improve program flexibility?

Which statement best describes how conditional logic helps programs be flexible?

AIt stores user data securely in memory.
BIt makes programs run faster by removing unnecessary code.
CIt lets programs respond differently to changing inputs or situations.
DIt automatically corrects user mistakes without input.
Attempts:
2 left
💡 Hint

Think about how programs can change what they do based on what happens.

Practice

(1/5)
1. Why do we use conditional logic like if statements in C programs?
easy
A. To repeat code multiple times
B. To make decisions based on different conditions
C. To store multiple values in one variable
D. To define new data types

Solution

  1. Step 1: Understand the purpose of conditional logic

    Conditional logic allows a program to choose different actions depending on conditions.
  2. Step 2: Compare with other options

    Repeating code is done with loops, storing values uses arrays or variables, and defining types uses structs or typedefs.
  3. Final Answer:

    To make decisions based on different conditions -> Option B
  4. Quick Check:

    Conditional logic = decision making [OK]
Hint: If you want choices, use if-else [OK]
Common Mistakes:
  • Confusing loops with conditionals
  • Thinking variables store multiple values
  • Mixing data types with logic
2. Which of the following is the correct syntax for an if statement in C?
easy
A. if (condition) { /* code */ }
B. if condition { /* code */ }
C. if condition then { /* code */ }
D. if [condition] { /* code */ }

Solution

  1. Step 1: Recall C syntax for if statements

    In C, conditions must be inside parentheses and code inside braces.
  2. Step 2: Check each option

    if (condition) { /* code */ } matches the correct syntax: if (condition) { /* code */ }.
  3. Final Answer:

    if (condition) { /* code */ } -> Option A
  4. Quick Check:

    Parentheses around condition = correct syntax [OK]
Hint: Remember: if (condition) { } in C [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Using square brackets instead of parentheses
  • Adding 'then' keyword which C does not use
3. What will be the output of this C code?
int x = 10;
if (x > 5) {
    printf("Big\n");
} else {
    printf("Small\n");
}
medium
A. Compilation error
B. Small
C. No output
D. Big

Solution

  1. Step 1: Evaluate the condition

    The variable x is 10, which is greater than 5, so the condition (x > 5) is true.
  2. Step 2: Determine which branch runs

    Since the condition is true, the code inside the if block runs, printing "Big".
  3. Final Answer:

    Big -> Option D
  4. Quick Check:

    10 > 5 is true, so prints Big [OK]
Hint: Check condition true or false to pick output [OK]
Common Mistakes:
  • Confusing which block runs on true
  • Ignoring the newline character
  • Thinking else block runs when condition is true
4. Find the error in this C code snippet:
int num = 7;
if num > 5 {
    printf("Yes\n");
} else {
    printf("No\n");
}
medium
A. Missing parentheses around the condition
B. Missing semicolon after if statement
C. Incorrect use of else keyword
D. Variable num not declared

Solution

  1. Step 1: Check the if statement syntax

    The condition in an if statement must be inside parentheses in C.
  2. Step 2: Identify the error

    The code uses if num > 5 { ... } without parentheses, which is invalid syntax.
  3. Final Answer:

    Missing parentheses around the condition -> Option A
  4. Quick Check:

    If condition needs ( ) in C [OK]
Hint: Always put condition inside ( ) for if [OK]
Common Mistakes:
  • Forgetting parentheses in if condition
  • Adding semicolon after if line incorrectly
  • Misplacing braces
5. You want to write a program that prints "Even" if a number is divisible by 2, and "Odd" otherwise. Which code snippet correctly uses conditional logic to do this?
hard
A. if (num % 2 != 0) { printf("Even\n"); } else { printf("Odd\n"); }
B. if (num / 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); }
C. if (num % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); }
D. if (num % 2 = 0) { printf("Even\n"); } else { printf("Odd\n"); }

Solution

  1. Step 1: Understand the condition for even numbers

    A number is even if dividing by 2 leaves remainder 0, checked by num % 2 == 0.
  2. Step 2: Check each option's condition

    if (num % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } correctly uses num % 2 == 0. if (num / 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } uses division instead of modulo. if (num % 2 = 0) { printf("Even\n"); } else { printf("Odd\n"); } uses assignment (=) instead of comparison (==). if (num % 2 != 0) { printf("Even\n"); } else { printf("Odd\n"); } reverses the logic.
  3. Final Answer:

    if (num % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } -> Option C
  4. Quick Check:

    Modulo 2 equals 0 means even [OK]
Hint: Use modulo (%) to check divisibility [OK]
Common Mistakes:
  • Using = instead of == in condition
  • Using division instead of modulo
  • Reversing if-else logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard