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

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Chapter 4: Control Structures II
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A First Book of C++ Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2 Flow of Control Copyright © 2016 Pearson, Inc. All rights reserved.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Topic 4: Looping Statements
Review 1.
REPETITION CONTROL STRUCTURE
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Programmer-Defined Functions Dr.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Chapter 2.2 Control Structures (Iteration)
Lecture 2-3 C++ Basic Constructs
Chapter 5: Control Structures II
Structured Program Development in C
Control Statements Loops.
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
Chapter 5: Control Structures II (Repetition)
Control Statements Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University Some slides are adapted from notes by Dr. Walter Savitch (UCSD)

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.

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

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.

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

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.

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?

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

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.

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.

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

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

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.

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.

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.

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

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.