1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail.

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

Computer Science 1620 Loops.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Smoking is prohibited 1 CS 101 Second Exam Review Prepared by Dr. Amer Al-Badarneh 
Chapter 5: Control Structures II (Repetition)
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Help session - C++ Arranged by : IEEE – NIIT Student Chapter.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4 Program Control Statements
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Chapter 4 Selection Structures: Making Decisions.
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.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
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.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 05 (Part III) Control Statements: Part II.
Control Structures Repetition or Iteration or Looping Part II.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Control Structures RepetitionorIterationorLooping Part I.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
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.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Computer Programming -1-
Control Structures Repetition or Iteration or Looping Part II.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
Controlling execution - iteration
CS1010 Programming Methodology
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Intro to Programming Week # 5 Repetition Structure Lecture # 9
TOPIC 4: REPETITION CONTROL STRUCTURE
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
do/while Selection Structure
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 4 Repetition Structures
Presentation transcript:

1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail

2 The for Loop Repetition –construct to execute a section of code a specified number of times for (initialization; test-condition; increment) { statement(s) } (1) Set initial value of counter. (2) Perform test to see if loop should continue. (3) Execute statements. (4) Update counter. (5) Repeat from (2). #include int main() { int count;//loop control variable for (count=1; count<=100; count++) cout << count << " "; return 0; }

3 The for Loop Output? for (count=10; count < 5; count++) cout << count; A square root calculating program #include #include // for older compilers, use int main() { int num; for(num=1; num < 100; num++) { cout << num << " " << sqrt((double) num) << '\n'; } return 0; }

4 Variations of the for Loop int main() { for(int i=100; i >= -100; i = i-5) cout << i << ' '; return 0; } ================================ for(x=0, y=10; x<=10; ++x, --y) cout << x << ' ' << y << '\n'; ================================ #include int main() { int i; // print numbers until a key is pressed for(i=0; !kbhit(); i++) cout << i << ' '; return 0; }

5 Variations of the for Loop int x; for(x=0; x != 123; ) { cout << "Enter a number: "; cin >> x; } ================================ int x = 0; for( ; x<10; ) { cout << x << ' '; ++x; } ================================ for(;;) { body of loop }

6 The while Loop Repetition construct –it’s a for loop stripped of the initialization and update parts while (test-condition) { statement(s) }  for ( ;test-condition; ) { statement(s) }  initialize counter while (test-condition) {statement(s); update counter; }

7 The while Loop Might use it if do not know beforehand when to stop repeating int n = 99; //why initialize? cout << “Guess my fav number”; while(n != 0) cin >> n; cout << “Yes, I was looking for 0 \n”; ================================ unsigned char ch; ch = 1; while(ch) {//when does it terminate? cout << ch; ch++; }//displays what?

8 The do-while Loop Similar to while, except that tests condition after the body of the loop i.e. statements executed at least once do { statement(s); } while (test-condition); ================================ int num; do { cout << "Enter a number (100 to stop): "; cin >> num; } while (num != 100);

9 Which Loop to Use? if (number of repetitions known) use for; else if (body to be executed once at least) use do while; else {use while;} No hard rule; all interchangeable mostly; matter of style

10 The continue Statement continue, break and goto are jump statements. continue used in loops and causes program to skip rest of loop’s body and start the next iteration immediately. Actually takes you to the closing brace of the loop body long dividend, divisor; char ch; do { cout > dividend; cout > divisor; if (divisor == 0) //divide by zero error { cout << "Illegal divisor\n"; continue; } cout << "Quotient is " << dividend/divisor; cout << ", remainder is " << dividend%divisor; cout << "\nDo another? (y/n): "; cin >> ch; } while (ch != 'n');

11 The continue Statement Output of following? #include int main() { int x; for(x=0; x<=100; x++) { if(x%2) continue; cout << x << ' '; } return 0; }

12 The break Statement Causes exit from loop or switch #include int main() { double x; while (1) { cout << "Enter number for square root: "; cin >> x; if (x <0.0) break; //exit loop if x is -ve cout << sqrt(x) << endl; }// break jumps to here cout << "Sorry, can't do that\n"; return 0; }

13 The break Statement int t; for(t=0; t<100; t++) { if(t==10) break; cout << t << ' '; } ================================ for(i=0; i<1000; i++) { // do something if(kbhit()) break; }// keypress stops execution break causes exit from the innermost loop or switch, and not from the enclosing loops or switches

14 The goto Statement Unconditional branch statement that jumps to a label (identifier) goto label; … label: statement(s); ================================ int x = 1; loop1: cout << x << endl; x++; if(x < 100) goto loop1; ================================ goto end; int i = 10; //error: goto skips initialization end: ;

15 Example Read and understand the magic number program given at the end of the chapter 4

16 while #include int main() { int count_down; cout << "How many greetings do you want? "; cin >> count_down; while (count_down > 0) { cout << "Hello "; count_down = count_down - 1; } cout << endl; cout << "That's all!\n"; return 0; }

17 #include int main() { char ans; do { cout << "Hello\n"; cout << "Do you want another greeting?\n" << "Press y for yes, n for no,\n" << "and then press return: "; cin >> ans; } while (ans == 'y' || ans == 'Y'); cout << "Good-Bye\n"; return 0; } do.. while

18 Counting Space Characters (while) void countSpaces1(void) { int ch = 0; int numofspaces = 0; printf("Enter a sentence: "); ch = getchar(); while ( ch != '\n' ) { if ( ' ' == ch ) numofspaces++; ch = getchar(); } cout << "\nThe number of spaces is: " << numofspaces << endl; }

19 Counting Space Characters (do..while) #include // getchar() void countSpaces2(void) { int ch = 0; int numofspaces = 0; printf("Enter a sentence: "); do { ch = getchar(); if ( ' ' == ch ) numofspaces++; } while ( ch != '\n' ); cout << "\nThe number of spaces is: " << numofspaces << endl; }

20 Make Number from Digits #include // getchar() #include // isdigit() void makeInteger(void) { int num=0; int digit=0; printf("Enter a number: "); digit = getchar(); for( ; isdigit(digit) ; digit=getchar() ) { num = num * 10; num = num + (digit - '0'); } cout << "The number is: " << num << endl; }

21 Skip Spaces #include // header file for isspace() for (int = getchar(); isspace( c ) ; c = getchar() ) ; ungetc( c, stdin );// put the non-space char // back in the input buffer

22 Skip Spaces #include // getchar() #include // isdigit(), isspace() void skipSpaces(void) { int c = 0; int count = 0; cout << "\nSkipping Spaces\n\n\n"; cout.flush(); for ( c=getchar(); isspace(c); c=getchar() ) { count++; } ungetc(c, stdin); cout << count << " spaces skipped\n"; } What is the behavior of this program What is the function of cout.flush() What will happen if it is removed What does ungetc() do