Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Control Statement

• C language has decision making capabilities and supports controlling of statements.


• C supports following control or decision making statements:

1. IF

2. Switch

3. Break

4. Continue

5. Goto

If statement

• The keyword if tells the compiler that what follows is a decision control instruction.

• The condition following the keyword if is always enclosed within a pair of parentheses.

• If the condition, whatever it is, is true, then the statement is executed. If the condition is not
true then the statement is not executed;
• if statement is a powerful decision making statement.

• if statement is used to control the flow of execution of statements.

Different forms of If statement

1. Simple If statement.

2. If…..else statement.

3. Nested if…..else statement.

4. Else if ladder or else if …else .

Simple if statement

 If the condition, whatever it is, is true, then the statement is executed. If the condition is
not true then the statement is not executed;
 The general form of a simple if statement is :
Syntax => if (Condition)

{
True statement 1;
True statement 2;
...
}
If….Else statement

 IF the if expression evaluates to true, the block following the if statement or


statements are executed.
 The else statement is optional. It is used only if a statement or sequences of
statements are to be executed in case, the if expression evaluates to false.
 The group of statements after the if upto and not including the else is called an ‘if block’.
Similarly, the statements after the else form the ‘else block’.
 Notice that the else is written exactly below the if.
Syntax =>

if(Condition)
{
True statement…1
True statement…2

}
else
{
False statement 1
False statement 1

}

Else if ladder

• The if – else – if statement is also known as the if-else-if ladder or the if-else-
if staircase.
• The conditions are evaluated from the top downwards.
Syntax:-
If(condition)
{
True statement…1
True statement…2

}
Else if(condition)
{
True statement…1
True statement…2

}
else
{
False statement 1
False statement 1

}
Nested if…else statement:

• It is perfectly all right if we write an entire if-else construct within either the body of the if
statement or the body of an else statement. This is called ‘nesting ‘of ifs.
• The general form of nested if…else statement is:

Syntax:
if (condition 1)
{
if (condition 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
statement 3;
}

EX:- In a company an employee is paid as under: If his basic salary is less than Rs. 1500, then HRA
= 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs.
1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input through
the keyboard write a program to find his gross salary.
/* Calculation of gross salary */
main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}
Switch – case statement

 The control statement that allows us to make a decision from the number of choices is called
a switch
 The switch-case control statement is a multi-way decision maker that tests the value
of an expression against a list of integers or character constants.
 When a match is found, the statements associated with that constant are executed.
 a switch-case-default, since these three keywords go together to make up the control
statement. They most often appear as follows:

switch ( integer expression )


{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}

First, the integer expression following the keyword switch is evaluated. The value it gives is then
matched, one by one, against the constant values that follow the case statements. When a match is
found, the program executes the statements following that case, and all subsequent case and
default statements as well. If no match is found with any of the case statements, only the
statements following the default are executed. A few examples will show how this control structure
works.

Ex:=>

main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in case 1 \n" ) ;
case 2 :
printf ( "I am in case 2 \n" ) ;
case 3 :
printf ( "I am in case 3 \n" ) ;
default :
printf ( "I am in default \n" ) ;
}
}
The output of this program would be:
I am in case 2
I am in case 3
I am in default
Ex->2
main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in case 1 \n" ) ;
break ;
case 2 :
printf ( "I am in case 2 \n" ) ;
break ;
case 3 :
printf ( "I am in case 3 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
}
The output of this program would be:
I am in case 2

Break statement

• We often come across situations where we want to jump out of a loop instantly,
without waiting to get back to the conditional test. The keyword break allows us to do
this.
• When the keyword break is encountered inside any c loop, control automatically
passes to the first statement after the loop.

• A break is usually associated with an if.

• The general syntax of break statement is:

For(; ;)

{ --------

--------

if (condition)

break;

-------

--------
The continue Statement
In some programming situations we want to take the control to the beginning of the loop, bypassing
the statements inside the loop, which have not yet been executed. The keyword continue allows us to
do this. When continue is encountered inside any loop, control automatically passes to the beginning
of the loop.
A continue is usually associated with an if. As an example, let's consider the following program.
main( )
{
int i, j ;
for ( i = 1 ; i <= 2 ; i++ )
{
for ( j = 1 ; j <= 2 ; j++ )
{
if ( i == j )
{
continue ;
}
printf ( "\n%d %d\n", i, j ) ;
}
}
}
The output of the above program would be...
12
21

Go to statement

 C supports the go to statement to branch unconditionally from one point to another


in the program.
 It requires a label in order to identify the place where branch is to be made.
 The general syntax of go to statement is:

goto label;
Statement 1;
Statement 2;
--------

label:

You might also like