do/while Selection Structure

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
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.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Fundamental Programming Fundamental Programming for Loops.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer Programming -1-
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
for Repetition Structures
Intro to Programming Week # 6 Repetition Structure Lecture # 10
CS150 Introduction to Computer Science 1
Chapter 2.2 Control Structures (Iteration)
Chapter 8 Repetition Statements
Repetition Statements
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
switch Selection Structure
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
Let’s all Repeat Together
Fundamental Programming
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Functions Divide and Conquer
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
Life is Full of Alternatives Part 3
Reading from and Writing to Files
Presentation transcript:

do/while Selection Structure 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time We Learnt about the switch selection structure Learn about ASCII values Learn about cin.get() Today we will Introduce the do/while repetition structure 10/14/05 CS150 Introduction to Computer Science 1

do/while Repetition Structure What repetition structures have we covered so far? do/while is another repetition structure Useful when the test happens at the end of the loop 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 do/while Loops do { cout << “Enter a year:” << endl; cin >> year; } while (year < 0); 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 When to use do while? When loop must execute at least once Perfect for data validation! Post-tested loop General format: do { statements; } while (condition is true); 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example 14.1 Write a program segment that takes as input a number between 5 and 10. Error proof the segment 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What’s the output? 14.2 m = 10; do { cout << m << endl; m = m - 3; } while (m > 0); 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Rewrite 14.3 num = 10; while (num <= 100) { cout << num << endl; num += 10; } 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Rewrite 14.4 for (n = 3; n > 0; n--) cout << n << “ squared is” << n*n << endl; 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the Output? 14.5 int counter = 1; do { cout << counter << " "; } while( ++counter <= 10 ); } while( counter++ <= 10 ); 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the Purpose? 14.6 char ch; do { cout << "Enter a valid code (h,c,i): "; cin >> ch; } while ((ch != 'h') && (ch != 'c') && (ch != 'i')); 10/14/05 CS150 Introduction to Computer Science 1

Rewrite as a do-while Loop 14.7 int i; for(i = 0; i <= 50; i++) sum += i; 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Nested Loops A loop within a loop Can repeat multiple things within a loop Example: Read in 10 grades for each of 20 students How would we write loops to do this? 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the Output? 14.8 for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) cout << “*”; cout << endl; } 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the Output? 14.9 for (int i = m; i > 0; i--) { for (int j = n; j > 0; j--) cout << “*”; cout << endl; } 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 14.10 What is the Output? cout << setw(4) << "|"; for( int head = 0; head <= 10; head++ ) cout << setw(3) << head; cout << endl; for( int i=0; i< 3*12+1; i++ ) cout << "-"; for( int row = 0; row <= 10; row ++ ) { cout << setw(3) << row << "|"; for( int col = 0; col <= 10; col++ ) cout << setw(3) << row * col; } 10/14/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered Introduce the do/while repetition structure Readings P. 120 - 121 do/while repetition structure 10/14/05 CS150 Introduction to Computer Science 1