Despite the many pre-defined methods, there are instances when case-specific logic is required. Control statements allow the execution of customized statements. By controlling the execution order of the statements, it alters the flow of the statements.
Suppose a particular code block should only be executed once a specific condition is met. In that case, a control statement is excellent for guaranteeing a smooth and deliberate flow of code.
Also Read: Do you know, What is the Need of Data Types in C?
Table of Contents
What is the Need for Control Statements in C?
Some computer instructions call for certain conditions to be accomplished, specific tasks repeated many times, or specified actions to be performed. Utilizing control statements, all of these are achievable.
Also Read: How Pointer Changed the life of C Programming?
For example, for Fibonacci series/factorization loop statements and to determine prime numbers conditional statements are used in programs.
Types of Control Statements in C
Generally speaking, The primary categories of control statements are
- Conditional statements
- Loop statements
- Jump statements
What is Conditional Control Statement in C?
Instructions are carried out depending on the indicated condition in conditional control statements, which are also decision-making statements. It manipulates the execution flow following the conditions.
Also Read: What is String in C Programming?
A true or false boolean value is generated from the execution of codes or instructions. When no condition is stated, it operates sequentially.
What is Switch Statements in C ?
Switch case statements enable multi-way branching that includes default cases as well as conditional cases. It is employed to perform the corresponding case statement of the matching condition among several conditions.
It evaluates its expression before executing each statement that comes after the case label that corresponds to it. When none of the circumstances matches, the default statement is carried out.
Syntax
Switch (expression){
Case label 1:
statement1;
break;
Case label 2:
statement2;
break;
Case label 3:
statement3;
break;
…………….
…………….
…………….
Case label n:
statement n;
break;
default:
default statement;
}
Code block:
int n = 5;//declare variable
printf(“n is %d \n”, n);
//print the comment stating n value taken
switch(n){//’n’ expression is compared with case values
case 1: //check if n is 1
printf (“the value of n is 1”); //if print the following
break; //stop the execution
case 2: //check n value is 2 or not
printf (“the value of n is 2”); //if print the following
break; //stop execution
case 3: //check if n is 1
printf (“the value of n is 3”); //print the following
break; //stop the execution
default: //when none cases match, default is executed
printf(“value is invalid”);//print the following
}
Output: the value of n is 3
What is Decision Control Statement in C?
It is necessary to make several decisions while programming based on the findings or to resolve problems like error handling. As a result, decision-making is a key notion in the management and control of code execution.
The following sections describe the C decision-making statements. These are also conditional statements as they make decisions based on the condition given.
If Statement in C
It is a one-way selection statement, meaning that only if the provided condition is true will the code be implemented. This suggests that an argument is added when a conditional statement is used. If it is satisfied, the code is compiled.
Syntax:
if(argument){…//if statement//…}
Flow Chart:
When conditional code is compiled, the following block—the if block—is performed if the argument is satisfied. If the argument is false, the if block is bypassed, and the statement outside the if block is run.
Code block:
int a = 3;
int b = 4;
//defining integer type variable
if(a % b == 0)//check if a is greater than b i.e. if argument
{//opening if block bracket
printf(“%d is divisible by %d”, a, b);
//print since the supplied argument is true
}//closing if block bracket
printf(“%d is not divisible by %d”, a, b);
//in case the argument is false
Output: 3 is not divisible by 4
If-Else Statement in C
If else is a two-way selection statement, unlike an if statement. This statement applies to the boolean argument values, true or false, addressing success and failure conditions. It is the better decision in situations with several variables or prolonged conditional parameters.
Syntax:
if(argument){
…//handling true argument statement//…
}else{
…//false argument handling statement//…
}
Flow Chart:
If the specified argument is true, the if block is executed in if-else statements; otherwise, the else block is executed. The subsequent sequential block below the if-else block is compiled following its execution.
Code block:
int num=0;
//declaring a variable
printf(“enter the number”);
//print comments for user convenience
scanf(“%d”,&num);
//gets the user value
if(num%2==0)//check the argument is true or false
{//starting of if block bracket
//execution of if block since the provided argument is true
printf(“%d number in even”, num);
//print the following
}//end point of if block, i.e., closing brackets for if block
else//execution of else block upon argument failure
{//start of else block
printf(“%d number in odd”, num);
//print the statements
}//closing brackets for else block
Input: 4
Output: 4 number is even
Nested-if Statement in C
Nested-if statements are often used when more than an expression is expected and a series of judgments must be decided. It supports numerous alternative outcomes when a prior argument’s viability has been established, and a new argument has to be presented instead of if-else, which only allows two outcomes.
To put it simply, it is a multi-way selection statement. It results in condition-bubbling issues and increased code complexity. Consequently, the programmers wouldn’t promote it.
Syntax:
if(argument1){//argument1 is valid then check argument2 validity
if(argument2){
//statement//
}else{
//statement//
}
}else{
if(argument3){
//statement//
}else{
//statement//
}
}
Flow Chart
We can manage layered conditions with ease by using nested if statements. In this case, if one condition is met, we check the other conditions listed under condition 1 and follow the instructions. If a condition is not met, other conditions under it are checked.
Code block:
int a, b;
//declare variables
printf(“enter the numbers for a and b”);
//print comments for user
scanf (“%d%d”, 8a, &b);
//getting the user value
if (a != b){//checking condition validity
//condition to true
printf(“%d is not equal to %d “, a, b);
//print the following
if (a>b){//check another condition, i.e., if a is not equal to b, then is it greater or lesser
//argument is valid, then
printf(“%d is greater than %d”, a, b);
//print the following
}else{ //if the condition is not met, follow the following instruction
printf(“%d is less than %d”, a, b);
//print this
}else{//condition one is false
printf (“%d is equal to %d “, a, b);
//print a = b
}//closing else block
Input: 5
7
Output: 5 is less than 7
Else-If Ladder in C Programming Language
Following a failure of the if statement, the multipath decision statement if else if ladder examines several criteria one after another. To execute the statement under the if condition, whenever any of the requirements is true, the multiple else if is executed. The execution is carried out from top to bottom.
An else if block is made up of a succession of if…then statements, with each followed by an else if statement and ending with an else statement that comes in handy for debugging.
Syntax:
if (condition1){
//statement1;
}else if (condition2){
//statement2;
}else if (condition3){
//statement3;
}
……………
……………
……………
else if (condition n-1){
//statement n-1
}else{
//statement n;
}
Flow Chart
Suppose if’s condition is valid, then the statement 1 path is followed; otherwise, if condition validity is checked and as per validity, its following statement block is executed. This process will repeat as long as the else-ifs are in the program. In case neither is true, then the else statement gets executed.
Code block:
int a;
//declaring a variable
printf(“enter the number for a”);
//print the comment for the user
scanf( “%d”,&a);
//get the user input
if(a != 10){ //check the condition 1 validity
printf(“%d is not equal to 10 “, a);
//then print the following
}else if(a>15){ //check condition 2 validity after confirming that condition 1 is not valid
printf (“%d is greater than 15”, a);
//then print the following
}else if(a<28){ //check condition 3 validity after confirming that condition 2 is not valid
printf(“%d is less than 20”, a);
//then print the following
}else{ //follow else block when all the condition mentioned is not valid
printf(“%d is equal to 10 “a);
//then print the following
}
Input: 67
Output: 67 is greater than 15
What is Loop Statements in C?
Loop statements run code multiple times until a certain criterion is met or the assertion is proven false. The test expression is assessed here, and the count is initialized. It streamlines code and facilitates traversing an array’s components.
Its real-world uses include ATMs, which process the transaction in the loop. Loop statements are:
While Loop in C Programming Language
The condition is assessed before the statement is performed, making it an entering loop. This implies that a code loop is performed when the provided condition is true; else, execution ends.
Syntax: In its structure, conditions are enclosed in parentheses.
while(expression){
//statement//
}
Flow chart
Till the expression is valid, the loop statement will be executed repeatedly.
Code block: A program for printing a table of 2.
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf(“%d “, 2*num);
num++; //incrementing operation
}
printf(“\nNum value: %d “, num);
Output: 2 4 6 8 10 12 14 16 18 20
Num value: 10
Do-while Loop in C
The do-while loop runs the loop statement, then evaluates the statement’s criteria to determine if the condition is true or not. Statements are re-executed each time the condition evaluates to be true. Thus the term exit loop and post-test loop.
It is simply a while loop version, but Regardless of whether the condition is tested first, it always runs at least once, which is a key distinction.
Syntax: Its framework consists of while and condition.
do{
//statement//
}while(expression);
Flow chart
Code block:
int num=1; //initializing the variable
do{//do-while loop
printf(“%d\n”,2*num);
num++; //incrementing operation
}while(num<1);
Output: 2
As per the while loop:
int num=1; //initializing the variable
while(num<1) //while loop with condition
{
printf(“%d “, 2*num);
num++; //incrementing operation
}
printf(“\nNum value: %d “, num);
Output: Num value: 10
This signifies that whatever the condition is given, the do-while loop will run for once instead of the while loop.
For Loop in C
A for loop, in which the validity of the condition is tested before each re-evaluation, is another method of running a portion of the program until a certain condition is fulfilled. Therefore a pre-test loop. Unlike other loop statements, it is utilized when the needed number of iterations is known.
Syntax: Three arguments contribute to the structure: initialization in argument 1, a constraint in argument 2, and updating in argument 3.
for(expression 1; expression 2; expression 3){
//statement//
}
Flow chart
Code block: a program for printing a table of 2.
int i; //declare a variable
for (i = 1; i < 11; i++){//iteration from 1 to 10.
//expression 1: i = 1(initialization), expression 2: i < 11(constraint), expression 3: i++(updating)
printf(“%d “, 2*i);//print the evaluated value
}
Output: 2 4 6 8 10 12 14 16 18 20
What is the Difference between for loop, while loop, and do while loop?
For Loop | While Loop | Do While Loop |
Syntax: For(initialization; condition; updating) {// Statements; } | Syntax: while(condition) {// Statements; } | Syntax: do {//Statements; } while(condition); |
Entry loop | Entry loop | Exit loop |
When the condition is not true the first time then control will never enter inside the loop. | When the condition is not true the first time then control will never enter inside the loop. | Even When the condition is not true for the first time the control will enter inside the loop and execute the statements once. |
Unconditional/Jump Statement in C
Jump statements block program flow by diverting the program’s execution from one point to another. Generally, It is employed to customize the behavior of conditional and sequential statements within an established loop or to avoid a program segment as a replacement for loops.
Different forms of jump statements in C are:
Break Statement in C
Break ceases the operation instantaneously, pausing the computation of the code to be implemented and restarting the execution from the following section.
Break is widely applied in association with loops especially switch cases.
Syntax: break;
Flow chart
Code block:
int var = 1; //initialize the value to a
while (var <= 10) // run loop unless the value is 10
{
if (var == 3) // if the value is 3
break; //break the loop
printf(“var:%d \n”, var);
var++;
}
printf(“Outside the loop”); //print statement outside the loop
Output: var=1
var=2
Outside the loop
Continue Statement in C
Continue commands the loop to move on to the subsequent iteration after skipping a particular one. Only iterative statements like “do,” “for,” or “while” and continue statements are used together.
A continue in a line command activates a loop’s increment and conditional test sections. It restarts the loop by giving the program control.
Syntax: continue;
Flow chart
Code block:
int main()
{
int i;
for ( i = 1; i <= 4; i++ )
{
if( i == 3) //condition
{
continue; //continue statement and increment i directly without executing next statements
}
printf(“value = %d \n”, i);
}
Output: value: 1
value: 2
value: 4
Return Statement in C
Return terminates or closes the program instantly, with or without a value, and restarts the execution of the program from the spot at which it is called. It makes the current subroutine exited and the code restart execution at the instruction’s immediate successor.
It is frequently used inside functions to return a value to the caller function. All additional return statements will be canceled and of no further use after the return statements.
Syntax: return;
or
return //expression//;
Flow chart
Code block:
int sum()
{
int var = 0;
int num;
//initialization
for( num = 0; num <= 10; num++) {//looping
a += num; //var = var + num
}
return var; //return the value at the function calling point
}
void main()
{
int a = sum();
printf(“Sum = %d\n”, var);
return 0;
}
Output: Sum = 55
Goto Statements in C
With the goto statement, we can specify to transfer control to the pre-defined label after skipping. It is similar to continue, but unlike continue, it can be used anywhere in the program and has fewer restrictions. It can enter the scope of any variable other than a variable-length array or variably-modified pointer.
It is rarely used because it makes the program confusing, less readable, and complex. Hence considered a poor programming approach.
Syntax: goto //label//;
Flow chart
Code block:
int var = 4; //initialized
Even: //label code
printf(“%d is Even”, var);
var++;
if (var%2 == 0)
goto Even; //goto statement
Output: 4 is even
Conclusion
Conditional, loop and jump statements are control statements in the C programming language. By manipulating the program’s control flow, we may accomplish various goals. Of course, it has both benefits and drawbacks, which we discussed in the post, along with details on how it works and how it is used in programs. Also, check our other content.
Frequently Asked Questions (FAQs)
Q1. Differences between the while loop and for loop in the C.
Ans: The significant difference between the while loop and the do while loop is that in a while loop, conditions are checked before the loop statement execution, whereas in the do while loop block is executed once, and then conditions are verified.
Q2. Difference between for loop and do while loop.
Ans: For loop consumes the initialization, condition, and increment/decrement in one line, providing shorter and easy debugging code, unlike while loop, which consists of an expression that can be updated in the block only.
Q3. How can we use a C loop to decrease the number from 10 to 1?
Ans: To decrement the number from 10 to 1, any loop can be used the syntax is.
For the for loop the expression will be – for(int n=10; n>=1;n – -){//statement}
While loop – int n = 10;
while(n>=1){
//statement//
n = n – 1;
}
Do while loop – int n = 10;
do{
//statement//
n = n – 1;
}while(n>=1);
Q4. Difference between entry control loop and exit control loop.
Ans: Entry control loops are while and for loops in which the condition is checked before the block statement execution. Exit control loops verify the conditions at the exit, such as the do-while loop.
Q5. Difference between if-else and while loop.
Ans: if else statements are used to handle the various possibilities during programming, whereas the while loop iterates a set of instructions till the condition is true.
Q6. Why do we need a loop in c?
Ans: loop is used to execute code multiple times based on a certain condition without writing it repeatedly, which makes it easy to debug and neat.