Title: C Simple Choice
1 C Simple Choice and Repetition Chapter 2A
CSIS 10A
2Agenda
- if-else (conditional) statements
- Relational operators and expressions
- nested if-else
- while loops
- Indefinite loops
- Counted loops
3Simple Flow of Control
2.4
- Flow of control
- The order in which statements are executed
- Conditional (Branch)
- Lets program choose between two alternatives
- Loop
- Lets program repeat a block of statements many
times
4Conditional (Branch) Example
- To calculate hourly wages there are two choices
- Regular time ( up to 40 hours)
- gross_pay rate hours
- Overtime ( over 40 hours)
- gross_pay rate 40 1.5 rate (hours -
40) - The program must choose which of these
expressions to use
5 Conditional Flowchart (if-else)
true
false
hoursgt40
calc pay with overtime
calc pay regular
display pay to screen
6Designing the Conditional
- Decide if (hours gt40) is true
- If it is true, then use gross_pay rate
40 1.5 rate (hours - 40) - If it is not true, then use gross_pay rate
hours
7Implementing the Branch
- if-else statement is used in C to perform a
branch - if (hours gt 40) gross_pay rate 40
1.5 rate (hours - 40) - else
- gross_pay rate hours
8Calculating Pay
- // CALCPAY.CPP A program to calculate paychecks
with overtime - include ltiostream.hgt
- void main()
- float hours, pay, rate
- cout ltlt "enter hours and rate "
- cin gtgt hours gtgt rate
- if (hours gt 40) pay rate 40 1.5
rate (hours - 40) - else
- pay rate hours
- cout ltlt "pay is " ltlt pay
9General Syntax, if-else
- if (expr1) // expr 1 is a relational
expression -
- C block 1 // any block of C statements
-
- else
-
- C block 2 // any block of C statements
-
10Relational Expressions
- Relational expressions are expressions that are
either true or false - (4lt3)
- (hoursgt40)
- (LengthgtWidth)
- relational operators such as gt (greater than)
are used to compare variables and/or numbers
11Relational Operators
- The Six Relational operators
- (No spaces allowed between the symbols!)
- lt less than
- gt greater than
- lt greater than or equal to
- gt less than or equal to
- equal or equivalent DONT USE NOW
- ! is not equal to DONT USE NOW
- (not suitable with floats)
12Compound Statements
- A compound statement is more than one statement
enclosed in - Branches of if-else statements often need to
execute more that one statement - Example if (boolean expression)
true statements
else
- false
statements
13Example with two if-else statements
- if (hours gt 40.0)
- pay 40.0 rate (hours 40) 1.5 rate
- else
- pay hours rate
- cout ltlt "pay is " ltlt pay
- if (hours gt 40.0)
- cout ltlt " overtime worked"
- else
- cout ltlt " no overtime worked"
14Above redone (simplified) with compound statements
- if (hours gt 40.0)
-
- pay 40.0 rate (hours 40) 1.5 rate
- cout ltlt " pay is " ltlt pay ltlt " overtime
worked" -
- else
-
- pay hours rate
- cout ltlt " pay is " ltlt pay ltlt " no overtime" ltlt
endl -
15Your Turn
- Can you
- Write an if-else statement that outputs the
wordHigh if the value of the variable score is
greaterthan 100 and Low if the value of score is
less than or equal to 100?
16Multi-Way Branches
- How to specify more than two possibilities?
- How to specify a range of values (1 way)?
- Answer if-else if
- if (expr1)
- statement1 // do if expr1 is true
- else // do if expr1 is false
- if (expr2)
- statement2 // expr1 false, expr2 true
- else
- statement3 // both expr1 and expr2 false
17Nested if-else (multi-way branch)
Syntax
Example
if( gradegt90) coutltlt"Your GPA is
A."ltltendl else if (gradegt80) coutltlt"Your
GPA is B."ltltendl else if (gradegt70)
coutltlt"Your GPA is C."ltltendl else if
(gradegt60) coutltlt"Your GPA is
D."ltltendl else coutltlt"Your GPA is
F."ltltendl coutltlt"You will cry!"ltltendl
if (expr1) block1 else if (expr2)
block2 else if (expr3) block
3 else block4
18Repetition, Repetition, Repetition
- How can you repeat a block of statements ?
- Keep calculating sphere volumes until radius of
-1is entered - Calculate paychecks for 100 employees
- Find the highest of a list of test scores
- Print a table of Fahrenheit vs Celsius Temps
19Simple Loops
- When an action must be repeated, a loop is used
- C includes several ways to create loops
- Until chapter 7 we only use the while-loop
- Example count_down 3
- while (count_down gt 0)
cout ltlt
Hello count_down
cout_down 1 - Output Hello Hello Hello
20Control Structures (while-loop, repetition)
Flowchart while-loop
count_down 3
Display Hello Subtract 1 from count_down
true
count_downgt0
false
21While Loop Operation
- First, the relational expression is evaluated
- If false, the program skips to the line following
the while loop - If true, the body of the loop is executed
- During execution, some item from the boolean
expressionis changed - After executing the loop body, the relational
expression is checked again repeating the
processuntil the expression becomes false - A while loop might not execute at all if the
relational expression is false on the first check - Or the loop could execute forever if the
relational expression is never false
22while Loop Syntax
- while (boolean expression is true)
statements to repeat - Semi-colons are used only to end the
statementswithin the loop (not after boolean
expression) - while (boolean expression is true)
statement to repeat
23Infinite Loops
- Loops that never stop are infinite loops
- The loop body should contain a line that
willeventually cause the boolean expression to
become false - Example Print the odd numbers less than 12
x 1 while (x lt 12)
cout ltlt x
ltlt endl x x 2
- In this loop, make x larger xx2
24Your Turn
- Can you
- Show the output of this code?x 10while ( x gt
0) cout ltlt x ltlt endl x
x 3 - Show the output of the previous code using the
comparison x lt 0 instead of x gt 0?
25Repeating Calculations
- Usually you use a loop to repeat some code
- Counted Loop
- repeat a certain number of times
- Indefinite Loop
- repeat until a key value (sentinel) is entered
26Simple Pay Calculation (no overtime)
- // CALCPAY.CPP A program to calculate paychecks
- include ltiostream.hgt
- void main( )
- float hours, pay, rate
- cout ltlt "enter hours and rate "
- cin gtgt hours gtgt rate
- pay rate hours
- cout ltlt "pay is " ltlt pay
-
- Lets see how to repeat this
27Counted (Definite) Loop Concept
- Add a counter variable declaration
- Set counter to initial value (usually 0)
- while (counter lt Max)
- get input data
- calculate result
- display result
- add 1 to counter
28Counted (Definite) Loop Code
- // CALCPAY.CPP A program to calculate paychecks
- include ltiostream.hgt
- void main( )
- float hours, pay, rate, count0
- while(countlt3)
-
- cout ltlt "enter hours and rate "
- cin gtgt hours gtgt rate
- pay rate hours
- cout ltlt "pay is " ltlt pay
- countcount1
-
29Unfolding the Code (Counted)
- count0
- countlt3? T
- Do a complete pay calc
- count1
- countlt3? T
- Do a complete pay calc
- count2
- countlt3? T
- Do a complete pay calc
- count3
- countlt3? F Quit Loop
30Indefinite Loop Concept
- Read first data set
- while (data is Good) (not a sentinel)
- calc result for last data
- display result
- get another data set
-
-
31Indefinite Loop Code
- // CALCPAY.CPP A program to calculate paychecks
- include ltiostream.hgt
- void main( )
- float hours, pay, rate
- cout ltlt "enter hours and rate, -1 -1 to stop
" - cin gtgt hours gtgt rate // get
first data set - while(hoursgt0) // is data OK
-
- pay rate hours // process last data
set - cout ltlt "pay is " ltlt pay
- cin gtgt hours gtgt rate // get next
data set -
32Unfolding the Code (Indefinite)
- Enter hours and rate, -1 -1 to stop
- 35 10.00
- hoursgt0? T
- Calc and display pay (350.00)
- 40 12.00
- hoursgt0? T
- Calc and display pay (480.00)
- -1 -1 (Sentinel)
- hoursgt0? F stop
33What is a Sentinel?
- Data that is clearly not validphony
- Hours worked -1
- Radius -1
- HighTemperature 1000
- JellyBeans-1
34Thats a wrap !
- What we learned today
- if-else (conditional) statements
- Relational operators and expressions
- Nested if-else
- Indefinite loops
- Counted loops
35Go back home proud ! Youre a C programmer !