unit-4 decision statement - wordpress.com · unit-4 decision statement. topics to be covered 4.1....
TRANSCRIPT
![Page 1: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/1.jpg)
UNIT-4
DECISION STATEMENT
![Page 2: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/2.jpg)
TOPICS TO BE COVERED
4.1. Develop programs using decision making
statements in ‘C’ language.
Unconditional branching: goto statement
Conditional branching statements: If statement
If-else statement
Nested If-else statement
If-else-if Ladder statement
break, continue and goto statements
switch statements
![Page 3: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/3.jpg)
INTRODUCTION
Decision making statement in C language are as
follows:
1) if statement
2) switch statement
3) conditional operator statement
4) goto statement
In C this statements are known as control
statements.
![Page 4: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/4.jpg)
IF STATEMENT
if statement is powerful decision making statement & used to control the flow of execution of statement.
It is a two-way decision making statement.
Syntax:
if( test expression)
It allows the computer to evaluate the expression first and then, depending on the value of the expression is ‘true’( non zero) or ‘false’( zero), it transfer the control to a particular statement.
![Page 5: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/5.jpg)
test
expression
?
Entry
False
TrueFlow of if
statement
![Page 6: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/6.jpg)
DIFFERENT FORM OF IF STATEMENTS:
1) Simple if statement
2) if…..else statement
3) Nested if….else statement
4) else if ladder
![Page 7: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/7.jpg)
SIMPLE IF STATEMENT
General Form:
If the test expression is true then the statement-block will
be executed; otherwise the statement-block will be
skipped and the execution will jump to the statement-x.
If( test expression )
{
statement-block;
}
Statement-x;
![Page 8: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/8.jpg)
FLOWCHART OF SIMPLE IF :
test
expression
?
Statement-block
Statement-x
Next statement
Entry
False
True
![Page 9: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/9.jpg)
EXAMPLE
If( a>b)
{
printf(“ a is greater than b ”);
}
![Page 10: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/10.jpg)
THE IF….ELSE STATEMENT
General Form :
If the test expression is
true then the true-clock
statement are executed
otherwise the false-
clock statement are
executed.
if( test expression)
{
true-block statement
}
else
{
false-block statement
}
![Page 11: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/11.jpg)
FLOWCHART OF IF….ELSE :
test
expression
?
true-block
statement
Statement-x
Entry
True
false-block
statement
False
![Page 12: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/12.jpg)
EXAMPLE
if(n%2==0)
{
printf(“ n is even ”);
}
else
{
printf(“ n is odd ”);
}
![Page 13: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/13.jpg)
NESTING OF IF…ELSE STATEMENT
Used when we need series of decisions.
General Form : if( test condition1)
{
if( test condition2)
{
statement1;
}
else
{
statement2;
}} else
{
statement3;
}
statement x;
![Page 14: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/14.jpg)
FLOWCHART OF NESTED IF ELSE STATEMENT
![Page 15: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/15.jpg)
If the condition1 is false then the statement3 is
executed, otherwise it continues to perform the
second test, if condition2 is true then the
statement1 will be executed; otherwise
statement2 will be executed and then the control
is transferred to statement x.
![Page 16: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/16.jpg)
EXAMPLE
If(a>b){
if(a>c){
printf(“ a is max ”);}else{
printf(“ c is max ”);}
}
else{
if(b>c){
printf(“ b is max ”);}else{
printf(“ c is max ”);}
}
![Page 17: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/17.jpg)
THE ELSE IF LADDER
Used when we need multipath decisions.
General Form:
if(condition1)
statement1;
else if(condition2)
statement2;
else if(condition3)
statement3;
else
default-statement;
statement-x;
![Page 18: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/18.jpg)
THE ELSE IF LADDER
![Page 19: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/19.jpg)
In this, the condition are evaluated from top to
downwards.
When all the n conditions are become false, then
the final else containing the default statement
will be executed.
![Page 20: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/20.jpg)
EXAMPLEif( marks>84 )
Grade=“AA”;
else if( marks>74 )
Grade=“AB”;
else if( marks>64 )
Grade=“BB”;
else if( marks>54 )
Grade=“BC”;
______________
______________
else
Grade=“Fail”;
![Page 21: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/21.jpg)
THE SWITCH STATEMENT
We uses an if statement to control the selection.
However, the complexity of such a program increases
when the number of alternatives increases.
And the program becomes more difficult to read and
follow.
The switch statement tests the value of a given variable
against a list of case values and when a match is found,
a block of statements associated with that case is
executed.
![Page 22: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/22.jpg)
FLOWCHART OF SWITCH CASE
![Page 23: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/23.jpg)
General Form
switch (expression)
{
case value1:
block-1;
break;
case value2:
block-2;
break;
___________________________
___________________________
default:
default-block;
break;
}
statement-x;
![Page 24: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/24.jpg)
Where, the expression is an integer expression or
characters.
Value1, value2….. Are constants or constant
expression and are known as case labels.
Case labels end with a colon(:)
When the switch is executed, the value of the
expression is successfully compared against the
values. If a case is found whose value matches with
the value of the expression, then the block of
statement that follows the case are executed.
![Page 25: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/25.jpg)
The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement transferring the control to the statement-x following the switch.
The default is an optional case. When present, it will be executed if the value of the expression does not match with any of the case values.
If not present, no action takes place if all matches fail and the control goes to the statement-x.
![Page 26: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/26.jpg)
EXAMPLE
switch( op)
{
case +:
ans = a + b;
break;
case -:
ans = a - b;
break;
default:
printf(“ wrong choice ”);
break;
}
printf(“ Answer is : %d”, ans);
![Page 27: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/27.jpg)
THE ? : OPERATOR
It is known as conditional operator and it takes three
operands.
General Form:
conditional expression ? exp1 : exp2
The conditional expression is evaluated first.
If the result is non-zero, exp1 is evaluated and is
returned as the value of the conditional expression.
Otherwise, exp2 is evaluated and its value is
returned.
Example: ans = (a>b) ? a : b ;
![Page 28: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/28.jpg)
Advantage:
When the conditional operator
is used, the code becomes more concise and
efficient.
Disadvantage:
Readability is poor.
![Page 29: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/29.jpg)
THE GOTO STATEMENT
C supports the goto statements to branch
unconditionally from one point to another in the
program.
The goto requires a label in order to identify the
place where the branch is to be made.
Label must be followed by colon(:).
The label is placed immediately before the statement
where the control is to be transferred.
![Page 30: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/30.jpg)
GENERAL FORM:
goto label;
_________
_________
label:
statement;
Forward Jump
label:
statement;
___________
___________
goto label;
Backward Jump
![Page 31: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/31.jpg)
The goto statement breaks the normal sequential
execution of the program.
Use:
1) Used at the end of a program to
direct the control to go to the input statement, to
read further data.
2) Used to transfer the control out of a
loop when certain peculiar conditions are
encountered.
![Page 32: UNIT-4 DECISION STATEMENT - WordPress.com · UNIT-4 DECISION STATEMENT. TOPICS TO BE COVERED 4.1. Develop programs using decision making statements in ‘C’ language. ... It is](https://reader033.vdocuments.site/reader033/viewer/2022052723/5f0d916a7e708231d43b0030/html5/thumbnails/32.jpg)
Thank you