hamburger

What is Control Statements in C?

By BYJU'S Exam Prep

Updated on: September 25th, 2023

Control Statements in C allow us to define program control flow in the order in which the program’s instructions are executed. We have observed that a C program is executed sequentially from the first statement to the last statement. That is, the second statement is executed after the first, the third statement is executed after the second, and so on and so forth.

Control Statements in C PDF

Sometimes we need to execute a particular code segment according to our requirements. In such situations, control statements in C help us. In this article, we will learn about the control statements in C and various types of control statements in C, along with their examples. 

Download Formulas for GATE Computer Science Engineering – Programming & Data Structures

What are Control Statements in C?

Control statements in C enable the user to execute a particular code block conditionally. C supports decision control statements that can alter the flow of a sequence of instructions. The control flow statements allow the user to execute the code as per the requirements without executing it in sequential order.

It is expected that questions can be formulated based on the control Statements in C in the GATE CSE exam. Control statements hinder the sequential flow of execution; using control statements in C programming, we can branch to a particular code, iterate a particular segment of code or jump to a segment of code based on the conditions.

Types of Control Statements in C

In C programming, we have three types of control statements. They allow the user to execute a particular code segment and skip the rest based on the programmer’s need. There are three types of control statements in C; they are as follows:

  • Decision Control Statements
  • Iterative Statements
  • Jump Statements

Decision control or branching means deciding what actions to be taken, looping means deciding how many times a particular code will execute, and jump statements mean transferring the control from one point to another. We shall briefly discuss each control statement to understand its functionality and workflow in the program.

Download Formulas for GATE Computer Science Engineering – Discrete Mathematics

Decision Control Statements in C

The decision control statements in C help jump from one part of the program code to another depending on whether the condition is satisfied. Having discussed the meaning and need of control statements in C, now let us discuss various decision control statements available in C for the GATE CSE syllabus. These decision control statements include:

  • The “if“ Statement
  • The “if-else” statement
  • The “if-else-if“ statement
  • Switch-case statement

The “if” Statement

The “if” statement is the simplest decision control statement frequently used to transfer the control flow from one part to the other part of the code. The if block may include one or n statements enclosed within curly brackets.

Example of a C program with an if statement:

#include< stdio.h>

int main()

{

int a=10;

if(a>0)

a++;

printf(“%d”, a);

return 0;

}

Output: 11

Download Formulas for GATE Computer Science Engineering – Algorithms

The “if-else” Statement

In the if-else construct, first, the test expression is evaluated. If the expression is true, then if the block is executed otherwise, the else block is executed. The if-else construct is different from that of the if statement. An example of an if-else block is as follows:

#include< stdio.h>

int main()

{

int a=10;

if(a%2==0)

printf(“%d”, a is even);

else

printf(“%d”, a is odd);

return 0;

}

Output: a is even

The “if-else-if” Statement

In the ”if-else-if” construct, first, the test expression is evaluated. If the expression is true, then if the block is executed otherwise, the else block is executed; it supports an additional condition apart from the initial test condition. The “if-else-if” construct works similarly to a normal if statement.

Example:

#include< stdio.h>

int main()

{

int number;

printf(“Enter any number”);

scanf(“%d”, &number);

if(number==0)

printf(“The value is equal to zero”);

else if(number>0)

printf(“The value is positive”);

else

printf(“The value is negative”);

return 0;

}

Switch Case

A switch case statement is a multi-way decision statement that is an alternative to the if-else-if construct. The switch statements are mostly used in two scenarios:

  • When there is only one variable to evaluate in the expression.
  • When many conditions are tested simultaneously.

The general form or syntax of a switch case is as follows:

switch(variable)

{

case value 1:

statement block 1;

break;

case value 2:

statement block 2;

break;

case value N:

statement block N;

break;

default:

statement block D;

break;

}

Statement X;

Iterative Statements in C

The iterative statements are used to repeat the execution of a particular sequence of instructions until the specified expression becomes false. The C language supports three types of iterative statements also known as looping statements. The iterative statements present in C are as follows:

  • The “for” loop
  • The “while” loop
  • The “do-while” loop

Jump Statements in C

To transfer the control from one block to another, we use jump statements; for transferring control, we have two types of jump statements: broken and continuous. The jump statements are also used in questions asked in the GATE exam. In C programming language, the break statement is used to terminate the execution of the nearest enclosing loop in which it appears.

The break statement is widely used in decision control statements and iterative statements. It is frequently used in switch cases and iterative statements like for, while, and do-while.

Important Topics for Gate Exam
2s Complement Acid Properties in Dbms
Angle of Contact Asynchronous Counter
Belt Drive Bending Moment
Bending Stress Boolean Theorems
Boundary Layer Bresenham Line Drawing Algorithm
Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium