Conditional Branching and Loops in C
Introduction
In C programming, decision-making and looping structures are essential for controlling program flow. These are classified into control statements, which are divided into three categories:
Selection Statements (Decision-Making)
Iterative Statements (Loops)
Jumping Statements (Unconditional Control Transfer)
1. Selection Statements (Decision-Making)
Selection statements allow the program to choose between different paths based on conditions.
1.1 if Statement
- Used to execute a block of code only if a specified condition is true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
#include <stdio.h>
int main() {
int num = 10;
if (num > 5) {
printf("Number is greater than 5\n");
}
return 0;
}
1.2 if-else Statement
- Executes one block of code if the condition is true and another block if it is false.
Syntax:
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
Example:
#include <stdio.h>
int main() {
int num = 10;
if (num % 2 == 0) {
printf("Even number\n");
} else {
printf("Odd number\n");
}
return 0;
}
1.3 Nested if-else
if-elsestatements inside anotherif-else.
Syntax:
if (condition1) {
if (condition2) {
// Code if both conditions are true
} else {
// Code if only condition1 is true
}
} else {
// Code if condition1 is false
}
Example:
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
if (num % 2 == 0) {
printf("Positive Even number\n");
} else {
printf("Positive Odd number\n");
}
} else {
printf("Number is negative\n");
}
return 0;
}
1.4 else-if Ladder
- Used when multiple conditions need to be checked sequentially.
Syntax:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if none of the conditions are true
}
Example:
#include <stdio.h>
int main() {
int marks = 85;
if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 75) {
printf("Grade B\n");
} else {
printf("Grade C\n");
}
return 0;
}
1.5 switch Statement
- Used to replace multiple
if-elsestatements when checking for equality.
Syntax:
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no case matches
}
Example:
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
default: printf("Invalid day\n");
}
return 0;
}
2. Iterative Statements (Loops)
Loops allow executing a block of code multiple times.
2.1 while Loop
- Repeats execution as long as the condition remains true.
Syntax:
while (condition) {
// Code to execute
}
Example:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
2.2 for Loop
- Used when the number of iterations is known.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute
}
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
2.3 do-while Loop
- Executes at least once before checking the condition.
Syntax:
do {
// Code to execute
} while (condition);
Example:
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
3. Jumping Statements
Jumping statements are used to control the flow of execution.
3.1 break Statement
- Used to exit loops or
switchstatements.
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
printf("%d\n", i);
}
return 0;
}
3.2 continue Statement
- Skips the current iteration and continues with the next.
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d\n", i);
}
return 0;
}
3.3 goto Statement
- Transfers control to a labeled statement.
Example:
#include <stdio.h>
int main() {
int i = 1;
loop:
printf("%d\n", i);
i++;
if (i <= 5) goto loop;
return 0;
}
Conclusion
Selection statements (
if-else,switch) help in decision-making.Loops (
while,for,do-while) allow executing code multiple times.Jumping statements (
break,continue,goto) control execution flow.
These control structures are fundamental for writing efficient C programs.
Some practice questions
1. Selection Statements (Decision-Making)
Q1: What is the purpose of the if statement in C?
A: The if statement is used to execute a block of code only if a specified condition evaluates to true.
Q2: What is the difference between if and if-else statements?
A: The if statement executes a block of code only when the condition is true, whereas if-else provides an alternative block to execute when the condition is false.
Q3: What is an else-if ladder in C?
A: An else-if ladder is used to check multiple conditions sequentially. It consists of multiple if-else if-else statements.
Q4: What is the difference between if-else and switch statements?
A:
| Feature | if-else | switch |
| Used for | Complex conditions (logical, relational) | Checking multiple constant values |
| Efficiency | Slower for multiple conditions | Faster when checking many constant cases |
| Data Types | Works with all data types | Works only with integer and character types |
Q5: What is the default case in a switch statement?
A: The default case in a switch statement executes when none of the specified case values match the given expression.
Q6: Why is the break statement used inside a switch case?
A: The break statement prevents fall-through and ensures that only one case executes in a switch statement.
2. Iterative Statements (Loops)
Q7: What is a loop in C?
A: A loop is used to repeatedly execute a block of code until a specified condition is met.
Q8: What are the three types of loops in C?
A:
whileloopforloopdo-whileloop
Q9: What is the difference between while and do-while loops?
A:
| Feature | while Loop | do-while Loop |
| Execution | Checks condition first, then executes | Executes first, then checks condition |
| Minimum Execution | May not execute if the condition is false initially | Executes at least once |
Q10: What is an infinite loop? Give an example.
A: An infinite loop runs indefinitely because its condition never becomes false.
Example:
while (1) {
printf("Infinite Loop\n");
}
Q11: What is the syntax of a for loop in C?
A:
for (initialization; condition; increment/decrement) {
// Loop body
}
Q12: How is the while loop different from the for loop?
A:
| Feature | while Loop | for Loop |
| Best Used For | When number of iterations is unknown | When number of iterations is known |
| Structure | Condition is separate from initialization and increment | All parts (initialization, condition, update) are in one line |
3. Jumping Statements
Q13: What is the purpose of the break statement?
A: The break statement is used to terminate a loop or a switch case immediately.
Q14: What is the purpose of the continue statement?
A: The continue statement skips the current iteration of the loop and moves to the next iteration.
Q15: What is the goto statement? Why is it discouraged?
A: The goto statement is used to transfer control to a labeled statement elsewhere in the program. It is discouraged because it makes the code harder to read and maintain.
Example:
goto label;
label: printf("Jumped to this line\n");
4. Conceptual Questions
Q16: What happens if there is no break statement in a switch case?
A: If break is missing, execution will continue to the next case (fall-through behavior).
Q17: What is an entry-controlled and exit-controlled loop?
A:
Entry-controlled loop: Condition is checked before execution (
for,while).Exit-controlled loop: Condition is checked after execution (
do-while).
Q18: Can we use a switch statement with a float variable?
A: No, switch works only with integer or character types.
Q19: Can a for loop work without an initialization or condition?
A: Yes, all parts are optional. Example of an infinite loop using for:
for (;;) {
printf("Infinite Loop\n");
}
Q20: What is nesting of loops? Give an example.
A: Nesting means placing one loop inside another.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf("i=%d, j=%d\n", i, j);
}
}
Multiple Choice Questions (MCQs) on Selection Statements in C
Basic MCQs
What is the purpose of selection statements in C?
a) To execute statements sequentially
b) To make decisions based on conditions
c) To repeat statements multiple times
d) To define functions✅ Answer: (b) To make decisions based on conditions
What will be the output of the following code?
#include <stdio.h> int main() { int x = 5; if (x = 0) { printf("Hello"); } else { printf("World"); } return 0; }a) Hello
b) World
c) Compilation error
d) No output✅ Answer: (a) Hello (because
x = 0assigns 0 tox, which is false, so theifblock is skipped, and "World" is printed.)Which of the following is NOT a selection statement in C?
a)if
b)switch
c)for
d)if-else✅ Answer: (c) for (it is a loop, not a selection statement)
Intermediate MCQs
How many cases can a
switchstatement have in C?
a) Only 2
b) Between 2 and 5
c) Any number
d) Maximum of 10✅ Answer: (c) Any number
What will be the output of the following code?
#include <stdio.h> int main() { int num = 3; if (num > 5) if (num < 10) printf("A"); else printf("B"); printf("C"); return 0; }a) A
b) B
c) C
d) A C✅ Answer: (c) C (because
num > 5is false, so the innerifis skipped, and only "C" is printed.)What is the default return type of the
main()function in C?
a)int
b)void
c)char
d)float✅ Answer: (a) int
Advanced MCQs (Tricky Questions)
What happens if we forget to write
breakin aswitchcase?
a) Only the matched case executes
b) The program crashes
c) The next cases also execute until abreakis found
d) Compilation error✅ Answer: (c) The next cases also execute until a
breakis found (This is called fall-through behavior in C.)What will be the output of the following code?
#include <stdio.h> int main() { int x = 2; switch (x) { case 1: printf("One"); case 2: printf("Two"); case 3: printf("Three"); default: printf("Default"); } return 0; }a) Two
b) TwoThreeDefault
c) Compilation error
d) Default✅ Answer: (b) TwoThreeDefault (No
breakstatements cause fall-through.)Which of the following is true about the
switchstatement in C?
a)caselabels can have duplicate values
b)defaultis mandatory
c)breakis optional but prevents fall-through
d)switchcan only work with integers✅ Answer: (c)
breakis optional but prevents fall-throughConsider the following code:
#include <stdio.h> int main() { int a = 10, b = 20; if (a == 10) if (b == 20) printf("X"); else printf("Y"); return 0; }What will be the output?
a) X
b) Y
c) Compilation error
d) No output✅ Answer: (a) X (Both conditions are true.)
Bonus: Conceptual MCQs
Can we use
floatvalues in aswitchcase statement in C?
a) Yes
b) No✅ Answer: (b) No (
switchonly works withint,char, andenumtypes.)What is the correct way to compare two
charvalues in anifcondition?
a)if (char1 == char2)
b)if (char1 = char2)
c)if (char1.compare(char2))
d)if (strcmp(char1, char2))✅ Answer: (a)
if (char1 == char2)