Chapter 6: Control Structures
Control structures in C are used to control the flow
of program execution based on certain conditions or loops. They include
decision-making structures (if-else, switch), looping structures (for, while,
do-while), and branching structures (break, continue, goto). Let's explore each
of these control structures:
6.1
Decision-Making Structures:
6.1.1
If-Else Statement:
The if-else statement allows the program to make
decisions based on a condition. It executes a block of code if the condition is
true, and an optional else block if the condition is false.
if
(condition) {
// Code to be executed if condition is true
}
else {
// Code to be executed if condition is
false
}
Example:
#include
<stdio.h>
int
main() {
int age = 18;
if (age >= 18) {
printf("You are eligible to
vote.\n");
} else {
printf("You are not eligible to
vote.\n");
}
return 0;
}
The program checks the value of the `age` variable
and prints a message based on the condition. Compile and run the program to see
the output.
6.1.2
Switch Statement:
The switch statement allows multiple conditional
branches based on the value of an expression. It provides an efficient way to
select one of many code blocks to be executed.
switch
(expression) {
case constant1:
// Code to be executed if expression
matches constant1
break;
case constant2:
// Code to be executed if expression
matches constant2
break;
// ...
default:
// Code to be executed if expression
doesn't match any constant
break;
}
Example:
#include
<stdio.h>
int
main() {
int choice = 2;
switch (choice) {
case 1:
printf("You selected option
1.\n");
break;
case 2:
printf("You selected option
2.\n");
break;
default:
printf("Invalid
choice.\n");
break;
}
return 0;
}
The program checks the value of the `choice`
variable and executes the corresponding code block. Compile and run the program
to see the output.
6.2
Looping Structures:
6.2.1
For Loop:
The for loop is used to execute a block of code
repeatedly for a specific number of times.
for
(initialization; condition; update) {
// Code to be executed
}
Example:
#include
<stdio.h>
int
main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
The program uses a for loop to print the numbers
from 1 to 5. Compile and run the program to see the output.
6.2.2
While Loop:
The while loop is used to execute a block of code
repeatedly as long as a condition is true.
while
(condition) {
// Code to be executed
}
Example:
#include
<stdio.h>
int
main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
The program uses a while loop to print the numbers
from 1 to 5. Compile and run the program to see the output.
6.2.3
Do-While Loop:
The do-while loop is similar to the while loop but
guarantees that the code is executed at least once before checking the
condition.
do
{
// Code to be executed
}
while (condition);
Example:
#include
<stdio.h>
int
main() {
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
printf("\n");
return 0;
}
The program uses a do-while loop to print the
numbers from 1 to 5. Compile and run the program to see the output.
6.3
Branching Structures:
6.3.1
Break Statement:
The break statement is used to exit the current loop
or switch statement.
Example:
#include
<stdio.h>
int
main() {
int i;
for (i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
printf("%d ", i);
}
printf("\n");
return 0;
}
The program uses a for loop and breaks out of the
loop when `i` becomes 3. Compile and run the program to see the output.
6.3.2
Continue Statement:
The continue statement is used to skip the current
iteration of a loop and continue with the next iteration.
Example:
#include
<stdio.h>
int
main() {
int i;
for (i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
printf("\n");
return 0;
}
The program uses a for loop and skips printing the
number 3 using the continue statement. Compile and run the program to see the
output.
6.3.3
Goto Statement:
The goto statement allows for an unconditional
transfer of control to a labeled statement in the program. However, it is
generally considered bad practice and should be used sparingly, if at all.
Example:
#include
<stdio.h>
int
main() {
int i;
for (i = 1; i <= 5; i++) {
if (i == 3) {
goto label;
}
printf("%d ", i);
}
label:
printf("Goto statement
executed.\n");
return 0;
}
The program uses a for loop and jumps to the `label`
using the goto statement when `i` becomes 3. Compile and run the program to see
the output.
That concludes our exploration of control structures
in C. In the next chapter, we will cover functions, which allow for modular and
reusable code.
0 Comments