Control Structures in C: An In-Depth Guide
Control Structures in C
Control structures are an essential part of any programming language, and C is no exception. Control structures allow you to control the flow of execution of a program, making it possible to perform complex operations with ease.
In this tutorial, we will explore the different control structures in C, including the if...else statement, the switch statement, and the various loops (for, while, and do...while).
Table of Contents
- The if...else Statement
- The switch Statement
- The for Loop
- The while Loop
- The do...while Loop
- Loop Control Statements
- break
- continue
1. The if...else Statement
The if...else statement is used to execute code based on a condition. The basic syntax of an if...else statement is as follows:
sqlif (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
The condition can be any expression that evaluates to a boolean value (true or false). If the condition is true, the code inside the if block is executed. If the condition is false, the code inside the else block is executed.
Here is an example:
arduino#include <stdio.h>
int main() {
int x = 10;
if (x > 0) {
printf("x is positive\n");
} else {
printf("x is negative or zero\n");
}
return 0;
}
In this example, we use an if...else statement to check if the value of x is positive or negative. If x is greater than zero, the program will print "x is positive." Otherwise, it will print "x is negative or zero."
2. The switch Statement
The switch statement is used to execute different blocks of code based on the value of a variable. The basic syntax of a switch statement is as follows:
arduinoswitch (expression) {
case value1:
// Code to execute if expression is equal to value1
break;
case value2:
// Code to execute if expression is equal to value2
break;
default:
// Code to execute if expression does not match any of the cases
break;
}
The expression can be any variable or expression that evaluates to an integral value. The cases represent the different possible values of the expression. If the expression matches one of the cases, the code inside that case block is executed. If there is no match, the code inside the default block is executed.
Here is an example:
arduino#include <stdio.h>
int main() {
int x = 2;
switch (x) {
case 1:
printf("x is 1\n");
break;
case 2:
printf("x is 2\n");
break;
case 3:
printf("x is 3\n");
break;
default:
printf("x is not 1, 2, or 3\n");
break;
}
return 0;
}
In this example, we use a switch statement to check the value of x. If x is equal to 1, the program will print "x is 1." If x is equal to 2, the program will print "x is 2." If x is equal to 3, the program will print "x is 3." If x is not equal to any of these values, the program will print "x is not 1, 2, or 3."
3. The for Loop
The for loop is used to execute a block of code a fixed number of times. The basic syntax of a for loop is as follows:
cssfor (initialization; condition; increment/decrement) {
// Code to execute
}
The initialization is used to initialize a counter variable. The condition is used to check whether the loop should continue executing or not. Finally, the update statement is used to modify the counter variable before the next iteration of the loop starts.
Here's an example of using the for loop to print the first 10 positive integers:
c#include <stdio.h>
int main() {
int i;
for(i=1; i<=10; i++) {
printf("%d\n", i);
}
return 0;
}
In this example, we initialize the variable i
to 1, and then we check if it's less than or equal to 10. If it is, we print the value of i
using the printf()
function and then increment i
by 1. We continue this process until i
is no longer less than or equal to 10.
4. The while Loop
The while
loop is another type of loop in C that allows you to execute a block of code repeatedly while a condition is true. The syntax of the while loop is as follows:
cwhile(condition) {
// code to be executed
}
In this syntax, condition
is the expression that is tested before each iteration of the loop. If the condition is true, the code inside the curly braces is executed. After each execution of the code, the condition is tested again, and the loop continues until the condition becomes false.
Here's an example of using the while loop to print the first 10 positive integers:
c#include <stdio.h>
int main() {
int i = 1;
while(i <= 10) {
printf("%d\n", i);
i++;
}
return 0;
}
In this example, we initialize the variable i
to 1, and then we use a while loop to print the value of i
and increment i
by 1 until i
is no longer less than or equal to 10.
5. The do...while Loop
The do...while
loop is similar to the while
loop, but it guarantees that the code inside the loop is executed at least once, even if the condition is false. The syntax of the do...while loop is as follows:
cdo {
// code to be executed
} while(condition);
In this syntax, the code inside the curly braces is executed first, and then the condition is tested. If the condition is true, the code is executed again, and the loop continues. If the condition is false, the loop exits.
Here's an example of using the do...while loop to print the first 10 positive integers:
c#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while(i <= 10);
return 0;
}
In this example, we initialize the variable i
to 1, and then we use a do...while loop to print the value of i
and increment i
by 1 until i
is no longer less than or equal to 10.
6. Loop Control Statements (break and continue)
C provides two loop control statements, break
and continue
, which allow you to control the flow of a loop.
1. The break
statement is used to exit a loop immediately. When the break
statement is encountered inside a loop, the loop is terminated, and the program continues with the next statement after the loop.
Here's an example that demonstrates the use of the break
statement:
arduino#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
printf("\nOut of loop.\n");
return 0;
}
In this example, a for
loop is used to print the numbers 1 to 10. However, when the value of i
is 5, the break
statement is executed, which causes the loop to terminate immediately. Therefore, only the numbers 1 to 4 are printed, and the program continues with the printf()
statement outside the loop, which prints "Out of loop." to the console.
The output of this program is:
arduino1 2 3 4
Out of loop.
In addition to for
loops, the break
statement can also be used in while
and do-while
loops.
2. C also provides the continue
statement, which is used to skip over the current iteration of a loop and continue with the next iteration.
Here's an example that demonstrates the use of the continue
statement:
arduino#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
printf("%d ", i);
}
printf("\nOut of loop.\n");
return 0;
}
In this example, a for
loop is used to print the numbers 1 to 10. However, when the value of i
is 5, the continue
statement is executed, which causes the loop to skip over the current iteration and continue with the next iteration. Therefore, the number 5 is skipped, and the program continues to print the numbers 1 to 4 and 6 to 10.
The output of this program is:
arduino1 2 3 4 6 7 8 9 10
Out of loop.
In addition to for
loops, the continue
statement can also be used in while
and do-while
loops.
C Programming Basic Concepts << Previous || Next >> Arrays and Strings in C
Comments
Post a Comment