Control structures in C programming allow you to control the flow of execution of your program based on conditions or repetition. There are three main control structures in C:
- Conditional (if-else) statements: These are used to execute a block of code only if a certain condition is met.
Example:
int x = 10;
if (x > 5) {
printf(“x is greater than 5”);
}
- Loops: These are used to repeatedly execute a block of code until a certain condition is met.
Example:
for (int i = 0; i < 10; i++) {
printf(“%d\n”, i);
}
- Switch statements: These are used to select one block of code from many to be executed.
Example:
char grade = ‘B’;
switch (grade) {
case ‘A’:
printf(“Excellent!”);
break;
case ‘B’:
printf(“Good!”);
break;
case ‘C’:
printf(“Average”);
break;
default:
printf(“Invalid grade”);
}
Decision making structures
Decision making structures in C are used to make decisions in a program based on certain conditions. There are two main decision making structures in C:
The if
statement in C programming is used to execute a block of code only if a certain condition is met. The general syntax for an if
statement is as follows:
if (condition) {
// code to be executed if condition is true
}
The if
statement in C programming is used to execute a block of code only if a certain condition is met. The general syntax for an if
statement is as follows:
if (condition) {
// code to be executed if condition is true
}
Here, condition
is a Boolean expression that can be either true or false. If the condition
is true, the code inside the braces will be executed, and if the condition
is false, the code inside the braces will be skipped.
Example:
int x = 10;
if (x > 5) {
printf(“x is greater than 5”);
}
In this example, the if
statement checks if x
is greater than 5. If it is, the message “x is greater than 5” is printed.
- if-else statement: The if-else statement allows you to execute a block of code if a certain condition is met and another block of code if the condition is not met.
Example:
int x = 10;
if (x > 5) {
printf(“x is greater than 5”);
} else {
printf(“x is not greater than 5”);
}
- switch statement: The switch statement allows you to choose one block of code from many to be executed based on the value of an expression.
Example:
char grade = ‘B’;
switch (grade) {
case ‘A’:
printf(“Excellent!”);
break;
case ‘B’:
printf(“Good!”);
break;
case ‘C’:
printf(“Average”);
break;
default:
printf(“Invalid grade”);
}
Nested If-else
Nested if-else is a construct in C programming where an if-else statement is contained inside another if-else statement. This allows you to test multiple conditions and execute different blocks of code based on those conditions.
Example:
int x = 10, y = 20;
if (x > 5) {
if (y > 15) {
printf(“x is greater than 5 and y is greater than 15”);
} else {
printf(“x is greater than 5 and y is not greater than 15”);
}
} else {
printf(“x is not greater than 5”);
}
In this example, the outer if statement tests if x
is greater than 5. If it is, then the inner if statement is executed, which tests if y
is greater than 15. Depending on the results of these two conditions, different messages are printed.
loop control structures
Loop control structures in C programming are used to repeatedly execute a block of code until a certain condition is met. There are three main loop control structures in C:
for
loop: Thefor
loop is used to execute a block of code a specific number of times. The general syntax for afor
loop is:
for (initialization; condition; increment) {
// code to be executed
}
Here, initialization
is the initial value for the loop variable, condition
is the condition that must be met for the loop to continue executing, and increment
is the step by which the loop variable is incremented on each iteration.
Example:
for (int i = 0; i < 10; i++) {
printf(“%d\n”, i);
}
Loop control structures in C programming are used to repeatedly execute a block of code until a certain condition is met. There are three main loop control structures in C:
for
loop: Thefor
loop is used to execute a block of code a specific number of times. The general syntax for afor
loop is:
cssCopy codefor (initialization; condition; increment) {
// code to be executed
}
Here, initialization
is the initial value for the loop variable, condition
is the condition that must be met for the loop to continue executing, and increment
is the step by which the loop variable is incremented on each iteration.
Example:
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
In this example, the for
loop will execute 10 times, starting from 0 and ending at 9, printing each value of i
on a separate line.
while
loop: Thewhile
loop is used to execute a block of code as long as a certain condition is true. The general syntax for awhile
loop is:
while (condition) {
// code to be executed
}
Here, condition
is the condition that must be true for the loop to continue executing.
Example:
int i = 0;
while (i < 10) {
printf(“%d\n”, i);
i++;
}
In this example, the while
loop will execute 10 times, starting from 0 and ending at 9, printing each value of i
on a separate line.
do-while
loop: Thedo-while
loop is similar to thewhile
loop, but with one important difference: the code inside the loop will always be executed at least once, regardless of the condition. The general syntax for ado-while
loop is:
do {
// code to be executed
} while (condition);
Here, condition
is the condition that must be true for the loop to continue executing.
Example:
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 10);
In this example, the do-while
loop will execute 10 times, starting from 0 and ending at 9, printing each value of i
on a separate line.
do-while
loop: The do-while
loop is similar to the while
loop, but with one important difference: the code inside the loop will always be executed at least once, regardless of the condition. The general syntax for a do-while
loop is:
do {
// code to be executed
} while (condition);
Here, condition
is the condition that must be true for the loop to continue executing.
Example:
int i = 0;
do {
printf(“%d\n”, i);
i++;
} while (i < 10);
In this example, the do-while
loop will execute 10 times, starting from 0 and ending at 9, printing each value of i
on a separate line
Nested for loop
A nested for loop is a loop inside another loop. It allows you to run multiple iterations of the inner loop for each iteration of the outer loop. Here is an example in C:
include<studio.h>
int main() {
int i, j;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 2; j++) {
printf(“i = %d, j = %d\n”, i, j);
}
}
return 0;
}
This code will output:
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
break, continue, goto, exit
Here’s a brief explanation of break
, continue
, goto
, and exit
in C programming language with examples:
break
: Thebreak
statement is used to exit a loop prematurely. It can be used infor
,while
,do-while
loops. Here’s an example:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
return 0;
}
This code will output:
Copy code0
1
2
3
4
continue
: Thecontinue
statement is used to skip the current iteration of a loop and move on to the next iteration. It can be used infor
,while
,do-while
loops. Here’s an example:
cCopy code#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d\n", i);
}
return 0;
}
This code will output:
Copy code1
3
5
7
9
goto
: Thegoto
statement is used to jump to a labeled statement in the program. It can be used to jump out of a deeply nested loop or to jump to a specific section of code. Here’s an example:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
goto end;
}
printf("%d\n", i);
}
end:
printf("Reached end of program.\n");
return 0;
}
This code will output:
0
1
2
3
4
Reached end of program.
exit
: Theexit
function is used to immediately exit a program and return a status value. Here’s an example:
#include<studio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
exit(0);
}
printf(“%d\n”, i);
}
return 0;
}
This code will output:
0
1
2
3
4