Chapter # 07
Conditional statement in C
Short Q/A
1. What is Control Structure used in C?
Answer:
Control structures in C are statements that control the flow of program
execution. They decide which part of the program runs first, next, or
repeatedly.
C has three main control structures:
1. Sequence (Sequential Flow)
2. Selection (Decision Making)
3. Iteration (Looping)
Example:
#include <stdio.h>
int main() {
int a = 10, b = 20;
if(a < b) {
printf("b is greater");
}
return 0;
}
2. Explain the Control Structure Sequence or Sequential Flow.
Answer:
The sequence control structure means that the program executes
statements one after another in the same order they are written —
from top to bottom.
No condition or loop is used; it’s the simplest type of control flow.
Example Program:
#include <stdio.h>
int main() {
int a = 5, b = 10, sum;
sum = a + b; // Step 1
printf("Sum = %d\n", sum); // Step 2
printf("Program executed in sequence."); // Step 3
return 0;
}
Output:
Sum = 15
Program executed in sequence.
3. Explain the Control Structure Selection or Conditional Flow.
Answer:
The selection control structure allows the program to make
decisions.
It executes different blocks of code depending on whether a condition
is true or false.
Common selection statements in C are:
if
if-else
nested if
switch
Example Program:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num > 0)
printf("Number is Positive");
else if(num < 0)
printf("Number is Negative");
else
printf("Number is Zero");
return 0;
}
Output (if input = -5):
Number is Negative
4. Explain the Control Structure Iteration or Repetitive Flow.
Answer:
The iteration control structure (also called looping) allows the program
to repeat a set of instructions multiple times until a certain condition
becomes false.
Loops help reduce code repetition.
Common loops in C are:
for
while
do-while
Example Program (using for loop):
#include <stdio.h>
int main() {
int i;
for(i = 1; i <= 5; i++) {
printf("Hello World %d\n", i);
}
return 0;
}
Output:
Hello World 1
Hello World 2
Hello World 3
Hello World 4
Hello World 5
Long Q./A
Q1. Explain if statement with syntax, example, and flowchart
Definition:
The if statement in C is a decision-making control structure that
allows the program to execute a certain block of code only if a specific
condition is true.
If the condition is false, the program skips that block and continues with
the next statement.
Syntax:
if (condition) {
// statements to be executed if condition is true
}
Explanation:
The condition is tested inside parentheses ().
If the condition evaluates to true (non-zero), the code inside the {
} block runs.
If it evaluates to false (zero), the code is skipped.
Example Program:
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 50) {
printf("You passed the exam.\n");
}
printf("Program ended.\n");
return 0;
}
Output Example:
Enter your marks: 70
You passed the exam.
Program ended.
Q2. Define and explain switch statement. Explain with example program.
Definition:
The switch statement in C is a multi-way selection statement.
It allows the program to choose one block of code to execute from
multiple options, based on the value of an expression (usually an
integer or character).
Syntax:
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
...
default:
// statements if no case matches
}
Explanation:
The expression is evaluated once.
The result is compared with each case label.
When a match is found, the corresponding statements are
executed.
The break statement ends the switch block.
Without break, the execution “falls through” to the next case.
The default case is optional and runs when no case matches.
Example Program:
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1–3): ");
scanf("%d", &day);
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day number\n");
}
return 0;
}
Output Example:
Enter a number (1–3): 2
Tuesday
Q3. Why is nested if statement used? Explain with example program.
Definition:
A nested if statement means using one if statement inside another.
It is used when multiple conditions must be tested one after another —
that is, the inner condition is checked only if the outer condition is true.
Syntax:
if (condition1) {
if (condition2) {
// statements if both conditions are true
}
}
Explanation:
When complex decisions are needed (for example, checking
multiple ranges), nested ifs help make the logic more precise.
It allows checking hierarchical or dependent conditions.
Example Program:
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 50) {
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 70) {
printf("Grade: B\n");
} else {
printf("Grade: C\n");
}
} else {
printf("You failed.\n");
}
return 0;
}
Output Example:
Enter your marks: 85
Grade: B
Q4. Differentiate between switch and if statement
Feature if Statement switch Statement
1. Purpose Used for simple or complex Used for multi-way
condition checking. selection based on one
variable’s value.
2. Expression Can check relational or Works only with integral
Type logical conditions (e.g. x > values (int, char).
10 && y < 20).
3. Syntax More flexible but longer for Easier to read when
Simplicity multiple conditions. there are many fixed
cases.
4. Execution Checks conditions Directly jumps to the
Flow sequentially until one is matching case.
true.
5. Use of No need for break. Requires break to stop
break fall-through.
6. Default No default, but can use Has default case for
Option else. unmatched values.
7. Nested Can be nested easily. Nested switches are
Structure complex and less
readable.
8. Example Range checking (marks > Fixed values (case 1,
Usage 80). case 2).
Important Note:
"You will draw the flowchart for each program yourself in the
long questions and answers."
MCQs
1. Which statement is used in C for decision-making?
a) for
b) if
c) while
d) goto
→ Answer: b) if
2. In an if statement, what happens if the condition is false?
a) The program stops
b) The statement inside if block executes
c) The statement inside if block is skipped
d) The program restarts
→ Answer: c) The statement inside if block is skipped
3. Which of the following is the correct syntax for an if statement?
a) if condition { }
b) if (condition);
c) if (condition) { }
d) if condition;
→ Answer: c) if (condition) { }
4. What keyword is used in C to check multiple constant values of a variable?
a) if
b) else
c) switch
d) for
→ Answer: c) switch
5. What is the purpose of the break statement in a switch block?
a) To continue to the next case
b) To exit the switch statement
c) To stop the program
d) To repeat the case
→ Answer: b) To exit the switch statement
6. Which of the following data types can be used in a switch expression?
a) float
b) double
c) int or char
d) long double
→ Answer: c) int or char
7. When do we use a ested if statement?
a) To repeat statements
b) To test multiple conditions one inside another
c) To call a function
d) To stop execution
→ Answer: b) To test multiple conditions one inside another
8. Which statement is true about the switch statement?
a) It can test relational expressions
b) It can only test constant integer or character values
c) It works like a loop
d) It must always include a default case
→ Answer: b) It can only test constant integer or character values
9. In an if-else statement, if the condition is true, which block is executed?
a) else block
b) if block
c) both blocks
d) none of these
→ Answer: b) if block
10. Which statement provides better readability when checking many fixed values?
a) if
b) nested if
c) switch
d) while
→ Answer: c) switch