Bird
Raised Fist0
C Sharp (C#)programming~5 mins

If statement execution flow in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of an if statement in C#?
An if statement checks a condition and runs a block of code only if that condition is true.
Click to reveal answer
beginner
What happens when the condition in an if statement is false?
The code inside the if block is skipped, and the program continues with the next statement after the if block.
Click to reveal answer
beginner
Explain the flow of an if-else statement.
If the if condition is true, its block runs. If false, the else block runs instead.
Click to reveal answer
intermediate
What is the role of else if in C#?
else if lets you check multiple conditions one after another. The first true condition runs its block, and the rest are skipped.
Click to reveal answer
intermediate
How does the program decide which block to execute in an if-else if-else chain?
It checks each condition from top to bottom. The first condition that is true runs its block, and the rest are ignored. If none are true, the else block runs.
Click to reveal answer
What happens if the if condition is false and there is no else block?
AThe program skips the <code>if</code> block and continues after it.
BThe program runs an error.
CThe program stops running.
DThe program runs the <code>if</code> block anyway.
In an if-else if-else chain, how many blocks can run?
AAll blocks run.
BOnly the <code>else</code> block runs.
COnly the first true condition's block runs.
DNone run.
Which keyword allows checking multiple conditions in sequence?
Aelse
Belse if
Cswitch
Dfor
What is the correct syntax to start an if statement in C#?
Aif (condition) { }
Bif condition { }
Cif: condition { }
Dif [condition] { }
What does the program do after finishing an if block?
AIt repeats the <code>if</code> block.
BIt stops running.
CIt jumps to the start of the program.
DIt continues with the next statement after the <code>if</code> block.
Describe how an if-else if-else statement controls the flow of a program.
Think about how the program chooses which code to run based on conditions.
You got /4 concepts.
    Explain what happens when an if condition is false and there is no else block.
    Consider what the program does when the condition is not met and no alternative is given.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does an if statement do in C#?
      easy
      A. It defines a new variable.
      B. It repeats code multiple times.
      C. It chooses which code to run based on a condition.
      D. It stops the program immediately.

      Solution

      1. Step 1: Understand the purpose of if

        An if statement checks a condition to decide which code runs next.
      2. Step 2: Compare options

        Only It chooses which code to run based on a condition. describes choosing code based on a condition, which matches the if statement's role.
      3. Final Answer:

        It chooses which code to run based on a condition. -> Option C
      4. Quick Check:

        if chooses code path = C [OK]
      Hint: If checks true/false to pick code path quickly [OK]
      Common Mistakes:
      • Confusing if with loops
      • Thinking if defines variables
      • Believing if stops program
      2. Which of these is the correct syntax for an if statement in C#?
      easy
      A. if (x > 5) Console.WriteLine("Yes") end
      B. if x > 5 then Console.WriteLine("Yes")
      C. if x > 5: Console.WriteLine("Yes")
      D. if (x > 5) { Console.WriteLine("Yes"); }

      Solution

      1. Step 1: Review C# if syntax

        C# requires parentheses around the condition and braces for the block.
      2. Step 2: Check each option

        if (x > 5) { Console.WriteLine("Yes"); } uses parentheses and braces correctly; others use wrong keywords or punctuation.
      3. Final Answer:

        if (x > 5) { Console.WriteLine("Yes"); } -> Option D
      4. Quick Check:

        Correct C# if syntax = D [OK]
      Hint: Remember: if(condition) { code } in C# [OK]
      Common Mistakes:
      • Omitting parentheses around condition
      • Using Python or other language syntax
      • Missing braces for code block
      3. What will this code print?
      int x = 10;
      if (x < 5) {
        Console.WriteLine("Less");
      } else {
        Console.WriteLine("More or equal");
      }
      medium
      A. Less
      B. More or equal
      C. Nothing
      D. Error

      Solution

      1. Step 1: Evaluate the condition

        The condition x < 5 is false because x is 10.
      2. Step 2: Determine which block runs

        Since the condition is false, the else block runs, printing "More or equal".
      3. Final Answer:

        More or equal -> Option B
      4. Quick Check:

        Condition false -> else runs = A [OK]
      Hint: If false, else block runs [OK]
      Common Mistakes:
      • Thinking if block runs when false
      • Ignoring else block
      • Confusing < with >
      4. Find the error in this code:
      int num = 7;
      if num > 5 {
        Console.WriteLine("Big");
      } else {
        Console.WriteLine("Small");
      }
      medium
      A. Missing parentheses around the condition
      B. Missing semicolon after if
      C. Wrong keyword else
      D. Variable num not declared

      Solution

      1. Step 1: Check if statement syntax

        C# requires parentheses around the condition in an if statement.
      2. Step 2: Identify the error

        The code has if num > 5 { without parentheses, causing a syntax error.
      3. Final Answer:

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

        Parentheses needed in if condition = A [OK]
      Hint: Always put condition inside parentheses in if [OK]
      Common Mistakes:
      • Forgetting parentheses in if
      • Adding semicolon after if line
      • Misusing else keyword
      5. You want to print "Positive", "Negative", or "Zero" based on an integer n. Which code correctly uses if statements to do this?
      hard
      A. if (n > 0) { Console.WriteLine("Positive"); } else if (n < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); }
      B. if (n > 0) Console.WriteLine("Positive"); if (n < 0) Console.WriteLine("Negative"); else Console.WriteLine("Zero");
      C. if (n > 0) { Console.WriteLine("Positive"); } else { if (n > 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } }
      D. if (n > 0) { Console.WriteLine("Positive"); } else if (n <= 0) { Console.WriteLine("Zero"); } else { Console.WriteLine("Negative"); }

      Solution

      1. Step 1: Understand the logic needed

        We need to check if n is greater than 0, less than 0, or exactly 0.
      2. Step 2: Analyze each option

        if (n > 0) { Console.WriteLine("Positive"); } else if (n < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } correctly uses if, else if, and else to cover all cases without overlap or missing conditions.
      3. Final Answer:

        if (n > 0) { Console.WriteLine("Positive"); } else if (n < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } -> Option A
      4. Quick Check:

        Proper if-else chain covers all cases = B [OK]
      Hint: Use if-else if-else chain for multiple conditions [OK]
      Common Mistakes:
      • Using separate ifs causing multiple prints
      • Wrong order of conditions
      • Incorrect else condition 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