1
- Dr. Adriana Badulescu Kallas
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
Chapter 4: Control Structures I (Selection) C++ Programming 1 Dr. - - PowerPoint PPT Presentation
Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Chapter 4: Control Structures I (Selection) C++ Programming 1 Dr. Adriana Badulescu Kallas Introduction to Computer Science &
1
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
2
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
3
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
4
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
false true
Expression
Statement false true
Expression
Statement1 Statement2 Statement1 Statement2 Statement3
Sequence Selection Repetition
5
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
6
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
(8 < 15)
(6 != 6)
(2.5 > 5.8)
(5.9 <= 7.5)
(‘a’ == 97)
(97.0 == 97)
7
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
cout << (‘a’ == 97); cout << (‘a’ > ‘z’); cout << (97.0 == 97); 101
8
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
9
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
10
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
11
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
12
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
13
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
14
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
15
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
16
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
17
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
18
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
19
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
20
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
The Statement is executed if the value of the Expression is true The Statement is bypassed if the value is false and the
false true
Expression Statement
21
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
if (score >= 60); grade = ‘P’; if score >= 60 grade = ‘P’; grade = ‘F’; if (score >= 60) grade = ‘P’;
CORRECT – but we do need the grade = ‘F’; assignment before the if to make sure the grade will have a value if the score is lower than 60 INCORRECT – missing parentheses SYNTACTICALLY CORRECT – it has a valid if with an empty statement – that does not do (if (score >=
60);) , followed by an assignment grade = ‘P’ but
LOGICALLY INCORRECT – it will make the grade =
‘P’ for any score so you need to delete the ; at the
end of the first line
if (score >= 60) grade = ‘P’;
SYNTACTICALLY CORRECT but LOGICALLY INCORRECT – it will make the grade = ‘P’ for scores larger than 60 but will not assign any value for scores smaller than 60
22
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
23
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
false true
Expression Statement1 Statement2
24
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours – 40.0); else wages = hours * rate;
25
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
Statement1 Statement2 StatementN
…
26
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
if (age > 18) { cout << "Eligible to vote." << endl; cout << "No longer a minor." << endl; } else { cout << "Not eligible to vote." << endl; cout << "Still a minor." << endl; }
27
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
if (expression1) statement1; else if (expression2) statement2; else statement3; if (expression1) statement1; else { if (expression2) statement2; else statement3; }
28
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
29
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
30
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
31
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
32
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
33
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
34
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
5 15 0<=-5 <=10 0<=5 <=10 0<=15<=10 0 <=10 1 <=10 1 <=10 1 1 1 0 10
Always
true
5 15 0<=-5 && -5<=10 0<=5 && 5<=10 0<=15 && 15<=10 0 && 1 1 && 1 1 && 0 1 0 10 true
between 0 and 10
35
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
36
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
37
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
if (x = 5) cout << "The value is five." << endl; else cout << "The value is five." << endl;
38
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
39
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
40
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
41
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
42
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
43
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
switch (Expression) { case Value1: Statement1 break; case Value2: Statement2 break; … case ValueN: StatementN break; default: Statements }
44
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming false true
Expression ==Value1 Expression ==Value1 Statement1
false true
Expression ==Value2 Expression ==Value2 Statement2
false true
Expression ==ValueN Expression ==ValueN StatementN
…
Statements
45
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
46
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
47
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
48
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
49
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
50
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
51
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
52
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
– Bill processing fee: $4.50 – Basic service fee: $20.50 – Premium channel: $7.50 per channel
– Bill processing fee: $15.00 – Basic service fee: $75.00 for first 10 connections/$5.00 for
– Premium channel cost: $50.00 per channel for any number of
53
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
54
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
55
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
– Customer for whom the billing amount is calculated (residential or
business)
– Number of premium channels to which the customer subscribes – Bill processing fees and the cost of a premium channel
– Number of basic service connections – Number of premium channels
56
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
– Prompt user for number of premium channels – Compute and print the bill
– Prompt user for number of basic service connections and
– Compute and print the bill
57
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
58
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
59
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
if (numOfBasicServConn <= 10) amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL; else amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + (numOfBasicServConn - 10) * BUS_BASIC_CONN_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL;
60
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
Output floating-point numbers with two decimal places
61
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
62
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
63
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
64
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
Two selection structures: one-way selection and two- way selection The expression in an if or if...else structure is usually a logical expression A sequence of statements enclosed between braces, { and }, is called a compound statement or block of statements Using assignment in place of the equality
semantic error switch structure handles multiway selection break statement ends switch statement Use assert to terminate a program if certain conditions are not met