The control statements in c specify the flow of the program execution.

Control statements in c

What is Control statement?
The Control statements in c enables us to specify the flow of the program that is the order in which the program gets executed.
These Controls statements are able to make decisions, can execute the same statements repeatedly or can jump to any other statement. This is called conditional or looping.
Conditional statements and looping in C are used to change the flow of control of the program.
The flow of control means that the way in which the program executes.

Control statements in c
Control statements in c

There are three types of control statements in c

  1. Conditional statements
  2. Looping Statements
  3. Selection Statements

Conditional statements in c

These statements are also referred to as a decision making statements in c.
What is a conditional statement?: The set of statements are executed based on some condition. In C, condition statement is written based on the condition the decision is taken and perform a specified action.
In C, there are different conditional statements are available which are as follow:

1. If statement

If statement is the simple conditional statement in c. if statement contains condition along with this. if followed with the condition, if this conditional is true then the statements followed by if a statement is executed.
Syntax:
if(condition){
// block of statements
}
Let’s see example which is to check where number is positive if positive then print number is positive number using If statement in c.

if statement program in c
#include <stdio.h>

int main()
{
int a;

printf(" Enter a number:");

scanf("%d", &a);

if(a>0)

{

printf( "\n The number %d is positive.",a);

}
return 0;
}

Output:

If Statement2. If_else statement

if _else is also the conditional statement in c which most commonly used. if followed by condition if this condition is true then the block of statements followed by if is executed. Otherwise, the statement in else block is executed.
Syntax :
if(condition){
// block of statements
}else
{
// block of statements
}
Let’s see example which is to check where number is positive or negative if positive then print number is positive number else number is negative using If else statement in c.

If else statement in c program
#include <stdio.h>

int main()
{
int a;

printf(" Enter a number:");

scanf("%d", &a);

if(a>0)

{

printf( "\n The number %d is positive.",a);

}
else

{

printf("\n The number %d is negative.",a);

}
return 0;
}

Output:

If else StatementLooping statements in c

Looping statement is used to execute the set of statements repeatedly.
These set executed repeatedly the number of times based on some condition.
There are different statements in c which are as follow:

1. while loop

This loop checks the condition first if its true then the block of statements followed by the while is executed else not.
Syntax:
while(condition){
//Block of statements
}

While loop program in c
#include <stdio.h>

int main()
{
int n=1;

while(n<=5)

{

printf( "\n The number is = %d ",n);
n++;

}
return 0;
}

Output:

While Loop in c

2. do _ while loop

This loop checks the condition at the end of the loop. It executes the block first after that checks the condition. The do _while loop executes at least once.
Syntax :
do{
//statements
} while(condition);

Do-while loop program in c
#include <stdio.h>

int main()
{
int n=1;

do
{

printf( "\n The number is = %d ",n);
n++;

}while(n<=5);

return 0;
}

Output:

While Loop in c3. for loop

This is most commonly used looping in c. The first statement in loop treated as a statement, second treated as the condition if it is true then loop executes, and third treated as the increment or decrements which used for the iterating loop.
Syntax:
for(exp1;exp2;exp3){
//statements
}

For loop in c program
#include <stdio.h>

int main()
{
int n=1;

for(n=1;n<=5;n++)
{

printf( "\n The number is = %d ",n);

}

return 0;
}

Output:

While Loop in c

Selection Statement in C

Selection statement is based on selecting the one among multiple. In C, the switch statement is used to implement such a scenario.

Switch statement

This switch statement which contains multiple branch statements. Its checks with each value. Each value is called as the case. if this case true then statement, in this case, is executed.
Syntax:
switch(expression){
case value:
// statements.
break;
case value:
// statements.
break;
case default:
//statements
break;
}
we write break after each case because we do not need to execute rest of the cases.

Switch statement program in c

#include<stdio.h>

int main( )

{

int day;

printf("Enter the number of the day:");

scanf("%d",&day);

switch(day)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Invalid choice");
}
return 0;

}

Output:

switch StatementBreak statement

This is usually used to break the flow of control. or to terminate loop and program flow goes to the next statement after the loop. This can be used in some situation where for a particular case you want to stop the execution of the loop. Let see example where we have an array of some positive and negative numbers and print numbers from an array and stop execution after finding the negative number.

Break Statement Example
#include <stdio.h>

int main()
{
    int n[5]={10,2,-1,6,-3};
    int i;
    for(i=0;i<5;i++){
        if(n[i]<=0)
        break;
        printf("\n Number=%d ",n[i]);
    }
    return 0;
}

Continue statement:

This is slightly similar to break. It restarts the loop with the next value. The code below continues skipped and move to the next iteration. Let see an example where we have an array of some positive and negative numbers and print numbers from an array by skipping negative numbers.

Continue Statement Example
#include <stdio.h>

int main()
{
    int n[5]={10,2,-1,6,-3};
    int i;
    for(i=0;i<5;i++){
        if(n[i]<=0)
        continue;
        printf("\n Number=%d ",n[i]);
    }
    return 0;
}

 

Write A Comment