Control statements in C are programming constructs that determine the order in which instructions are executed within a program. They allow for decision-making, repetition of code blocks, and alteration of the program's flow based on specific conditions.
Decision making Statements :-
Decision making statements, also known as conditional statements, are fundamental programming constructs that allow a program to execute different code blocks based on whether a specific condition is true or false.
If Statement:- This executes a block of code only if a specified condition is true. If the condition is false it will not executed .
Syntax: - if(condition)
{
Statement ;
}
If else Statement:- This executes one block of code if the condition is true and a different block if the condition is false.
Syntax: - if(condition)
{
Statement 1 ;
}
else
{
Statement 2;
}
Nested If else Statement:- A nested if-else statement in C means placing one if or else block inside another. This is useful when you want to check multiple conditions that depend on each other.
Syntax: if (condition1) {
// Executes if condition1 is true
if (condition2) {
// Executes if condition1 and condition2 are true
} else {
// Executes if condition1 is true but condition2 is false
}
}
else {
// Executes if condition1 is false
}
Ladder If else Statement: This structure is useful when you need to check multiple conditions in sequence. Also known as if else if .
Syntax: if (condition1) {
// Code block if condition1 is true
} else if (condition2) {
// Code block if condition2 is true
} else if (condition3) {
// Code block if condition3 is true
} else {
// Code block if none of the above conditions are true
}
Switch Statement: The switch statement is used to select one of many code blocks to execute based on the value of a variable or expression.
OR
When the number of conditions becomes large it becomes too dificult to use ladder and nested if else so we use switch staement.
Syntax:
switch (expression) {
case constant1:
// Code block for case 1
break;
case constant2:
// Code block for case 2
break;
...
default:
// Default code block if no case matches
}
Looping Statements :-
Looping statements are used to repeat a block of code multiple times. C provides three primary types of loops.
while loop: This loop repeats a block of code as long as a specified condition remains true. The condition is evaluated before each iteration, meaning if the condition is initially false, the loop body may not execute at all.
Syntax : while (condition)
{
// code to be executed
}
do while loop: Similar to the while loop, but it guarantees that the loop body executes at least once because the condition is tested after the first iteration.
Syntax : do {
// code to be executed
} while(condition);
for loop: The main problem with while and do while is that in these we do initilization , condition and increment/decrement in different steps, but in for loop initilization , condition and increment/decrement done in a single step .
Syntax : for(initilization ; condition ; increment/decrement)
{
Statement;
}
Example : for(i=0 ; i<=10 ; i++)
{
printf(:Numbers are ",i);
}
Jumping Statements :-
Jumping statements are used to alter the normal flow of control in a program. They allow you to exit loops, skip iterations, or jump to another part of code.
break : The break statement in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block. The break statement can only break out of a single loop at a time.
Syntax : break;
continue : The continue statement in C is a jump statement used to skip the current iteration of a loop and continue with the next iteration. It is used inside loops (for, while, or do-while) along with the conditional statements to bypass the remaining statements in the current iteration and move on to the next iteration.
Syntax : continue;
goto:The goto statement in C allows the program to jump to some part of the code, giving you more control over its execution. While it can be useful in certain situations, like error handling or exiting complex loops, it's generally not recommended because it can make the code harder to read and maintain .
Syntax : goto (level name); Here level name means the part of the program which you want to execute .