Chapter 2 - The Decision Control Structure
-
Upload
waleed-liaqat -
Category
Documents
-
view
220 -
download
0
Transcript of Chapter 2 - The Decision Control Structure
-
8/2/2019 Chapter 2 - The Decision Control Structure
1/43
FUNDAMENTALSOF COMPUTER
PROGRAMMINGInstructor
Mr. Asad Ali Shah
-
8/2/2019 Chapter 2 - The Decision Control Structure
2/43
CHAPTER 2:
THE DECISION CONTROL STRUCTUREInstructor
Mr. Asad Ali Shah
-
8/2/2019 Chapter 2 - The Decision Control Structure
3/43
DECISIONS! DECISIONS!
Instructions were executed sequentially so far
Structures other than sequential structure
Decision Control Structure
You may want to execute an instruction in one situationand a different instruction for another situation
E.g. A/B where B should not be 0
In short, only divide the two variables if B is not 0
-
8/2/2019 Chapter 2 - The Decision Control Structure
4/43
CONTROL STRUCTURES
Control structure enable us to specify the order inwhich the various instructions in a program are tobe executed.
In short, they determine the flow of control
The three types of control structures are givenbelow
Sequence Control Instruction
Instructions are executed in order
Decision/Selection Control Instruction
Instructions execute if they meet the conditions
Repetition/Loop Control Instruction
The execution of instructions are repeated
-
8/2/2019 Chapter 2 - The Decision Control Structure
5/43
DECISION CONTROL STRUCTURE
Decide whether the instruction(s) should beexecuted or not.
A decision control instruction can be implementedin C using
The if statement
The if-else statement
The conditional operators
-
8/2/2019 Chapter 2 - The Decision Control Structure
6/43
DECISION CONTROL STRUCTURE
The different variations of Decision ControlStructure are given below
If Statement
If-else statement
Nested if-else statements
Else if statements
Case control
-
8/2/2019 Chapter 2 - The Decision Control Structure
7/43
THE IF STATEMENT
if keyword used for decision control instruction
if(this condition is true)
execute this statement;
If the condition within the parenthesis is true thenthe statement is executed
Otherwise, the compiler will skip past it
This is all good but how do we write thoseconditions anyway?
-
8/2/2019 Chapter 2 - The Decision Control Structure
8/43
CONDITIONAL EXPRESSIONS
The conditional expression are used with if
They are constructed using relational operators
Relational Operators allow us to compare twovalues.
This Expression Is true if
x==y x is equal to y
x!=y x is not equal to yxy x is greater than y
x=y x is greater than and equal to y
-
8/2/2019 Chapter 2 - The Decision Control Structure
9/43
EXERCISE
What are the results of the following relationalexpressions
9 < 25
9 < 3
9 > 14
9 = 25
9 == 13
9 != 13
9 !< 25
-
8/2/2019 Chapter 2 - The Decision Control Structure
10/43
SOLUTION
true
false
false
true false
false
true
Error, the "not less than" is not a valid operator.
-
8/2/2019 Chapter 2 - The Decision Control Structure
11/43
CAUTION!!!!!!!!!!!
X=Y IS NOT EQUIVALENT TO X==Y
Int x=3, y=3, z=4;
(1)X=Y assigns Ys value to X
(2)X==Y compares the two values
(3)X==Z compares the two values
The == comparison returns either true or false
Output of (1) is 3
Output of (2) is 1
Output of (3) is 0
-
8/2/2019 Chapter 2 - The Decision Control Structure
12/43
SIMPLE PROGRAMUSING IF STATEMENT
#include
void main(void)
{
int num;
printf(Enter a number less than 10);
scanf(%d,&num);
if(num
-
8/2/2019 Chapter 2 - The Decision Control Structure
13/43
WHATISHAPPENINGHERE?
#include
void main(void)
{
int num;
printf(Enter a number equal to10);
scanf(%d,&num);
if(num=10)
printf(What a obedient servant you are);
}
-
8/2/2019 Chapter 2 - The Decision Control Structure
14/43
THE ANSWERTO X=10 STATEMENT
The if conditions will be satisfied everytime.
All assignment operations like X=Y areconsidered as true.
All values other than zero areconsidered to be true
-
8/2/2019 Chapter 2 - The Decision Control Structure
15/43
FLOW CHARTS
A graphic representation of an algorithm, oftenused in the design phase of programming towork out the logical flow of a program.
Flow Charts are used to help programmers in theearly stages of programming.
A flow chart is a chart that flows from one stage tothe next and it will show what stage or event is first,second, third etc...
-
8/2/2019 Chapter 2 - The Decision Control Structure
16/43
FLOWCHART EXAMPLE
-
8/2/2019 Chapter 2 - The Decision Control Structure
17/43
SYMBOLSUSEDIN FLOW CHARTS
-
8/2/2019 Chapter 2 - The Decision Control Structure
18/43
EXERCISE
Draw a flow chart and write a program for theproblem given below
While purchasing certain items, a discount of 10%is offered if the quantity purchased is more than1000. If quantity and price per item are inputthrough the keyboards, write a program to calculatethe total expenses
-
8/2/2019 Chapter 2 - The Decision Control Structure
19/43
FLOW CHART
-
8/2/2019 Chapter 2 - The Decision Control Structure
20/43
PROGRAM
#include
void main(void)
{
int Discount=0, Quantity;float Rate, Total;
scanf(%d %f, &Quantity, &Rate);
if(quantity>1000)
Discount=10;
Total= Quantity*Rate (Discount/100 * Rate * Quantity)
printf(Total Expenses =%f,Total);
}
-
8/2/2019 Chapter 2 - The Decision Control Structure
21/43
-
8/2/2019 Chapter 2 - The Decision Control Structure
22/43
THEIF-ELSE STATEMENT
IF expression executes instructions or set ofinstructions if the expressions is TRUE
If does nothing if the expression is FALSE
The else statement allows us to evaluateinstructions or set of instructions if the expression isfalse
If(expression)instruction;
else
instruction;
-
8/2/2019 Chapter 2 - The Decision Control Structure
23/43
EXAMPLE Write a program that tells takes an integer as input
and tells us whether the number entered is odd oreven.
#include
void main(void)
{
int a;
scanf(%d, &a);
if((a%2)==0)
printf(Number entered is even);
else
printf(Number entered is odd);
}
-
8/2/2019 Chapter 2 - The Decision Control Structure
24/43
FLOW CHART
-
8/2/2019 Chapter 2 - The Decision Control Structure
25/43
FEWPOINTSWORTH REMEMBERING
Groups of statements after the if up to elseis called an if block. Similarly, statementsafter else are in the else block
Else part always comes after an ifexpression
If an if block has only one statement thenbraces can be ignored. Same goes for theelse block
-
8/2/2019 Chapter 2 - The Decision Control Structure
26/43
FEWPOINTSWORTH REMEMBERING
if(expressions)
{ // if block starts
statement1;
statement2;} //if block ends
else
{ //else block starts
statement1;
statement2;
} //else block ends
-
8/2/2019 Chapter 2 - The Decision Control Structure
27/43
NESTED IF-ELSES
You can define an entire if-else construct within either thebody of the if statement or the body of an else statement. E.g.
If(expression1)
statement;
else{
if(expression2)
statement;
else
statement;}
NOTE: Indenting is VERY IMPORTANT. Improves readability
-
8/2/2019 Chapter 2 - The Decision Control Structure
28/43
-
8/2/2019 Chapter 2 - The Decision Control Structure
29/43
SOLUTION
#include
Void main(void)
{
int m1,m2,m3,m4,m5,percent;
printf(Enter marks in five subjects);
scanf(%d %d %d %d
%d,&m1,&m2,&m3,&m4,&m5);percent=(m1+m2+m3+m4+m5)/500 * 100;
-
8/2/2019 Chapter 2 - The Decision Control Structure
30/43
SOLUTION
If(percent>=60)
printf(First Division);
Else
{
If(percent>=50)
printf(Second Division);
Else
{
If(percent>=40)
printf(Third Division);Else
printf(Fail);
}
}
-
8/2/2019 Chapter 2 - The Decision Control Structure
31/43
DISADVANTAGESINTHEAPPROACH
Else blocks are moving towards the right
Can become difficult to match the corresponding ifand else blocks
Can become difficult to match braces of if and elseblocks
-
8/2/2019 Chapter 2 - The Decision Control Structure
32/43
USEOF LOGICAL OPERATORS
C allows usage of three logical operators
AND &&
OR ||
NOT !
The logical operators contain two symbols note 1
& and | have different meanings as compared to &&and ||
Logical operators can be used to combine two ormore conditions. E.g.
If(Marks>90 && Marks
-
8/2/2019 Chapter 2 - The Decision Control Structure
33/43
AND OPERATOR
AND operator is used when we want both theconditions to be true
Here the condition will become true if and only if thenumber is between 50 and 60
A>50 Operator A
-
8/2/2019 Chapter 2 - The Decision Control Structure
34/43
OR OPERATOR
OR operator is used when we want either of theconditions to be TRUE.
Here the condition will be true if either A or B isgreater than 50.
A>50 Operator B>50 Result
FALSE || FALSE FALSE
FALSE || TRUE TRUE
TRUE || FALSE TRUETRUE || TRUE TRUE
-
8/2/2019 Chapter 2 - The Decision Control Structure
35/43
NOT OPERATOR
The not operator i.e. ! reverses the result of the
expression.
E.g. 2==2 is true
However, !(2==2) if false
Similarly, 1==2 is false
But !(1==2) is true
If(!flag) is equivalent to if(flag==0)
-
8/2/2019 Chapter 2 - The Decision Control Structure
36/43
PROGRAM USING LOGICAL OPERATORS
#include
Void main(void)
{
int m1,m2,m3,m4,m5,percent;
printf(Enter marks in five subjects);
scanf(%d %d %d %d
%d,&m1,&m2,&m3,&m4,&m5);percent=(m1+m2+m3+m4+m5)/500 * 100;
-
8/2/2019 Chapter 2 - The Decision Control Structure
37/43
PROGRAM USING LOGICAL OPERATORS
If(percent>=60)
printf(First Division);
if(percent>=50 && percent=40 && percent
-
8/2/2019 Chapter 2 - The Decision Control Structure
38/43
ADVANTAGESINTHEAPPROACH
No else Blocks
No need to match the corresponding if and elseblocks
Matching braces of if blocks becomes easy
-
8/2/2019 Chapter 2 - The Decision Control Structure
39/43
THE ELSEIF CLAUSE
There is one more way of writing if-else statements
In this we use the else if clause
If(percent>=60)
printf(First Division);
else if(percent>=50)
printf(Second Division);
else if(percent>=40)
printf(Third Division);
else if(percent
-
8/2/2019 Chapter 2 - The Decision Control Structure
40/43
THE ELSEIF CLAUSE
In this approach the second else if statement is linkwith the first if statement.
The second else if part only works if the firstcondition becomes false.
The same pattern is followed as we go down
In short, the else part will only work if all theconditions above have failed.
Similarly, other conditions are not checked if thefirst statement is true.
-
8/2/2019 Chapter 2 - The Decision Control Structure
41/43
COMPARISONBETWEEN ELSE-IF AND NESTED IF
i=2;
If(i==2)
printf(true);
else{
If(j==2)
printf(true again);
}
-
8/2/2019 Chapter 2 - The Decision Control Structure
42/43
COMPARISONBETWEEN ELSE-IF AND NESTED IF
i=2;
If(i==2)
printf(true);
else if(i==2)
printf(true again);
-
8/2/2019 Chapter 2 - The Decision Control Structure
43/43
THECONDITIONAL OPERATOR
If(a%2==0)
b=t
else
b=f
Can be written as
a%2==0 ? b=t : b=f ;Condition if block else block