FUNDAMENTALS OF COMPUTER PROGRAMMING
Instructor Mr. Asad Ali Shah
CHAPTER 2: THE DECISION CONTROL STRUCTURE
Instructor Mr. Asad Ali Shah
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 situation and 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
CONTROL STRUCTURES
Control structure enable us to specify the order in which the various instructions in a program are to be executed. In short, they determine the flow of control The three types of control structures are given below
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
DECISION CONTROL STRUCTURE
Decide whether the instruction(s) should be executed or not. A decision control instruction can be implemented in C using
The if statement The if-else statement The conditional operators
DECISION CONTROL STRUCTURE
The different variations of Decision Control Structure are given below
If Statement If-else statement Nested if-else statements Else if statements Case control
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 then the statement is executed Otherwise, the compiler will skip past it
This is all good but how do we write those conditions anyway?
CONDITIONAL EXPRESSIONS
The conditional expression are used with if They are constructed using relational operators
Relational Operators allow us to compare two values.
This Expression x==y x!=y x<y Is true if x is equal to y x is not equal to y x is less than y
x>y
x<=y x>=y
x is greater than y
x is less than and equal to y x is greater than and equal to y
EXERCISE
What are the results of the following relational expressions
9 < 25 9<3 9 > 14 9 <= 17 9 >= 25 9 == 13 9 != 13 9 !< 25
SOLUTION
true false false true false false true Error, the "not less than" is not a valid operator.
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
SIMPLE PROGRAM USING IF STATEMENT
#include<stdio.h> void main(void) { int num; printf(Enter a number less than 10); scanf(%d,&num);
if(num<10) printf(What a obedient servant you are);
}
WHAT IS HAPPENING HERE?
#include<stdio.h> void main(void) { int num; printf(Enter a number equal to10); scanf(%d,&num);
if(num=10) printf(What a obedient servant you are);
}
THE ANSWER TO X=10 STATEMENT
The
if conditions will be satisfied every time. All assignment operations like X=Y are considered as true. All values other than zero are considered to be true
FLOW CHARTS
A graphic representation of an algorithm, often used in the design phase of programming to work out the logical flow of a program. Flow Charts are used to help programmers in the early stages of programming. A flow chart is a chart that flows from one stage to the next and it will show what stage or event is first, second, third etc...
FLOWCHART EXAMPLE
SYMBOLS USED IN FLOW CHARTS
EXERCISE
Draw a flow chart and write a program for the problem given below While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboards, write a program to calculate the total expenses
FLOW CHART
PROGRAM
#include<stdio.h> 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); }
MULTIPLE STATEMENTS WITHIN IF
An if expression can contain more than one statements Simply add starting and ending braces for the set of statements in the if expression
If(A>3) { B=5; Printf(%d,A+B); }
THE IF-ELSE STATEMENT
IF expression executes instructions or set of instructions if the expressions is TRUE If does nothing if the expression is FALSE
The else statement allows us to evaluate instructions or set of instructions if the expression is false If(expression) instruction; else instruction;
EXAMPLE
Write a program that tells takes an integer as input and tells us whether the number entered is odd or even. #include<stdio.h> void main(void) { int a; scanf(%d, &a); if((a%2)==0) printf(Number entered is even); else printf(Number entered is odd);
FLOW CHART
FEW POINTS WORTH REMEMBERING
Groups
of statements after the if up to else is called an if block. Similarly, statements after else are in the else block part always comes after an if expression an if block has only one statement then braces can be ignored. Same goes for the else block
Else
If
FEW POINTS WORTH REMEMBERING
if(expressions) { statement1; statement2; } else { statement1; statement2; }
// if block starts
//if block ends
//else block starts
//else block ends
NESTED IF-ELSES
You can define an entire if-else construct within either the body 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
PROBLEM
The Marks obtained by a student in 5 different subjects are input through the keyboard. The student gets division as per the following rules
Percentage above or equal to 60 is 1st Division Percentage between 50 and 59 is 2nd Division Percentage between 40 and 49 is 3rd Division Percentage less than 40 is Fail
SOLUTION
#include<stdio.h> 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;
SOLUTION
If(percent>=60) printf(First Division); Else { If(percent>=50) printf(Second Division); Else { If(percent>=40) printf(Third Division); Else printf(Fail); }
DISADVANTAGES IN THE APPROACH
Else blocks are moving towards the right Can become difficult to match the corresponding if and else blocks Can become difficult to match braces of if and else blocks
USE OF 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 or more conditions. E.g. If(Marks>90 && Marks<100)
AND OPERATOR
AND operator is used when we want both the conditions to be true Here the condition will become true if and only if the number is between 50 and 60
A>50 FALSE
FALSE TRUE TRUE
Operator &&
&& && &&
A<60 FALSE
TRUE FALSE TRUE
Result FALSE
FALSE FALSE TRUE
OR OPERATOR
OR operator is used when we want either of the conditions to be TRUE. Here the condition will be true if either A or B is greater than 50.
A>50 FALSE
FALSE TRUE TRUE
Operator ||
|| || ||
B>50 FALSE
TRUE FALSE TRUE
Result FALSE
TRUE TRUE TRUE
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, But
1==2 !(1==2)
is false is true
If(!flag)
is equivalent to
if(flag==0)
PROGRAM USING LOGICAL OPERATORS
#include<stdio.h> 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;
PROGRAM USING LOGICAL OPERATORS
If(percent>=60) printf(First Division); if(percent>=50 && percent<60) printf(Second Division); if(percent>=40 && percent<50) printf(Third Division); if(percent<40) printf(Fail); }
ADVANTAGES IN THE APPROACH
No else Blocks No need to match the corresponding if and else blocks Matching braces of if blocks becomes easy
THE ELSE IF 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<40) printf(Fail);
THE ELSE IF CLAUSE
In this approach the second else if statement is link with the first if statement. The second else if part only works if the first condition becomes false. The same pattern is followed as we go down In short, the else part will only work if all the conditions above have failed. Similarly, other conditions are not checked if the first statement is true.
COMPARISON BETWEEN ELSE-IF AND NESTED IF
i=2; If(i==2) printf(true); else { If(j==2) printf(true again); }
COMPARISON BETWEEN ELSE-IF AND NESTED IF
i=2;
If(i==2) printf(true); else if(i==2) printf(true again);
THE CONDITIONAL OPERATOR
If(a%2==0) b=t else b=f
Can be written as a%2==0 Condition ? b=t if block : b=f ; else block