1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

CMPUT 101 Lab # 5 October 22, :00 – 17:00.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 6 Control Structures.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 CS 105 Lecture 6 While Loops Wed, Feb 16, 2011, 5:13 pm.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
The If/Else Statement, Boolean Flags, and Menus Page 180
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
CONTROLLING PROGRAM FLOW
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
1 Chapter 9 Additional Control Structures Dale/Weems.
The for Loop Syntax: for ( expression1 ; condition ; expression2 ) statement ; for ( expression1 ; condition ; expression2 ) { statement ; } Same as: expression1.
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 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.
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.
Announcements Exam 1 Tuesday July minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 CS161 Introduction to Computer Science Topic #8.
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 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
REPETITION CONTROL STRUCTURE
Chapter 2.2 Control Structures (Iteration)
לולאות קרן כליף.
Announcements Exam 1 Grades Posted on Blackboard.
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Repetition Control Structure
Announcements Exam 1 Grades Posted on Blackboard.
Chapter 2.2 Control Structures (Iteration)
The for Loop Syntax: Same as: for (expression1;condition; expression2)
Summary Two basic concepts: variables and assignments Basic types:
Control Structures Part 1
Let’s all Repeat Together
Statements and flow control
do/while Selection Structure
Repetition Statements (Loops) - 2
(Dreaded) Quiz 2 Next Monday.
Announcements Exam 1 Grades Posted on blackboard Average: 83.26
Presentation transcript:

1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm

2 Exam 1 16 Scores: Avg 73.8 Std dev 14.1

3 Syntax  while (condition) statement;  With multiple statement body:  while (condition) { statement; statement;} The while Loop

4 Syntax:  for (expression1; condition; expression2) statement;  for (expression1; condition; expression2){  statement;  } Same as:  expression1;while (condition){ statement; expression2;} The for Loop

5 Example for Loop int numEmployees,curNum; cout << “Enter Number of Employees: “; cin >> numEmployees; if (numEmployees > 0) { for (curNum = 0; curNum < numEmployees; curNum++) { cout << “Welcome to CorpLand!” << endl; } else { cout << "Egads!! Negative number of employees??" << endl; }

6 Looping for Input char letterEntered = ‘A’; while (letterEntered != ‘Z’) { cout << “Enter a letter: “; cin >> letterEntered; }

7 Looping for Input char command; cout << "Welcome to our program!!" << endl << "We'll process a sequence of commands" << endl << "Enter command (or Q to quit): "; cin >> command; while (command != 'Q') { //... do stuff for this command... cout << “Enter command (or Q to quit): “; cin >> command; }

8 Looping until Flag boolean noMoreData(); boolean done = false; … while (!done) { … // do a bunch of stuff … if (noMoreData() == true) done = true; } cout << “We’re all through – thank you!”

9 Nested Loops int i, j; for (i=0; i < 5; i++) { for (j=0; j < 6; j++) { cout << i << j << “ “; } cout << endl; }

10 Nested Loops int i, j; for (i=0; i < 5; i++) { for (j=0; j < i; j++) { cout << i << j << “ “; } cout << endl; }

11 Syntax  do statement;while (condition);  do{ statement; statement;}while (condition); Do-while Loop

12 do-while Example int inputNum = 0; do { //... do something with inputNum... cout << “Please Input Number, –1 to quit: “; cin >> inputNum; } while (inputNum != -1);

13 What Is This? while (stringEntered != “apple”) { cout << “Enter a red fruit: “; cin >> stringEntered; } while (stringEntered != “banana”) { cout << “Enter a yellow fruit: “; cin >> stringEntered; } while (stringEntered != “pomegranate”) { cout << “Enter a random fruit: “; cin >> stringEntered; }

14 What Is This? for (i = 0; i < 10 ; i++) cout << “Interesting, isn’t it?” << endl; while (answer != ‘D’) { cout << “Enter an answer: “; cin >> answer; } while (answer != ‘D’) { cout << “Enter an answer: “; cin >> answer; }

15 What Is This? void kingsChair(int loopCounter) { int num; for(num = 0; num < loopCounter; num++) cout << “AAAAAAAAAAAAAAAAAAAAAAAAAA” << endl; }