Control Statements in C

The control statements used in the C language help a user to specify a program control’s flow. In simpler words, the control statements help users specify the order of execution of the instructions present in a program. These make it possible for the program to make certain decisions, perform various tasks repeatedly, or even jump from any one section of the code to a different section.

In this article, we will take a closer look at the Control Statements in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents:

Why Do We Use Control Statements in C?

In C language, the control of the program flows from a given instruction to another. This type of control flow that occurs from any given command to another is known as the sequential control flow. Now, in any C program, a programmer might want to repeat some sets of instructions or even skip the instructions when they are writing logic. Declarations in C, also known as control declarations or decision-making, help them in making such decisions.

Conditional statements are used in the C programming language for making certain decisions on the basis of the available conditions. These conditional statements get executed sequentially in case no condition is present around the statements. Whenever we put a condition for the block statements, the flow of execution may get altered on the basis of the result that is evaluated by the condition in the program. The process mentioned here is known as decision-making in the C language.

Control Statements In C Related Videos

Control Statements Types Used in C Language

The C language provides support for the following set of statements in its program:

  1. If Statements
  2. Switch Statement
  3. Conditional Operator Statement
  4. Goto Statement
  5. Loop Statements

1. The If Statements

This type of statement would enable a programmer to choose various instruction sets on the basis of the available condition. The instruction sets will only get executed when the evaluation of the condition turns out to be true. In case the evaluation of the condition is false, there will be an execution of a different instruction set. These are also known as decision control statements. These are of the following types:

  • Simple else or Null else
  • Else if ladder
  • Nested if
  • If… else

1.1. The If… Else Statement

When we use the if… else statement, there occurs an execution of two different types of statements in a program. First, if the available condition in the program is true, then there will be an execution of the first statement. The execution of the second condition will only occur if the condition available to us is false.

The syntax for this statement is as follows:

If (condition 1)

{

Statement 1 (s1);

}

else

{

Statement 2 (s2)

}

Statement

Example:

height=int(input(“Please enter your height: “))

if height>=160:

qualified=True

else:

qualified=False

print(“Qualification status: “,qualification)

1.2. The Nested If Statement

In this case, the condition available in the next if statement (the second statement) will only get evaluated if the evaluation of the condition available in the first statement turns out to be true. This occurs throughout the program that has a nested statement.

The syntax for this statement is as follows:

If (condition 1)

{

If (condition 2)

{

Statement 1 (s1);

}

Else

{

Statement 2 (s2)

}

}

Example:

if age>0:

print(“The candidate is a baby”)

if age<4:

print(“The candidate is a toddler”)

else if age<18:

print(“The candidate is not an adult”)

else if age<50:

print(“The candidate is an adult”)

else:

print(“No input of candidate”)

1.3. The Else If Ladder

In this statement, the execution of an array of instructions occurs only when the available condition is correct. The verification of the next condition occurs when this first condition is incorrect. In case all of the specifications fail even after the verification, then there will be an execution of the default block statements. The remainder of the program’s ladder is shown below.

The syntax for this statement is as follows:

If (condition 1)

{

Statement 1 (s1);

}

Else if (condition 2)

{

Statement 2 (s2);

}

else if (condition 3)

{

Statement 3 (s3)

}

Else

{

Statement 4 (s4)

}

Statement (s);

Example:

if scores>=85:

result=’A+’

else if scores>=65:

result=’B+’

else if scores>=45:

result=’C+’

else:

result=”FAIL”

print(“Result: “,result)

1.4. The Simple Else or Null Else

This condition occurs when a programmer can skip or execute a set of various instructions on the basis of the condition value. We select a one-way, simple statement. When the available condition gets evaluated as true, then a set of various statements will be carried out. In case the condition is false, then the control here will proceed ahead in the program with the declaration mentioned below, after the program’s if declaration.

The syntax for this statement is as follows:

If (condition1)

{

Statement 1 (s1);

}

Statement 2 (s2);

2. The Switch Statements

The C language offers its users with a selection statement in various ways in case a program becomes difficult to read with an increased number of conditions. A switch statement is a multi-way type of selection statement that would resolve this issue. The switch declaration comes into play when more than three alternatives (conditions) exist in a program. This command then switches between all the available blocks on the basis of the expression value. Then, each block has a corresponding value with it.

The syntax for this statement is as follows:

Switch (expression_A)

{

Label case_A:

Statement A (sA);

Break;

Label case_B:

Statement B (sB);

Break;

Label case_C;

Statement C (sC);

Break;

….

Label case_Z:

Statement Z (sZ);

Break;

Default:

Statement_1 (s1);

Break;

}

