Homework 1 Feedback 4 students missed deadlines 3
Homework 1 Feedback • 4 students missed deadlines • 3 students have wrong implementation • 2 students have compilation errors • 1 students does not have a good quality code • 85% of the students received 100 2 -1
Eclipse and Cygwin Problems Experience shared by Chris Dumas • Problem 1: This is a known problem with Windows Eclipse 64 bit. Console output just doesn't display unless you use debug, and even then you can't input. • Solution: use 32 bit, even on 64 bit machines. • Problem 2: Cygwin is actually incomplete in its gcc C++11/14 implementation. • Decision: Run a Linux VM instead of trying to work with Cygwin's gcc. It's going well so far. 2 -2
COMP 2710 Software Construction Flow of Control Programming Exercises Dr. Xiao Qin Auburn University http: //www. eng. auburn. edu/~xqin@auburn. edu Some slides are adapted from notes by Dr. Walter Savitch (UCSD)
The switch Statement • Controlling multiple branches • Uses controlling expression which returns bool data type (true or false) 2 -4
The switch Statement 2 -5
The switch: multiple case labels • Execution "falls thru" until break – switch provides a "point of entry” case "A": case "a": cout << "Excellent: you got an "A"!n"; break; case "B": case "b": cout << "Good: you got a "B"!n"; break; – Note multiple labels provide same "entry" 2 -6
Loops • 3 Types of loops in C++ – while • Most flexible • No "restrictions" – do-while • Least flexible • Always executes loop body at least once – for • Natural "counting" loop 2 -7
while Loop Example count = 0; while (count < 3) { cout << "Hi "; count++; } // Initialization // Loop Condition // Loop Body // Update expression Loop body executes how many times? 2 -8
do-while Loop Syntax Often used for menu applications. Why? 2 -9
do-while Loop Example count = 0; // Initialization do { cout << "Hi ”; // Loop Body count++; // Update expression } while (count < 3); // Loop Condition • Loop body executes how many times? • do-while loops always execute body at least once! 2 -10
for Loop Example for (count=0; count<3; count++) { cout << "Hi "; // Loop Body } • How many times does loop body execute? • Initialization, loop condition and update all "built into" the for-loop structure! • A natural "counting" loop 2 -11
Programming Exercise 1 • Write a C++ program that repeatedly asks a user to enter any number other than 0 until the user enters the number 0. • If the user enters 0, then your program notifies the user "Hey! you can not enter 0!" and exit the program. 3 -12
Programming Exercise 1 int main() { int num; cout << “Enter a number other than 0 (0 exit): ”; cin >> num; while (num != 0) { cout << “enter a number other than 0 (0 exit): ”; cin >> num; } cout << "Hey! you can not enter 0!" << endl; return 0; } 3 -13
Programming Exercise 2 • Write a C++ program that repeatedly asks a user to enter any number other than 0 until the user enters the number 0. • If the user enters 0, then your program notifies the user "Hey! you can not enter 0!" and exit the program. • Modify your previous program • After five iterations if the user still hasn't entered 0, then tell the user “You win!" and exit. 3 -14
Programming Exercise 2 After five iterations if the user still hasn't entered 0, then output “You win!" and exit. This program has a bug! See Demo. 3 -15
Exercise 3: Testing Please design test cases for this program? • How many cases? • Purpose of each case? • What are the input and expected output of each case?
Loop Pitfalls: Misplaced ; • Watch the misplaced ; (semicolon) while (response != 0) ; { cout << "Enter val: "; cin >> response; } • Notice the "; " after the while condition! Result here: INFINITE LOOP! 2 -17
Loop Pitfalls: Infinite Loops • Loop condition must evaluate to false at some iteration through loop – If not infinite loop. – Example: while (1) { cout << "Hello "; } – A perfectly legal C++ loop always infinite! • Infinite loops can be desirable – e. g. , "Embedded Systems" 2 -18
The break and continue Statements • Flow of Control – Recall how loops provide "graceful" and clear flow of control in and out – In RARE instances, can alter natural flow • break; – Forces loop to exit immediately. • continue; – Skips rest of loop body • These statements violate natural flow – Only used when absolutely necessary! 2 -19
Programming Exercise 4 • Ask a user to enter a positive integer. • Input: If the value is a negative one, the program repeatedly makes the user type in another value until a legal value is entered. • Output: Print a triangular pattern with side n. Its horizontal side is at the top and its vertical side is at the right. • For example, if the user enters 4, your program should print the following: **** ** * 3 -20
Summary • Boolean Expressions – Building, Evaluating & Precedence Rules • Branching Mechanisms – if-else – switch – Nesting if-else • Loops – While, do-while, for – Nesting loops 2 -21
- Slides: 21