• Articles
  • Tutorials
  • Interview Questions

Control Statements in C: Types and Examples

Control Statements in C: Types and Examples

In this blog, we will discuss everything about control statements in C. Moving ahead, you’ll learn what are the types of control statements with examples. 

Table of Contents

Explore the world of C programming with this captivating YouTube video—your gateway to a comprehensive learning experience!

Video Thumbnail

What are Control Statements in C?

Control statements in C programming are the statements that control the flow of a program in C. It controls the order in which certain instructions or statements will be executed. In other words, it decides the direction of the flow of execution of a program. These statements allow us to define conditions and make decisions based on those conditions. Moreover, control statements allow us to repeat actions in our programs.

If you want to know more about C programming, you can go through this C Programming Certification Course!

Types of Control Statements in C with Examples

There are three different types of control statements in the C programming language, i.e., conditional statements, jump statements, and iteration statements. All of these control the execution flow differently. In the following section, these types of control statements are discussed in detail.

Conditional Statements in C

Conditional statements are statements that allow different blocks of code to execute based on different conditions. For example, we say a person will only pass the exam if he obtains marks greater than 40. This is a conditional statement that allows certain outcomes to occur only if some condition is met. There are multiple conditional statements in C, and all of those are discussed below:

1. If Statement: In a simple if statement, there is a condition, and the statements or code inside the if block is executed only when the condition is true. If the condition is not true, the statements outside the if block will be executed.

The following flow chart explains the flow of execution in an if-statement:

 if Statement

The syntax for an if statement in C is as follows:

if(condition_expression)
{
Statement_to_be_executed;
}

The following example demonstrates the implementation of an if statement in C:

