5. C_ControlStatements_Selection_1101
5. C_ControlStatements_Selection_1101
5. C_ControlStatements_Selection_1101
Selection OR
Many time
Repetition
Control Statements
Let see a C program
Control Statements
Let see a C program
Sequential statements
Selection statements
Loop statements
Control Statements
Control the flow of execution in a program or function.
There are three kinds of execution flow:
If it does not - the statement is bypassed and the line of code following
the if is executed.
if Statements
The expression may be any valid C expression.
If it does not - the statement is bypassed and the line of code following
the if is executed.
if Statements
In C, an expression is true if it evaluates to any nonzero values
(5,9,-4,100 etc).
If it evaluate to zero, it is false.
int a=10, b=20; Output
if(a<b)
printf(“This line will print.”); This line will print.
if(expression)
statement1;
else
statement2;
If-else Statements
Structure of simple if-else is:
True
if(expression)
{
statements
}
else OR
{
statements
}
If-else Statements
Structure of simple if-else is:
False
if(expression)
{
statements
}
else OR
{
statements
}
If-else Statements
Structure of nested if-else is:
if(expression)
{
statements
}
else if(expression) OR
{
statements
}
...
else OR
{
statements
}
Switch Statements
If is good for choosing between two alternatives
When several alternatives are needed we should use switch
statement.
switch is C’s multiple selection statement.
Use to select one of several alternative paths in program
execution
Switch Statements
switch
A value is successively tested
against a list of integer or
character constants.
Web:
1. www.wikbooks.org
and other slide, books and web search.