Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

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.

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 Types Used in C Language


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

 If Statements
 Switch Statement
 Conditional Operator Statement
 Goto Statement
 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 if statement
 If… else
 Else if ladder
 Nested if

1.1 The Simple if statement

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);

1.2 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 (test expression)

True-block statement (s);

else

False-block statement (s);

Example 1: Write a program to check a number is even or odd.

Output:
Enter a number:1485
1485 is an odd number
Example 2: Write a program to find the greatest among two numbers.

Output:
Enter the first number:65
Enter the second number:79
79 is greatest

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);
}

Example 1: Write a program to print tha name of the day of the week accoriding to the input.
Output:
Enter a number between 1 to 7: 3
Wednessday

1.4 The Nested If Statement


When a series of decisions are involved, we may have to use more than one if…else
statement in nested form. 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.

if (test condition - 1)
{
if (test condition - 2)
{
Statement – 1;
}
else
{
Statement – 2;
}
}
else
{
Statement – 3;
}
Statement – 4;
Example: Write a program to fine the greatest among 3 numbers.

Output:
Enter three numbers:
45
78
74
78 is greatest

2. Switch Case
We have seen that when one of the many alternatives is to be selected, we can use an if
statement to control the selection. However, the complexity of such program increases
dramatically when the number of alternatives increases. The program becomes difficult to
read and follow. At times, it may confuse even the person who designed it. Fortunately, C has
a built-in multiway decision statement known as a switch.

The switch statement tests the value of a given variable against a list of case values and when
a match is found, a block of statements associated with that case is executed. The expressions
to be used in the switch should be integer expression or characters.
switch (expression)
{
case value – 1:
block – 1;
break;
case value – 2:
block – 2;
break;
……………..
……………..
default:
default – block;
break;
}

You might also like