What is the purpose of the `else` statement in C?
A
To repeat a block of code B
To execute code when `if` condition is false C
To terminate the program D
To define a function
Analysis & Theory
`else` is used to run code when the `if` condition evaluates to false.
Which is the correct syntax of an `if-else` block in C?
A
if(condition) then { ... } else { ... } B
if (condition) { ... } else { ... } C
if {condition} else { ... } D
if condition: do this else: do that
Analysis & Theory
The correct syntax is `if (condition) { ... } else { ... }`.
What will this code output?
```c
int x = 10;
if (x < 5) printf("Low"); else printf("High");
```
A
Low B
High C
Error D
Nothing
Analysis & Theory
`x < 5` is false, so the `else` block executes and prints "High".
What happens if you omit the `else` block in C?
A
It causes an error B
It prints 'else' C
Nothing; the program continues D
The program exits
Analysis & Theory
The `else` block is optional. If omitted, nothing happens when the `if` condition is false.
How do you chain multiple conditions in C?
A
Using `else and if` B
Using `elif` C
Using `elseif` D
Using `else if`
Analysis & Theory
In C, multiple conditions are chained using `else if`.
What is the output?
```c
int a = 3;
if (a > 5) printf("A");
else if (a == 3) printf("B");
else printf("C");
```
A
A B
B C
C D
Nothing
Analysis & Theory
`a > 5` is false, `a == 3` is true, so it prints "B".
Which statement is TRUE about `else if`?
A
It can only appear once in an if-else chain B
It must follow an `else` block C
It allows checking multiple conditions D
It is not allowed in C
Analysis & Theory
`else if` is used to check multiple conditions after the `if` fails.
What is the output?
```c
int x = 0;
if (x) printf("True"); else printf("False");
```
A
True B
False C
0 D
Nothing
Analysis & Theory
`x` is 0 (false), so the `else` block executes and prints "False".
Can an `else` block be used without an `if`?
A
Yes B
Only with a loop C
No D
Only inside a function
Analysis & Theory
`else` must follow an `if` block; otherwise it causes a syntax error.
What is the output?
```c
int a = 5;
if (a < 0) printf("Negative");
else if (a == 0) printf("Zero");
else printf("Positive");
```
A
Negative B
Zero C
Positive D
Nothing
Analysis & Theory
`a == 0` is false, but `a < 0` is also false, so it prints "Positive".