Download presentation
Presentation is loading. Please wait.
Published byShanon James Modified over 9 years ago
1
CS114-009 Class 04 Topics Selection statement – IF Expressions More practice writing simple C++ programs Announcements Read pages 50-73 for next class.
2
Class Exercise from last Thurs. What are the final values for e, f, g, and y? #include using namespace std; int main( ) { int a=2, b=4, c=8, d=5, e, f, g; double x = 2.5, y; e = (a + b * c) / (d + 12); f = x / 2; g = c % d; y = c / d + x; }
3
Questions to consider What if we used double instead of int for our variables in the microwave program? Try it Does the output change? What if min and sec are both a double, but time is still an int? Does it still run? Does it always work?
4
Comments on writing programs Everything is a combination of Sequence, Selection, and Iteration statements Program from last time, sequence only Read / Read / Compute / Print Other than S/S/I, need to know Variables – storage locations – today Functions – next month Arrays – March
5
Expressions statement; Sequence statements statement; No decisions Just perform one statement after another Selection and Iteration statements Make a decision to determine what to do (Which path? Another loop?) The decision is an “expression” which evaluates to either true or false statement; truefalse statement; truefalse
6
CS 114 Content (details) Algorithms Definition Tracing Writing Analyzing Efficiency Recursion Searching Sorting Language Implementation (using Visual Studio.NET 2005) Environment / compilation / debugging / resources / tools Sequence input output assign File I/O formatting Selection if switch Iteration for while Basic Elements variables operators expression functions arrays objects
7
If statement Syntax if ( expression ) statement ; // then clause else statement ; // else clause Expression determines path to take If TRUE, does “then” clause If FALSE, does the “else” clause (when it exists) Example if ( a < b ) cout << a << endl; else cout << b << endl; statement; truefalse
8
Need to understand expressions True or False? int a, b, c; a=5; b=3; c=10; ( b == c ) ( (a+b) < c) (b == (c / 3)) ( (b > c) || (c != 10) ) ( (a <= c) && (a == b) ) == equals != not equal <less than <= less than or equal to >greater than >= greater than or equal to &&logical and (both must be true) ||logical or (one must be true) !logical not (opposite of the expr)
9
Modifying Our First Program #include using namespace std; int main ( ) { int min, sec; cout > min; cout > sec; int time; if (min == 0) time = sec; else time = min * 60 + sec; cout << "total time is " <<time << endl; return 0; }
10
Class Exercises int main ( ) { int a=5, b=10, c=15; if ( a > (c%b) ) cout << “yes” << endl; else cout << “no” << endl; if ( (a+b) == c ) cout << “UA” << endl; if ( (b/a) < (c/a) ) cout << “AU” << endl; } int main ( ) { int a=2, b=4, c=8; if ( (b>1) && (c<10) ) if ( (a>c) || (a<b) ) cout << “UA” << endl; if ( (c%b) == a ) cout << “Roll” << endl; else cout << “Tide” << endl; if ( (a != 2) && (b != 4) ) cout << “Yes” << endl; else cout << “No” << endl; }
11
More on selection statements Selection Statements come in three flavors if statements switch statements ?: operator (we will not use this in CS 114) We will focus on if statements for now Can use { and } to group a block of statements together if (a == b) { c = a * a; b = b + 1; } else { a = a - 1; }
12
Watch out! int main( ) { int a = 5; if (a = 4) cout << “yes” << endl; else cout << “no” << endl; } Expression evaluates: True is non-zero False is zero C++ does the following The expression “a = 4” is an assignment stmt assigns a the value of 4 expression itself evaluates to value assigned (4) 4 is non-zero (TRUE) this expression is true! Output: “yes” Syntax is correct no compile errors Logic is wrong a semantic error
13
Class Exercises What does the program at the right do? int main( ) { int a, b, c; cin >> a; cin >> b; cin >> c; int num; if (a >= b) num = a; else num = b; if (num < c) num = c; cout << num << endl; }
14
Exercise - Put sequence & selection together Write a C++ program that: Reads in four integers from the user Calculates an average for these four numbers Prints out the average and the number of inputted values that were above the average (greater than or equal to) Example #1 Input = 1, 2, 3, 4 Output = 2.5 and 2 Example #2 Input = 1, 1, 1, 9 Output = 3 and 1 Hint: use a “counter” to keep track of how many are above average
15
Quick check of the solution #include using namespace std; int main( ) { int a, b, c, d; cin >> a;// read in the four values cin >> b; cin >> c; cin >> d; double avg = (a + b + c + d) / 4.0;// average should be a real number int count = 0;// this is our counter if (a >= avg) count = count + 1;// increment for each above avg if (b >= avg) count = count + 1; if (c >= avg) count = count + 1; if (d >= avg) count = count + 1; cout << avg << " " << count << endl;// print results }
16
End of Class 04 Read pages 50 – 73 for next time. Make sure you can do today’s exercises.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.