Professional Documents
Culture Documents
Control Statements in C
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.
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
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.
If (condition1)
Statement 1 (s1);
Statement 2 (s2);
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)
else
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
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
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;
}