Upload
ghanashyamkj
View
221
Download
0
Embed Size (px)
Citation preview
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 1/46
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 2/46
Decision Control Statements
Conditional control statements Unconditional control statement
Simple if statement
if-else statement
Nested if-else statement
switch statement
goto statement
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 3/46
F It is used to execute only one action.
F It is called one way branching.F Here the logical condition is tested which results either
TRUE or FALSE.
The syntax is:if (condition)
statement;
condition true
Execute this
Otherwise it just skip the
statement
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 4/46
Is
condition
?
statement
Next executable statement
True
False
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 5/46
main( )
{
int prize, dis;
printf(“Enter the prize of book \n”);
scanf(“%d”,&prize); if(prize>=500)
{
dis=10;
prize=prize-(prize*dis/100);}
printf(“Amount of book=%d”, prize);
getch( );
}
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 6/46
if(prize>=500)
{
dis=10;
prize=prize-(prize*dis/100);
} printf(“Amount of book=%d”, prize);
Suppose prize is 600
The condition is true
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 7/46
if(prize>=500)
{
dis=10;
prize=prize-(prize*dis/100);
} printf(“Amount of book=%d”, prize);
Suppose prize is 600 Execute this
dis=10
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 8/46
if(prize>=500)
{
dis=10;
prize=prize-(prize*dis/100);
} printf(“Amount of book=%d”, prize);
Suppose prize is 600 Execute this
prize=540
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 9/46
if(prize>=500)
{
dis=10;
prize=prize-(prize*dis/100);
} printf(“Amount of book=%d”, prize);
Suppose prize is 600 Execute this
Amount of book=540
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 10/46
Isprize>=500
?dis=10
prize=prize-(prize*dis/100)
print “Amount of book=prize”
True
False
Read prize
STOP
START
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 11/46
Isprize>=500
?dis=10
prize=prize-(prize*dis/100)
print “Amount of book=prize”
True
False
Read prize
STOP
START
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 12/46
Adding a semicolon at the end of an if statement is a common
mistake.if (prize>= 500);
{
dis=10;
prize=prize-(prize*dis/100);
}
printf(“Amount of book=%d”, prize);
This mistake is hard to find, because it is not a compilation error
or a runtime error, it is a logic error.
Wrong
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 13/46
The if statement does nothing when the expression evaluates to
false. So to execute another group of statements, we use if-elsestatement.
This is used to execute two statements alternatively.
It is called a two way branching.
The syntax is:
if (condition)statement 1;
else
statement 2;
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 14/46
The if statement does nothing when the expression evaluates to
false. So to execute another group of statements, we use if-elsestatement.
This is used to execute two statements alternatively.
It is called a two way branching.
The syntax is:
if (condition)statement 1;
else
statement 2;
condition true
Execute this
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 15/46
The if statement does nothing when the expression evaluates to
false. So to execute another group of statements, we use if-elsestatement.
This is used to execute two statements alternatively.
It is called a two way branching.
The syntax is:
if (condition)statement 1;
else
statement 2;
condition false
Execute this
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 16/46
Is
condition
?
Statement 1
Next executable statement
TrueFalse
Statement 2
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 17/46
main( )
{
int n;
printf(“Enter a number \n”);
scanf(“%d”,&n); if((n%2)==0)
printf(“The number is even”);
else
printf(“The number is odd”); getch( );
}
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 18/46
if((n%2)==0)
printf(“The number is even”);
else
printf(“The number is odd”);
Suppose n is 17 The condition is false
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 19/46
if((n%2)==0)
printf(“The number is even”);
else
printf(“The number is odd”);
Suppose n is 17 Execute this
The number is odd
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 20/46
Do not put assignment operator in the place of equal operator.
if((n%2)=0)
printf(“The number is even”);
else
printf(“The number is odd”);
Wrong
Condition is False
Hence the else condition is executed
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 21/46
If there are more than two alternative statements to be executed, then we use
nested if-else statement.
Here we use if-else statement within the other if-else statement.
The syntax is:
if (condition 1)
{
if(condition 2)
statement 1;
else
statement 2;
}
else
{
if(condition 3)
statement 3;
else
statement 4;
}statement x;
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 22/46
If there are more than two alternative statements to be executed, then we use
nested if-else statement.
Here we use if-else statement within the other if-else statement.
The syntax is:
if (condition 1)
{
if(condition 2)
statement 1;
else
statement 2;
}
else
{
if(condition 3)
statement 3;
else
statement 4;
}statement x;
If both condition1 and
condition 2 are true Execute this
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 23/46
If there are more than two alternative statements to be executed, then we use
nested if-else statement.
Here we use if-else statement within the other if-else statement.
The syntax is:
if (condition 1)
{
if(condition 2)
statement 1;
else
statement 2;
}
else
{
if(condition 3)
statement 3;
else
statement 4;
}statement x;
Condition1 true and
condition 2 is false Execute this
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 24/46
If there are more than two alternative statements to be executed, then we use
nested if-else statement.
Here we use if-else statement within the other if-else statement.
The syntax is:
if (condition 1)
{
if(condition 2)
statement 1;
else
statement 2;
}
else
{
if(condition 3)
statement 3;
else
statement 4;
}statement x;
Condition1 is falseExecute this
Condition 2 is true
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 25/46
If there are more than two alternative statements to be executed, then we use
nested if-else statement.
Here we use if-else statement within the other if-else statement.
The syntax is:
if (condition 1)
{
if(condition 2)
statement 1;
else
statement 2;
}
else
{
if(condition 3)
statement 3;
else
statement 4;
}statement x;
Condition1 is falseExecute this
Condition 2 is false
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 26/46
Is
condition2
?
statement x
True False
Statement 1
Is
condition1?
Is
condition3
?
Statement 2 Statement 3 Statement 4
True False FalseTrue
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 27/46
main( )
{ int n;
printf(“Enter a number \n”);
scanf(“%d”, &n);
if (n==0) printf(“Neither +ve nor –ve”);
else
{
if (n>0)
printf(“No. is +ve”);
else
printf(“No. is -ve”);
}
}
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 28/46
if (n==0)
printf(“Neither +ve nor –ve”);
else
{if (n>0)
printf(“No. is +ve”);
else
printf(“No. is -ve”);
}
Suppose n is -14The condition is false
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 29/46
if (n==0)
printf(“Neither +ve nor –ve”);
else
{if (n>0)
printf(“No. is +ve”);
else
printf(“No. is -ve”);
}
Suppose n is -14The condition is false
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 30/46
if (n==0)
printf(“Neither +ve nor –ve”);
else
{if (n>0)
printf(“No. is +ve”);
else
printf(“No. is -ve”);
}
Suppose n is -14The condition is false
Execute this
No. is -ve
Fl h t
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 31/46
Flow chart:
True FalseIs
n==0
?
Is
n>0
?
No. is +ve No. is -ve
FalseTrue
Read n
START
Neither +ve nor -ve
STOP
Flow chart:
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 32/46
Flow chart:
True FalseIs
n==0
?
Is
n>0
?
No. is +ve No. is -ve
FalseTrue
Read n
START
Neither +ve nor -ve
STOP
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 33/46
The control statement that allows us to make a decision from
the number of choices is called switch. The switch statement tests the value of expression against a list
of case values.
When the match is found, a block of statements associated with
that case is executed.
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 34/46
The keyword break is
used at the end of each
case to terminate fromthe switch statement.
If the break statement is
not present, the next case
statement will beexecuted.
When none of the
specified cases matchesthe switch-expression,
then the statement
following default are
executed.
switch (expression)
{
case value1: block 1;
break;
case value2: block 2;
break;…
case value N: block N;
break;
default: default-block;}
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 35/46
main( )
{ char op;
float a, b; printf(“Enter any two no. & the operator +, -, * or / \n”);
scanf(“%f%f%c”, &a, &b, &op);
switch(op){
case „+‟: printf(“%f+%f=%f”, a, b, a+b);
break;
case „-‟: printf(“%f -%f=%f”, a, b, a-b);
break;
case „*‟: printf(“%f*%f=%f ”, a, b, a*b);
break;case „/‟: printf(“%f / %f=%f”, a, b, a/b);
break;
default: printf(“INVALID OPERATOR”);
}
}
Example: program to preform any one of the arithmetic operation.
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 36/46
switch(op){
case „+‟: printf(“%f+%f=%f”, a, b, a+b);
break;
case „-‟: printf(“%f -%f=%f”, a, b, a-b);
break;case „*‟: printf(“%f*%f=%f ”, a, b, a*b);
break;
case „/‟: printf(“%f / %f=%f”, a, b, a/b);
break;default: printf(“INVALID OPERATOR”);
}
getch( );
Suppose operator is „*‟:
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 37/46
switch(op){
case „+': printf(“%f+%f=%f”, a, b, a+b);
break;
case „-‟: printf(“%f -%f=%f”, a, b, a-b);
break;case „*‟: printf(“%f*%f=%f ”, a, b, a*b);
break;
case „/‟: printf(“%f / %f=%f”, a, b, a/b);
break;default: printf(“INVALID OPERATOR”);
}
getch( );
operator is not „*‟:
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 38/46
switch(op){
case „+‟: printf(“%f+%f=%f”, a, b, a+b);
break;
case „-': printf(“%f -%f=%f”, a, b, a-b);
break;case „*‟: printf(“%f*%f=%f ”, a, b, a*b);
break;
case „/‟: printf(“%f / %f=%f”, a, b, a/b);
break;default: printf(“INVALID OPERATOR”);
}
getch( );
operator is not „*‟:
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 39/46
switch(op){
case „+‟: printf(“%f+%f=%f”, a, b, a+b);
break;
case „-‟: printf(“%f -%f=%f”, a, b, a-b);
break;case „*': printf(“%f*%f=%f ”, a, b, a*b);
break;
case „/‟: printf(“%f / %f=%f”, a, b, a/b);
break;default: printf(“INVALID OPERATOR”);
}
getch( );
operator is „*‟:
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 40/46
switch(op){
case „+‟: printf(“%f+%f=%f”, a, b, a+b);
break;
case „-‟: printf(“%f -%f=%f”, a, b, a-b);
break;case „*': printf(“%f*%f=%f ”, a, b, a*b);
break;
case „/‟: printf(“%f / %f=%f”, a, b, a/b);
break;default: printf(“INVALID OPERATOR”);
}
getch( );
Execute this line
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 41/46
switch(op){
case „+‟: printf(“%f+%f=%f”, a, b, a+b);
break;
case „-‟: printf(“%f -%f=%f”, a, b, a-b);
break;case „*': printf(“%f*%f=%f ”, a, b, a*b);
break;
case „/‟: printf(“%f / %f=%f”, a, b, a/b);
break;default: printf(“INVALID OPERATOR”);
}
getch( );
Execute this line
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 42/46
switch(op){
case „+‟: printf(“%f+%f=%f”, a, b, a+b);
break;
case „-‟: printf(“%f -%f=%f”, a, b, a-b);
break;case „*': printf(“%f*%f=%f ”, a, b, a*b);
break;
case „/‟: printf(“%f / %f=%f”, a, b, a/b);
break;default: printf(“INVALID OPERATOR”);
}
getch( );
Execute this line
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 43/46
The goto statement is used to take control where we want,unconditionally from one point to another in the program.
The goto requires a label in order to identify the place where the
control is to be made.
A label is any valid variable name, and must be followed by a
colon( : ).
The label is placed immediately before the statements where the
control is to be transferred.
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 44/46
goto label;
- - - - -
- - - - -- - - - -
label:
statement;
label:
Statement;
- - - - -- - - - -
- - - - -
goto label;
Forward jump Backward jump
If the label: is placed after the goto label; some statements will be
skipped and jump is known as forward jump.
If the label: is placed before the goto label; a loop will be formed and
some statements will be executed repeatedly. Such a jump is known as
a backward jump.
Example: program to calculate factorial of a non-
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 45/46
p p g
negative number
main( )
{ int n, fact=1, i=1;start:
clrscr( );
printf(“Enter a non-negative number”);
scanf(“%d”, &n); if(n<0) goto start;
find:
fact=fact*i;
i++;if(i<=n) goto find;
printf(“Factorial of %d=%d”, n , fact);
getch( );
}
Do not dis-
order
because ittakes
infinite loop
So avoid
goto
statement
as far as
possible
8/12/2019 Decision Control Statements
http://slidepdf.com/reader/full/decision-control-statements 46/46