0
0
Cprogramming~5 mins

If–else statement in C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an if–else statement in C?
An if–else statement lets the program choose between two paths: one if a condition is true, and another if it is false.
Click to reveal answer
beginner
Write the basic syntax of an if–else statement in C.
if (condition) { // code if condition is true } else { // code if condition is false }
Click to reveal answer
beginner
What happens if the condition in an if statement is false and there is no else block?
The program skips the if block and continues with the next code after it, doing nothing inside the if block.
Click to reveal answer
intermediate
Can you have multiple else if blocks in C? What is their purpose?
Yes, multiple else if blocks let you check several conditions one after another, running the code for the first true condition.
Click to reveal answer
beginner
Explain with an example how if–else helps in decision making.
Example: if (score >= 50) { printf("Pass\n"); } else { printf("Fail\n"); } This checks if score is 50 or more to print Pass, otherwise prints Fail.
Click to reveal answer
What will the following code print if x = 10? if (x > 5) { printf("Yes"); } else { printf("No"); }
AError
BNo
CNothing
DYes
Which keyword is used to check an additional condition after an if in C?
Aelse if
Belseif
Celif
Delse
What happens if the condition in an if statement is false and there is no else block?
AProgram crashes
BThe if block is skipped
CThe else block runs
DThe program asks for input
Which of these is a valid if–else syntax in C?
Aif condition then { } else { }
Bif condition { } else { }
Cif (condition) { } else { }
Dif (condition) then { } else { }
Can an if statement exist without an else block?
AYes, else is optional
BNo, else is mandatory
COnly if the condition is true
DOnly if the condition is false
Explain how an if–else statement controls the flow of a program.
Think about how the program chooses what to do next based on a question.
You got /4 concepts.
    Write a simple if–else statement in C that prints "Even" if a number is even, and "Odd" if it is odd.
    Use number % 2 to check if remainder is zero.
    You got /4 concepts.