Every block is shown here with the use of the case keyword. As a matter of fact, the case keyword is also followed by the block label. Note that the break statement and default block statement are very optional in the case of the switch statement.

3. The Conditional Operator Statements

The C language also comes with a very unusual operator for its programmers – the conditional operator.

The syntax of the conditional operator statements is as follows:

(condition 1)? expression_1: expression_2

Here, the execution of the expression_1 will only occur when the given condition is valid. In case this statement is incorrect, then the execution of the expression_2 will occur.

Example:

#include <stdio.h>

int main() {

int b;

int a = 2;

b = (a >= 6) ? 6 : a;/* Here, it is equivalent to: if (a >= 5) b = 5; else b = x; */

printf(“b =%d “,b);

return 0;}

The output obtained here would be:

b = 2

4. The Goto Statement

The Goto statement is especially known in the case of jumping control statements. We mainly use the goto statement when we want to transfer a program’s control from any one block to another. Also, we use the goto keyword for the declaration of the goto statement.

The syntax of the goto statement is as follows:

goto name_of_label;

name_of_label;

In the syntax given above, we have used the goto as a keyword for transferring the control of the program to the name_of_label. Here, the name_of_label refers to a variable’s name. Thus, in simpler words, the goto here will ultimately transfer the program’s control to the name_of_label. Thus, there will occur an execution of all those statements that are followed by the name_of_label.

5. The Loop Statements

A programmer in C might want to repeat any set of instructions or certain statements in the program to meet the necessary requirements. In such instances, it becomes difficult to rewrite and repeat everything. And that is exactly where we would like to create loops using the looping declarations. Loop control statements help in such types of situations in C. We have the following types of loops in C:

  • Do While Loop
  • While Loop
  • For Loop

Practice Problems on Control Statements in C

1. What would be the output obtained out of the program mentioned below:

#include<stdio.h>

int main()

{

int var1=1;

int var2=2;

if(var1<var2)

{

printf(“The value of var1 is smaller than that of the value of var2”);

}

return 0;

}

A. The value of var2 is smaller than that of the value of var1

B. The value of var1 is smaller than that of the value of var2

C. Compile time error will be obtained as a result

D. Garbage value will be obtained as a result

Answer – B. The value of var1 is smaller than that of the value of var2

2. What would be the output obtained out of the program mentioned below?

int a = 50;

a =a+ 1;

if (a == 51) {

printf(“Congratulations on your success today!”);}

A. a= 51>50

Congratulations on your success today!

B. a= 50<51

Congratulations on your success today!

C. Congratulations on your success today!

D. Compile time error will be obtained as a result

Answer – C. Congratulations on your success today!

3. What would be the output obtained out of the program mentioned below?

#include<stdio.h>

int main()

{

int val=109;

if(val<100)

{

printf(“The available value of the variable is less than 100”);

}

else

{

printf(“The available value of the variable is greater than 100”);

}

return 0;

}

A. The available value of the variable is less than 100

B. The available value of the variable is greater than 100

C. Garbage value will be obtained as a result

D. Compile time error will be obtained as a result

Answer – A. The available value of the variable is less than 100


Frequently Asked Questions

When do we use the nested if statement?

We use a nested if statement when we want the condition available in the next if statement (the second statement) to get evaluated, only if the evaluation of the condition available in the first statement turns out to be true. This occurs throughout the program that has a nested statement.
The syntax for this statement is as follows:
If (condition 1)
{
If (condition 2)
{
Statement 1 (s1);
}
Else
{
Statement 2 (s2)
}
}
Example:
if age>0:
print(“The candidate is a baby”)
if age<4:
print(“The candidate is a toddler”)
else if age<18:
print(“The candidate is not an adult”)
else if age<50:
print(“The candidate is an adult”)
else:
print(“No input of candidate”)

Why do we need the else-if ladder when we can perform simple decision making in the C language?

When we use the else if statement, the execution of an array of instructions occurs only when the available condition is correct. The verification of the next condition occurs when this first condition is incorrect. In case all of the specifications fail even after the verification, then there will be an execution of the default block statements. The remainder of the program’s ladder is shown below.
The syntax for this statement is as follows:
If (condition 1)
{
Statement 1 (s1);
}
Else if (condition 2)
{
Statement 2 (s2);
}
else if (condition 3)
{
Statement 3 (s3)
}

Else
{
Statement 4 (s4)
}
Statement (s);
Example:
if scores>=85:
result=’A+’
else if scores>=65:
result=’B+’
else if scores>=45:
result=’C+’
else:
result=”FAIL”
print(“Result: “,result)

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Leave a Comment

Your Mobile number and Email id will not be published.

*

*