0% found this document useful (0 votes)
13 views14 pages

Week 8

This document discusses if statements and flow charts for making decisions in computing fundamentals. It covers if statements, if-else statements, and if-else-if statements with syntax and examples. It also discusses logical operators, relational operators, short circuit evaluation, and practice problems involving evaluating conditions and using if/else statements to solve problems based on different conditions. The instructor is Shehzad Aslam and it is for week 8 of the course.

Uploaded by

usmanaslam17621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views14 pages

Week 8

This document discusses if statements and flow charts for making decisions in computing fundamentals. It covers if statements, if-else statements, and if-else-if statements with syntax and examples. It also discusses logical operators, relational operators, short circuit evaluation, and practice problems involving evaluating conditions and using if/else statements to solve problems based on different conditions. The instructor is Shehzad Aslam and it is for week 8 of the course.

Uploaded by

usmanaslam17621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Computing Fundamentals

Making Decision – IF Statement and flow charts


Week 8 Instructor: Shehzad Aslam
Decision
Execute or Skip statements based on some condition

 If some-test-true  If some-test-true
 Then do the following  Then do the following
 Do the following  Do the following
 If some-test-true  Else If some-other-test-true
 Then do the following  Then do the following
 Do the following  Do the following
 Else  …
 Then do the following  Else
 Do the following  Then do the following
 Do the following
Decision - Examples

 If voltages exceeds 240  If temperature exceeds 35


 Turn off the switch  Turn on AC
 If voltages drops 200 OR voltages  Turn off Fan
exceeds 240  Else If temperature exceeds 25
 Turn off the switch  Turn off AC
 If 5 seconds past  Turn ON Fan
 Turn on the Red light  Else
 If intensity drops 30 lux  Turn OFF AC
 Turn on Light  Turn ON FAN
 Else
 Turn off light
Condition
 Comparison / Relational Operators
 Used to compare values and gives result in TRUE or FALSE
 >, <, >=, <=, ==, !=
 Logical Operators
 Used to combine more than one conditions
 When more than one condition is combined its called compound condition
 && (and) , || (or) , ! (not)
Making Condition - Examples

 voltages exceeds 240?  Temperature exactly 30?


 voltages drops 200 OR voltages exceeds  No is even?
240?
 Number divides fully by 5?
 Turn off the switch?
 P1 is taller than P2?
 5 seconds past?
 Motor not rotating above 5kRPM
 intensity drops 30 lux ?
and blade temperature exceeds 70
 Number value is 50 or above? deg?
 Number is between 40 and 80?  Marks between 70 and 90?
 Speed is 40 or above and its not 3rd gear?  Price below 990?
Short Circuit Evaluation
 Computer ignore some condition based on partial results to speedup processing

 A < B && C > D


 A < B || A > C
 (A < B && B > D) || (G == M || K != 40) && T >= 500
If Statement
 Time is given in hr and minutes and check if time  Syntax
exceeds 9:18 then print you are late  if (condition) {
 Take a number from user and double it if it is multiple of  Statement1;
10
 Statement 2;
 If marks are above 40 print you passed
 …
 Take a character from user and print if it’s a vowel
 }

 Curly braces are optional


 Statements inside curly braces are called
body of if
 If braces are omitted then very next
single statement is considered as body
If-else Statement

 Time is given in hr and minutes and  if (condition) {


check if time exceeds 9:18 then print  Statement1;
you are late otherwise show you are on
 Statement 2;
time
 …
 Take a number from user and double it if
it is multiple of 10 otherwise make it  }
half  else {
 If marks are above 40 print you passed  Statement1;
otherwise show sorry you failed
 Statement 2;
 Take a character from user and print if
 …
it’s a vowel or consonant
 }
If-else-if Statement
 Time is given in hr and minutes and  if (condition) {
check if time exceeds 9:18 then print  Statement1;
you are late if its before 9:18 show you
 …
came early if its exactly 9:18 otherwise
show you hardly on time  }
 Take a number from user and double it if  else if (condition) {
it is multiple of 10 if its multiple of 3  Statement1;
then add 20 to it if its even make it
square otherwise make it half  …
 If marks are 81-100 show grade A, if  }
marks 61-80 show grade B, if marks 41-  else {
60 Show grade C, if marks 0-40 show
 Statement1;
sorry you failed
 …
 }
Practice Problems
 Evaluate the condition to true of false and tell which condition will be ignored based on
short circuit evaluation

Statement T/F Ignored conditions

int x = 10;
!(x > 10)

int x = 10, y = 15;


x <= 5 || y < 15
int x = 10, y = 15, z = 20;
(x != 5) && (y != z)
int x = 10, y = 15, z = 20;
x >= z || (x + y >= z)
int x = 10, y = 15, z = 20;
(x <= y - 2) && (y >= z) || (z -
2 != 20)
Solve the Following Problem – Will be checked
 input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Find total
marks, calculate percentage and grade according to following: (store results in variables)

Percentage >= 90% : Grade A


Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
 Use separate if to show comments

for A show “Excellent” and comments for B too


for B grade show “Good work” only
for C grade show “It could be batter” only
for D grade show “Saved form worst” only
Otherwise say “Fallen into a trap”
Practice Problems
 Input two sides of rectangle and show its square or not. Then ask user that enter 1 to
calculate parameters and 2 to calculate area. Based on the user choice calculate and
show results. Upon invalid choice show invalid option.
 Input a number from use and test its 4 digit number or not? If its 4 digit number then test each
digit is same?
 Saleman has 12500 basic pay. He is given 10.5% bonus on each sale. If sale exceeds 100k then
he is given extra 3% bonus on amount exceeding 100k. Input the sale from user and show net
pay.
Reading

 Chapter 4 from C++ Programming by DS Malik


 Solve exercise of chapter 4

You might also like