Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University

Similar presentations


Presentation on theme: "COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University"— Presentation transcript:

1 COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu Some slides are adapted from notes by Dr. Walter Savitch (UCSD)

2 The switch Statement A new stmt for controlling multiple branches Uses controlling expression which returns bool data type (true or false) Syntax: – Display page 62 next slide 2-2 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

3 The switch Statement in Action: Display, page 64 2-3Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

4 The switch: multiple case labels Execution "falls thru" until break – switch provides a "point of entry" – Example: 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-4 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

5 Programming Exercise 1 A user enters the ‘day’ (1-7) of a week. 1 is Sunday, 2 is Monday … If user enters 1 or 7, then your program displays “This is a weekend day”. Otherwise, if user enters 2 – 6, your program shows “This is a weekday. Otherwise, ?????? Use switch-case statements 3-5

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-6 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

7 while Loop Example Consider: count = 0;// Initialization while (count < 3)// Loop Condition { cout << "Hi ";// Loop Body count++;// Update expression } 2-7 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Loop body executes how many times?

8 do-while Loop Syntax 2-8Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Often used for menu applications. Why?

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-9 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

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-10 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

11 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. 3-11

12 Programming Exercise 3 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-12

13 Loop Pitfalls: Misplaced ; Watch the misplaced ; (semicolon) – Example: while (response != 0) ;  { cout > response; } – Notice the ";" after the while condition! Result here: INFINITE LOOP! 2-13 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

14 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-14 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

15 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-15 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

16 Programming Exercise 4 Ask a user to enter a positive integer. If the value is not legal, the program repeatedly makes the user type in another value until a legal value is entered. 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-16

17 Summary Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch – Nesting if-else Loops – While, do-while, for – Nesting loops 2-17 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.


Download ppt "COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University"

Similar presentations


Ads by Google