ng, Looping, Looping, Loopin - Virginia Tech

ng, Looping, Looping, Loopin - Virginia Tech

Chapter 6 Loops A control structure that causes a statement or group of statements to be executed repeatedly There are 3 types of loops while for

do while The While Statement while (expression) Statement while (inputVal != 25) cin >> inputVal;

while and if Not the same construct If statements do not repeat While statements will repeat until the expression becomes false While statements should always have a statement that will eventually make the expression to become false

Phases of Loop Execution Loop entry The point at which of the low of control reaches the first statement inside a loop. Iteration An individual pass through or repetition of, the body of a loop. Loop test The point at which the While expression is evaluated and a decision is made either to begin a new iteration or skip to the statement immediately following the loop.

Loop exit the point at which the repetition of the loop ends and control passes the first statement following the loop. Termination condition The condition that causes the loop to be exited. Loops using the While Statement Count-Controlled Loops Event-Controlled Loops

Sentinel-Controlled Loops End-of-File Controlled Loops Flag-Controlled Loops Count Controlled Loops loopCount = 1; while (loopCount <= 10) { cout << Hello!\n;

loopCount++; } Sentinel-Controlled Loops while(!(month == 2&&day == 31)) { cin >> month >> day; }

Sentinel-Controlled Loops cin >> month >> day; while(!(month == 2&&day == 31)) { cin >> month >> day; }

Sentinel-Controlled Loops cin >> month >> day; while(!(month == 2&&day == 31)) { cin >> month >> day; } End-of-File Controlled Loop

cin >> month >> day; while ( cin ) { cin >> month >> day; } End-of-File Controlled Loop cin >> month >> day;

while ( !cin.eof() ) { cin >> month >> day; } Flag-Controlled Loop sum = 0; cin >> number;

bool nonNegative = true; while ( nonNegative ) { if ( number > 0) sum = sum + number; else nonNegative = false; cin >> nonNegative; }

Quiz Write a code fragment that uses a loop to compute the sum of the squares of a series of numbers from 1 to a user entered number. Upon loop exit the sum of the squares should be printed. Looping Subtasks

Counting Summing Keeping track of the previous value Counting Loop Iteration Counter: A counter variable that is incremented with each iteration of a loop

count = 0; cin.get( inChar ); while ( inChar != . ) { count++; cin.get( inChar ); } Summing Loop

sum = 0; cin >> number; bool nonNegative = true; while ( nonNegative ) { if ( number > 0) sum = sum + number; else nonNegative = false;

cin >> nonNegative; } Remembering Loop count = 0; in.get ( prevChar ); in.get ( currChar ); while ( !in.eof() ) {

if ( currChar == = && prevChar == ! ) count++; prevChar = currChar; in.get( currChar ); } Designing Loops What is the condition that ends the loop? How should the condition be initialized?

How should the condition be updated? What is the process being repeated? How should the process be initialized? How should the process be updated? What is the state of the program on exiting the loop?

Recently Viewed Presentations

  • Welcome [www.thestreamingguys.com.au]

    Welcome [www.thestreamingguys.com.au]

    Wilkie v National Storage Operations Pty Ltd [2013] FCCA 1056 - employed as manager at storage facility but was transferred to another site in Assistant Manager role. Suspension with pay during investigation, resulting in employee being issued with written warning.
  • An overview DR MONIQUE MACKENZIE & PROF NORMAN

    An overview DR MONIQUE MACKENZIE & PROF NORMAN

    Dr monique Mackenzie & Prof normanowen-smith. Centre for research into ecological and environmental modelling (CREEM) Senior lecturer in mathematics and statistics. University of st Andrews, UK. The next few minutes. Objectives of the meeting.
  • Secrets for Approaching Bare-metal Performance With Real-time Nfv

    Secrets for Approaching Bare-metal Performance With Real-time Nfv

    In this presentation we Redhat and Sonus Networks are here to to talk about the advanced performance tuning configurations in both the OpenStack host and guest VM to achieve maximum performance and also share the learnings and best practices from...
  • PowerPoint-Präsentation

    PowerPoint-Präsentation

    R (review) Das Gelesene wiederholen 3. Gliederung erstellen Aufbau der Arbeit festhalten: chronologisch, Ursache-Wirkung, vom Allgemeinen zum Besonderen, vergleichend oder gegenüberstellend, induktiv (vom Beispiel zur Theorie), deduktiv (von der Theorie zum Beispiel) Inhalt der einzelnen Kapitel grob festhalten Vorläufige Gliederung...
  • European RMM - Fitac

    European RMM - Fitac

    IATA was founded the next year with a focus on working with ICAO to setup international standards for aviation. In the early days, IATA played an important role in facilitating fare setting and in establishing settlement systems for interline networks....
  • Faculty Work-Family Issues: Finding the Balance at a Liberal ...

    Faculty Work-Family Issues: Finding the Balance at a Liberal ...

    Faculty Work-Family Issues: Finding the Balance at a Liberal Arts College SUZANNE AMADOR KANE, Physics Department, Haverford College MARISSA GOLDEN, Political Science Department, Bryn Mawr College
  • CSC461: Lecture 13 Coordinates Objectives Introduce concepts such

    CSC461: Lecture 13 Coordinates Objectives Introduce concepts such

    Introduce concepts such as dimension and basis ... Can't answer without a reference system World coordinates Camera coordinates Coordinate Systems Consider a basis v1, v2,…., vn A vector is written v=a1v1+a2v2+….+anvn The list of scalars {a1, a2, …. an}is the...
  • Regional Association Overview ZIMS at the Regional Association

    Regional Association Overview ZIMS at the Regional Association

    Over time many Associations change their name. The Synonym grid. will display any of these previous names. If anyone searches by the. synonym they will be directed to your institution. This list is. maintained by Species360.