‘C’
language processes decision making capabilities supports the flowing statements
known as control or decision making statements
1. If statement
2. switch statement
3. conditional operator
statement
4. Goto statement
If Statement : The if statement is powerful decision making
statement and is used to control the flow of execution of statements The If
statement may be complexity of conditions to be tested
(a) Simple if statement
(b) If else statement
(c) Nested If-else statement
(d) Else –If ladder
Simple If Statement : The general form of simple if statement is
If(test
expression)
{ statement block;
} statement-x ;
The statement -block may be a single statement or a group
of statement if the test expression is true the statement block will be
executed. Otherwise the statement -block will be skipped and the execution will jump to the statement –X. If the condition
is true both the statement –block sequence .
Flow chart :
Ex : If(category = sports)
{ marks = marks + bonus marks;
} printf(“%d”,marks);
If the student belongs to the sports
category then additional bonus marks are added to his marks before they
are printed. For other bonus marks are not added .
If –Else Statement : The If statement is an extension of the simple If
statement the general form is
If
(test expression)
{
true-block statements;
}
else
{
false-block statements;
}
statement – x;
If the test expression is
true then block statement are executed, otherwise the false –block statement
are executed. In both cases either true-block or false-block will be executed
not both.
Flow chart :
Ex : If (code == 1)
boy = boy + 1;
else
girl
= girl + 1;
st-x;
Here if the code is equal to
‘1’ the statement boy=boy+1; Is
executed and the control is transfered to the statement st-n, after skipping the else part. If code is not equal
to ‘1’ the statement boy =boy+1; is
skipped and the statement in the else part girl
=girl+1; is executed before the control reaches the statement st-n.
Nested If –else statement : When a series of decisions are involved we
may have to use more than one if-else statement
in nested form of follows .
If(test
expression)
{ if(test expression)
{ st –1;
}
else
{ st – 2;
}else
{
st – 3;
}
}st – x;
If the condition is false
the st-3 will be executed otherwise it continues to perform the nested If –else
structure (inner part ). If the
condition 2 is true the st-1 will be executed otherwise the st-2 will be
evaluated and then the control is transferred to the st-x
Some other forms of nesting If-else
If ( test condition1)
{ if (test condition2)
st –1 ;
} else
if (condition 3)
{ if (condition 4)
st – 2;
}st – x;
Else-If ladder : A multi path
decision is charm of its in which the statement associated with each else is an
If. It takes the following general form.
If
(condition1)
St –1;
Else If (condition2)
St –2;
Else if (condition 3)
St –3;
Else
Default – st;
St –x;
This construct is known as
the wise-If ladder. The conditions are evaluated from the top of the ladder to
down wards. As soon as a true condition
is found the statement associated with it is executed and the control the is transferred
to the st-X (i.e.., skipping the rest of the ladder). when all the n-conditions
become false then the final else containing the default – st will be executed.
Ex : If (code = = 1) Color
= “red”;
Else if ( code = = 2) Color = “green”
Else if (code = = 3) Color = “white”;
Else Color
= “yellow”;
If code number is other than
1,2 and then color is yellow.
Switch Statement : Instead of else –if ladder, ‘C’ has a built-in
multi-way decision statement known as a switch. The general form of the switch
statement is as follows.
Switch
(expression)
{
case value1 : block1;
break;
case value 2 : block 2;
break;
default
: default block;
break;
}
st – x;
The expression is an integer
expression or character value1, value-2---- are constants or constant
expressions and also known as case lables. Each
of the values should be a unit within a switch and may contain zero or more statements.
When the switch is executed the value of the
expression is successively compared against the values value-1,value-2-------
If a case is found whose value matches
with the of the expression then the block of statements that follows the case
are executed .
The break statement at the end of each block signals
the end a particular case and causes an exist from the switch statement
transfering the control to the st-x
following the switch. The default is an optional case . If will be executed if
the value of the expression doesn’t match with any Of the case values then
control goes to the St-x.
Ex : switch (number)
{
case 1 : printf(“Monday”);
break;
case 2 : printf(“Tuesday”);
break;
case 3 :
printf(“Wednesday”);
break;
case 4 : printf(“Thursday”);
break;
case 5 : printf(“Friday”);
break;
default : printf(“Saturday”);
break;
}
The Conditional ( ? : )
Operator : These operator is a combinations of question
and colon and takes three operands this is also known as conditional operator.
The general form of the conditional operator is as follows
Conditional
expression? Expression 1:expression2
The conditional expression
is evaluated first If the result is non-zero expression is evaluated and is
returns as the value of the conditional expression, Otherwise expression2 is
evaluated and its value is returned.
Ex : flag = (
x<0) ? 0 : 1
It’s
equalent of the If-else structure is as follows
If
( x<0)
Flag = 0;
Else
Flag
= 1;
Goto Statement : The goto
statement is used to transfer the control of the program from one point to
another. It is something reffered to as unconditionally branching. The goto is
used in the form
Goto label;
Label statement : The
label is a valid ‘C’ identifier followed by a colon. we can precode any
statement by a label in the form
Label : statement ;
This statement
immediately transfers execution to the statement labeled with the label
identifier.
Ex : i = 1;
bc
: if(1>5) Output : 1
goto
ab; 2
printf(“%d”,i); 3
++i; 4
goto
bc; 5
ab
: {
printf(“%d”,i);
}
thanks dude
ReplyDeleteThank you it was useful
ReplyDeletethanks it was very useful
ReplyDelete