2. Flow of Control
Unless specified , the order of statement execution
through a C program is linear: one statement after
the other, in sequence.
Some programming statements modify that
order, allowing us to:
decide whether or not to execute a particular statement, or
perform a statement over and over, repetitively
3. 3
Flow of Control
These decisions are based on a boolean Or logical
expression (also called a condition) that evaluates to
true or false
The order of statement execution is called the flow of
control
7. Logical Expression
Logical expression is an expression which uses one
or more logical operators, e.g.,
(temperature > 90.0 && humidity > 0.90)
!(n <= 0 || n >= 100).
The output of the logical expression is the boolean
value either true or false.
8. If Statements
If statements consists of boolean expression followed
by one or more statements.
If the boolean expression evaluates to true, the
statements inside the block get executed otherwise
the first statement outside the block get executed.
The false value is o and all the other values are
evaluated as true in C.
17. If…elseif…else Statement
If statement can be followed by optional elseif..else
statement, which is very useful to test various
conditions using single if…elseif statement.
Following things should be kept in mind
An if can have zero or one else's and it must come after any
else if's.
An if can have zero to many else if's and they must come
before the else.
Once an else if succeeds, none of the remaining else if's or
else's will be tested.
19. If…elseif…else Statement(Example)
#include <stdio.h>
#include<conio.h>
int main ()
{
int a = 100;
if( a == 10 )
{
printf("Value of a is 10n" );
}
else if( a == 20 )
{
printf("Value of a is 20n" );
}
else if( a == 30 )
{
printf("Value of a is 30n" );
}
else
{
printf("None of the values is
matchingn" );
}
printf("Exact value of a is:
%dn", a );
getch();
return 0;
}
21. Nested if Statements
It is always legal in C programming to nest if-else
statements, which means we can use one if or else if
statement inside another if or else if statement(s).
22. Nested if Statements
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 100;
int b = 200;
if( a == 100 )
{
if( b == 200 )
{
printf("Value of a is 100
and b is 200n" );
}
}
printf("Exact value of a is :
%dn", a );
printf("Exact value of b is :
%dn", b );
getch();
return 0;
}
24. Switch Statement
A switch statement allows a variable to be tested for
equality against a list of values.
Each value is called a case, and the variable being
switched on is checked for each switch case.
The following rules apply to a switch statement:
The expression used in a switch statement must have an integral or
enumerated type, or be of a class type in which the class has a single
conversion function to an integral or enumerated type.
You can have any number of case statements within a switch. Each
case is followed by the value to be compared to and a colon.
The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
25. Switch Statement
When the variable being switched on is equal to a case, the
statements following that case will execute until a break
statement is reached.
When a break statement is reached, the switch terminates, and
the flow of control jumps to the next line following the switch
statement.
Not every case needs to contain a break. If no break
appears, the flow of control will fall through to subsequent
cases until a break is reached.
A switch statement can have an optional default case, which
must appear at the end of the switch. The default case can be
used for performing a task when none of the cases is true. No
break is needed in the default case.
30. Nested Switch Statements
It is possible to have a switch as part of the statement
sequence of an outer switch.
Even if the case constants of the inner and outer
switch contain common values, no conflicts will
arise.
32. Nested Switch Statements
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 100;
int b = 200;
switch(a)
{
case 100:
printf("This is part of outer
switchn", a );
switch(b)
{
case 200:
printf("This is part of inner
switchn", a );
}
}
printf("Exact value of a is : %dn", a
);
printf("Exact value of b is : %dn", b
);
getch();
return 0;
}