You are here: Home / Topics / Unit-3: Control structures : BCA semester 1

# Unit-3: Control structures : BCA semester 1

Filed under: BCA Study Material

```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”);}     2. 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);}3. 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 structuresDecision 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-elseNested 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 structuresLoop 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: The for loop is used to execute a block of code a specific number of times. The general syntax for a for 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: The for loop is used to execute a block of code a specific number of times. The general syntax for a for 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.2. while loop: The while loop is used to execute a block of code as long as a certain condition is true. The general syntax for a while 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.3. 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.4. 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 lineNested for loopA 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 = 1i = 1, j = 2i = 2, j = 1i = 2, j = 2i = 3, j = 1i = 3, j = 2break, continue, goto, exitHere’s a brief explanation of break, continue, goto, and exit in C programming language with examples:1.break: The break statement is used to exit a loop prematurely. It can be used in for, 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:0
1
2
3
4
2. continue: The continue statement is used to skip the current iteration of a loop and move on to the next iteration. It can be used in for, while, do-while loops. Here’s an example:#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:1
3
5
7
9
3. goto: The goto 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: The exit 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:01234```