#include <stdio.h>
int main() {
int x=10;
if(x>5){
  printf("The number is greater than 5.");
return 0;
}

This program will print ‘The number is greater than 5.’ on screen. Inside the main() function, if the number is less than or equal to 5, the control will be passed to the very next statement after the if statement. In this case, there is no statement outside the if-block, so it will not return anything and give us a blank screen. 

Get 100% Hike!

Master Most in Demand Skills Now!

2. If-Else Statement: Unlike the if statement, if-else has two blocks. First, the control will go to the if-statement and evaluate the expression. If the condition is false, the control will go to the else statement and execute the code inside it. Afterward, it will go to the statement written after the else block. If the statement is true, the control will not go to the else block and will directly execute the next statement written right after the else block. It is used when we generally have two possibilities, where one is true and the other is false. The following flow chart shows the flow of execution of the if-else statement.

if-else Statement

The syntax of an if-else statement is as follows:

if(condition_expression)
{
Statement_to_be_executed;
}
else{
Other_statement_to_be_executed;
}

The following example will help you understand how an if-else statement is used in C programming:

#include <stdio.h>
int main() {
int x=5;
if(x>5){
  printf("The number is greater than 5.\n");
}
else{
    printf("The number is less than or equal to 5.\n");
}
printf(“End of the Program.”);
return 0;

In this program, as the if-statement is false, the else block will be executed, and we will get the following output:

The number is less than or equal to 5.

End of the Program.

3. Nested If-Else Statements: This is just an advanced version of if-else statements. This statement is applied when we have more than two conditions. In this concept, we have if-else statements placed inside the if or else block. These statements help us handle complex decision-making scenarios. 

The following flow chart will help you understand the concept in a better way:

Nested if-else Statements

The syntax for nested if-else statements is as given below:

//outer-if block
if (condition1) 
{
   // Executes when condition1 is true
      if (condition2) 
   {     
  // Executes when condition2 is true
    Statement_to_be_executed;
   }
   else
   {
         // Executes when condition2 is false
  Statement_to_be_executed;
   }
}
//outer else-block
else{
   // Executes when condition1 is false
   if (condition3) 
   {
     // Executes when condition3 is true
        Statement_to_be_executed;
   }
   else
   {
         // Executes when condition3 is false
         Statement_to_be_executed;
   }

To understand its implementation in a better way, see the following example:

#include<stdio.h>
int main()
{
int x, y, z;
printf("Enter three numbers one by one:\n");
scanf("%d%d%d",&x, &y, &z);
//outer-if block
//first if-condition
if(x>y)
{
//code to be executed when the above condition is true
  if(x>z)
  {
  printf("The largest number is %d", x);
  }
  else
  {
  printf("The largest number is %d", z);
  }
}
//outer else-block
else
{
  //code to be executed when the first if-condition is false
  if(y>z)
  {
  printf("The largest number is %d", y);
  }
  else
  {
  printf("The largest number is %d", z);
  }
}
return(0);

In this code, we are checking the largest of three numbers. We enter three numbers, and the control goes to the first if-statement. It checks if the first number entered is greater than the second number. If it is true, it goes to the next if statement inside the first if-block. Again, it checks if the number is greater than the third number or not; if yes, it executes the if block. Otherwise, it goes inside the else block and executes the else block. 

On the other hand, if the first if-statement is false, the control is given to the outer else block, and the same procedure is followed as it did in the outer if-block.

Here, if we enter the numbers 23, 45, and 87, it gives the following output:

Enter three numbers one by one:

23

45

87

The largest number is 87

4. If Else-If Ladder Statement: Here, we have an if statement block, and inside it, there are else-if statements. Then, there is the else block, which executes if all the other statements are false. The execution of the program starts from the top and goes on until it finds a false condition. To understand this, see the following flowchart:

if else-if ladder Statement

The syntax of the if else-if ladder in C programming is as follows:

if(condition1) {
}
else if(condition2) { 
 // code to be executed when the if condition is false and the condition of this else-if is true
}
else if(condition3) { 
 // code to be executed when the previous else-if condition is false and the condition of this else-if is true
}
.
.
.
.
.
.
else 

// code to be executed when no previous condition is true
}

Consider the following example, where we are calculating the grade of a student based on his percentage:

// using the if else-if ladder
#include <stdio.h>
int main()
{
int m;
printf("Enter marks:");
scanf("%d%d%d",&m);
if (m <= 100 && m>= 95){
printf("S Grade");}
else if (m < 95 && m >= 85){
printf("A Grade");}
else if (m < 85 && m >= 70){
printf("B Grade");}
else if (m < 70 && m >= 60){
printf("C Grade");}
else if (m < 60 && m >= 50){
printf("D Grade");}
else{
printf("Failed");}
return 0;
}

The output of this program will be:

Enter percentage:65

C Grade

Get ready for high-paying programming jobs with these Top C & Data Structure Interview Questions And Answers!

Jump Statements in C

These statements are used when we want to skip a portion of the code and jump to some other part of the code. This alters the flow of the program and shifts control from one part of the code to another. There are multiple jump statements in C, including break, continue, goto, and return. All of these are discussed in detail in the section below: 

1. break in C: The break statement in C is used to break the loop and get control out of the loop without executing the remaining loop. To understand the flow of execution, see the following flow chart:

break in C

The syntax for the break statement is as follows:

break;

An example of the implementation of break for-loop is as follows:

// C program to illustrate the break in c loop 
#include <stdio.h> 
int main() 

int x; 
// for loop 
for (x = 1; x <= 8; x++) { 
//When x = 4, the loop should end 
if (x == 4) { 
break; 

printf("%d \n", x); 

printf("exited the loop.\n"); 
return 0; 
}

This program gives the following output:

exited the loop.

2. continue in C: This statement is used to bring the control of the program to the beginning of the loop. It will skip the current iteration and move the control to the start of the loop to execute the remaining iterations.

The following flow chart elaborates on the flow of the execution of the continue statement:

continue in C

The syntax of continue in C is as follows:

break;

This example will help you understand the concept better:

#include <stdio.h>
int main() {
    // loop with 6 iterations
    for (int i = 1; i <= 3; i++) {
        //Continue to print skipping number 3
        if (i == 3) {
            continue;
        }
        printf("%d ", i);
    }
    return 0;
}

This gives the output:

1 2 4 5 6 

3. goto statement in C: The goto statement allows a program to jump to a labeled statement elsewhere in the code. The label is an identifier followed by a colon, and it marks a specific point in the code. The flow of execution of the goto statement is as shown in the picture below:

goto Statement

The syntax for the goto statement is as follows:

goto label;

Consider the following example for a better understanding:

#include <stdio.h>
int main() {
    int i = 0;
    loopStart:
        if (i < 5) {
            printf("%d\n", i);
            i++;
            goto loopStart;  // Jump back to the labeled statement
        }
    return 0;
}

This program prints the value of i as long as i is less than 5. The output of this program will be as follows:

0

1

2

3

4

To become an expert in C programming practice these C pattern programs.

Iteration Statements in C

Iterations are done with the help of loop statements. Loop statements aim to perform repetitive operations using a short block of code. The loop keeps going on until the condition is false. There are multiple loops in C, i.e., while, do while, and for. All of these loops are discussed in the section below:

1. For loop: The for loop in C is a control flow statement that allows you to repeatedly execute a block of code a certain number of times. It’s often used when you know in advance how many times you want to iterate. The following flow chart explains for loop execution flow:

for Loop

The syntax of the for loop in C is as follows:

for (initialization; condition; update) {
    // Code to be repeated
}

An example of this loop is as follows:

#include <stdio.h>
int main()
{
int x = 0;
// conditional statement
for (x = 1; x <= 5; x++) 
{
// statement will be printed
printf("Intellipaat\n");
}
// Return statement to tell that code executed successfully
return 0;
}

The output of this program is as follows:

Intellipaat

Intellipaat

Intellipaat

Intellipaat

Intellipaat

2. While loop: While loop iterates over the loop body until the condition keeps returning true. The moment the condition becomes false, the loop ends. It is used to evaluate a test condition. This loop is used when the number of iterations is not known before writing the program. The flow control diagram and the syntax of the while loop are given below:

While loop 

The syntax for the while loop is as follows:

while (condition) {
    // Code to be executed as long as the condition is true
}

The following example will help you understand the while loop implementation in C:

#include <stdio.h>
int main()
{
int x = 0;
//Setting the test expression as (i < 3), means the loop
// will execute till i is less than 3
while (x < 3) {
// loop statements
printf("Intellipaat\n");
x++;
}
return 0;
}

Here, the loop starts at x = 0 and goes on until the value of x is less than three. When the value of x gets equal to 3, the loop terminates and gives us the final output. The output of this program looks like this:

Intellipaat

Intellipaat

Intellipaat

3. do-while Loop: The do-while loop in C is another type of loop that is used to repeatedly execute a block of code as long as a specified condition is true. Unlike the while loop, the do-while loop guarantees that the block of code is executed at least once because the condition is checked after the execution of the block. The following image explains the flow of control in the do-while loop:

do-while Loop

The syntax of a do-while loop is as follows:

do {
    // Code to be executed
} while (condition);

Let’s see an example of implementing a do-while loop in C:

#include <stdio.h>
int main()
{
    // loop variable declaration and initialization
    int x = 0;
    // do while loop
    do {
        printf("Intellipaat\n");
        x++;
    } while (x < 5);
    return 0;
}

This program prints the string “Intellipaat” to the console. The do-while loop executes as long as the condition x < 5 holds. Consequently, the output of the program consists of the string “Intellipaat” printed five times on separate lines. The `do-while` loop, in this scenario, ensures the execution of the code block even if the initial condition is false. The output looks like the following:

Intellipaat

Intellipaat

Intellipaat

Intellipaat

Intellipaat

C statements truly initiate the fundamentals of programming, read our blog on break and continue statements in C to know more.

Conclusion

Control statements in C are important tools for directing the flow of a program. They allow developers to implement decision-making and repetitive tasks, enhancing the flexibility and adaptability of code. By enabling conditional branching and looping constructs, these statements help create efficient and readable programs. They provide the means to respond dynamically to different scenarios, improving code organization and promoting modular design. Careful use of these control statements contributes to the creation of logically structured, maintainable, and expressive code in the C programming language.

FAQs

What is the difference between "if" and "else if" statements?

The “if” statement is used for the primary condition. If you have multiple conditions to check, you can use “else if” statements after the initial “if” statement to handle additional conditions.

How do you prevent an infinite loop in C?

To prevent an infinite loop, ensure that the loop condition becomes false at some point. Check that the loop termination condition is being updated within the loop body, so the loop can exit when the condition is no longer satisfied.

What is the purpose of the "continue" statement?

The “continue” statement is used to skip the rest of the code inside a loop for the current iteration and move to the next iteration. It allows you to selectively skip portions of the loop’s body.

How does the "break" statement work?

The “break” statement is used to exit from a loop or a switch statement prematurely. When encountered, the “break” statement terminates the loop or switch and transfers control to the statement immediately following the loop or switch.

Course Schedule

Name Date Details
Python Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.