DECISION CONTROL STATEMENTS
1. if Statement
2. if...else Statement
3. elseif Ladder
4. switch Statement
5. Conditional Operator
1. if Statement
2. if...else Statement
3. elseif Ladder
4. switch Statement
5. Conditional Operator
1.
if
Statement - The if statement is a powerful decision making statement and is
used to control the flow of execution of statements. The syntax and flowchart
is shown below:
Syntax:
if
(test expression)
{
statement-block;
}
statement x;
·
The statement block may be a single statement
or a group of statements.
·
The test expression may be a relational
expression or logical expression.
·
If the test expression is true (i.e. it
returns a nonzero value or 1) the statement-block will be executed, otherwise
the statement block be skipped and execution will jump to the statement-x.
·
If condition is true both statement-block and
statement-x are executed in sequence.
·
If there are only one statement in statement
block then braces are not necessary otherwise these are necessary.
·
The indentation does not affect the logic of a
program; it only enhance to increase the clarity of the program.
·
There can be any valid C statement inside if.
It can be another if statement. It is called nesting of if.
2. if … else Statement – With the
if statement we can execute a group of statement when the condition is true but
if condition is false we cannot execute a group of statements. It is possible
with if… else statement. The syntax and flowchart is shown below:
Syntax:
if
(test expression)
{
True block statement(s);
}
else
{
False block statements(s);
}
Statement-x;
·
If the test expression is true then true-block
stmt. will be executed, otherwise, false-block statement will be executed.
·
In either case, either true block or false
block will be executed, not both.
·
Nesting of
if-else statement – It is perfectly alright if we write an entire
if – else construct withing, either the bosy of if statement or the body
of else statement. This is called
‘nesting’ of ifs.
3.
The else if Ladder - There is another way of putting ifs
together when multipath decision are involved. A multipath decision is a chain
of ifs in which the statement associated with each else is an if. It takes the
following general form:
if (condition 1)
{
statement block 1
}
elseif (condition 2)
{
statement block 2
}
elseif (condition n)
{
statement n;
}
else
default-statement
statement-x
This construct is known as the else if ladder.
The conditions are evaluated from the top (of the Ladder), downwards. As soon
as a true condition is found, the statements associated with it are executed
and the control is transferred to the statement-x (skipping the rest of the
ladder).
When all the n conditions become false, then
the final else containing the default-statement will be executed.
Example:
………..
If (code ==1)
colour=”RED”;
else if (code==2)
colour=”WHITE”;
else if (code==3)
colour=”YELLOW”;
else
colour=”GREEN”;
……….
4. switch Statement –
The control statement which allows us to make a decision ffrom the
number of choice is called a switch, or more correctly a switch-case-default,
since these three keywords to together to make up the control statement. They
most often appear as follows:
Syntax:
switch
(integer expression)
{
case 1:
statements
1;
break;
case 2:
statements2;
break;
………….
…………
default:
default-statement;
}
·
The integer expression following the keyword
switch is any C expression that will yield an integer value. It could be and
integer constant like 1, 2, 3 … or character constant ‘a’, ‘y’, ‘N’ or an
expression that evaluate to an integer. Float value is not allowed in switch
statement.
·
Which case statement will be satisfied by the
value of switch that will be executed and after the execution the break
statement exists the loop from the switch.
·
switch executes
the case where the match is found and all the subsequent cases and the default
as well.
·
There is no need of the default statement.
·
It is not necessary that we put the case
statement in ascending order. We can kept them in any order.
·
We are also allowed to use char value in case
and switch statements.
·
We can mix integer and character constant in
different case of a switch.
·
Sometimes there may be not be any statement in
some of the case in switch, but still they might turn out to be useful. This is
shown below example.
·
Even if there are multiple statements to be
executed in each case, there is no need to enclose those statements within a
pair of braces.
·
If we have no default case, then the program
simply falls through the entire switch and continues with the next instruction
(if any) that follows the control structure.
·
switch is the
replacement for if because if after a better way of writing program as compare
to if. There is no confusing more indentation as if in the switch.
·
There is one disadvantage that we cannot use
any condition in case e.g. case I <= 20:
·
The break statement when used in a switch,
takes the control outside the switch. However, use of continue will not take
the control to the beginning of switch.
·
We can nest the switch statement.
·
If here is no break statement then all the
statements given below of satisfied case will be executed simultaneously
5.
Conditional operator ( … ? ... : … )
Conditional operator takes three operands and consists of two
symbols ? and : . Conditional operators are used for decision making in C. For
example:
c=(c>0)?10:-10;
If c is
greater than 0, value of c will be
10 but, if c is less
than 0, value of c will be
-10.
No comments:
Post a Comment