Control flow statements enable us to specify the flow of program control; ie, the order in which the instructions in a program must be executed. They make it possible to make decisions, to perform tasks repeatedly or to jump from one section of code to another.
There are four types of control statements in C:
1. Decision-making statements:
It takes an expression in parenthesis and a statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.
a) If statement
Syntax:
if (expression)
statement; //single line
or
if (expression)
{
Body of statements; // multiple lines
}
Flowchart:
b) if-else statement
Syntax:
if (expression)
{
Body of statements;
}
else
{
Body of statements;
}
Flowchart:
c) else-if statements:
Syntax:
if (expression)
{
Body of statements;
}
else if(expression)
{
Body of statements;
}
:
:
else if(expression)
{
Body of statements;
}
else
{
Body of statements;
}
Flowchart:
? : Operator
The ? : operator is just like an if ... else statement except that because it is an operator you can use it within expressions.
? : is a ternary operator in that it takes three values, this is the only ternary operator C has.
? : takes the following form:
if condition is true ? then X return value : otherwise Y value;
No comments:
Post a Comment