Introduction
In the previous chapter, we learned about C operators. In this chapter, we will introduce control flow statements in C. Control flow statements are used to determine the order in which statements are executed in a program. They allow you to make decisions, repeat tasks, and choose between different paths of execution.
What are Control Flow Statements?
Control flow statements enable you to control the direction and flow of execution of your program based on conditions, loops, and jumps. These statements are essential for creating complex and dynamic programs that can handle various scenarios and conditions.
Types of Control Flow Statements
Control flow statements in C can be broadly categorized into the following types:
- Conditional Statements
- Looping Statements
- Jump Statements
1. Conditional Statements
Conditional statements are used to perform different actions based on different conditions. The main conditional statements in C are:
- if Statement: Executes a block of code if a specified condition is true.
Example:
#include <stdio.h>
int main() {
int age = 18;
if (age >= 18) {
printf("You are eligible to vote.\n");
}
return 0; // Returning 0 to indicate successful execution
}
- if-else Statement: Executes one block of code if a condition is true and another block of code if the condition is false.
Example:
#include <stdio.h>
int main() {
int age = 16;
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
return 0; // Returning 0 to indicate successful execution
}
- else-if Ladder: Allows you to check multiple conditions by chaining
if
andelse if
statements.
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 if (marks >= 60) {
printf("Grade: C\n");
} else {
printf("Grade: D\n");
}
return 0; // Returning 0 to indicate successful execution
}
- switch Statement: Allows you to execute one of many blocks of code based on the value of a variable or expression.
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;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
break;
}
return 0; // Returning 0 to indicate successful execution
}
2. Looping Statements
Looping statements are used to repeat a block of code as long as a specified condition is true. The main looping statements in C are:
- for Loop: Repeats a block of code a specified number of times.
Example:
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("Iteration %d\n", i);
}
return 0; // Returning 0 to indicate successful execution
}
- while Loop: Repeats a block of code as long as a specified condition is true.
Example:
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("Iteration %d\n", i);
i++;
}
return 0; // Returning 0 to indicate successful execution
}
- do-while Loop: Similar to the
while
loop, but it executes the block of code at least once before checking the condition.
Example:
#include <stdio.h>
int main() {
int i = 0;
do {
printf("Iteration %d\n", i);
i++;
} while (i < 5);
return 0; // Returning 0 to indicate successful execution
}
3. Jump Statements
Jump statements are used to alter the normal flow of execution by jumping to a specific part of the code. The main jump statements in C are:
- break Statement: Terminates the nearest enclosing loop or
switch
statement.
Example:
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exiting the loop when i equals 5
}
printf("Iteration %d\n", i);
}
return 0; // Returning 0 to indicate successful execution
}
- continue Statement: Skips the current iteration of the nearest enclosing loop and proceeds to the next iteration.
Example:
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skipping even numbers
}
printf("Odd number: %d\n", i);
}
return 0; // Returning 0 to indicate successful execution
}
- goto Statement: Transfers control to a labeled statement within the same function.
Example:
#include <stdio.h>
int main() {
int i = 0;
start:
printf("Iteration %d\n", i);
i++;
if (i < 5) {
goto start; // Jumping back to the label 'start'
}
return 0; // Returning 0 to indicate successful execution
}
Conclusion
Control flow statements are fundamental building blocks in C programming. They enable you to control the execution flow of your programs, making them more dynamic and capable of handling various scenarios. By mastering conditional statements, looping statements, and jump statements, you can write more complex and efficient C programs. You will learn more about these control flow statements in upcoming chapters.