Copyright 2004-2005 Curt Hill A Quick Introduction to Looping Breadth not Depth.

Slides:



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

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
Chapter 5: Control Structures II (Repetition)
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.
Chapter 5: Control Structures II (Repetition)
CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
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.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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,
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 5: Control Structures II
Fundamental Programming Fundamental Programming for Loops.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
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.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © Curt Hill The C++ IF Statement The most important decision statement Part 1.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © Curt Hill Flow of Control A Quick Overview.
Batch Files Flow of Control to Strengthen Copyright © by Curt Hill.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
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.
The for Statement A most versatile loop
Flow of Control An Overview
REPETITION CONTROL STRUCTURE
while Repetition Structure
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 4 Control Structures I: Selection
Quick Test What do you mean by pre-test and post-test loops in C?
Control Structures II (Repetition)
Repetition-Counter control Loop
Iteration with While You can say that again.
Introduction to Programming
The most important decision statement
Compound Statements A Quick Overview
Computing Fundamentals
Lecture Notes – Week 4 Chapter 5 (Loops).
The Java switch Statement
CprE 185: Intro to Problem Solving (using C)
Chapter 5: Control Structures II (Repetition)
Repetition Statements (Loops) - 2
CprE 185: Intro to Problem Solving (using C)
While and Do While Syntax, semantics and examples
Presentation transcript:

Copyright Curt Hill A Quick Introduction to Looping Breadth not Depth

Copyright Curt Hill Flow of Control in C/C++ Recall the flow statements in C/C++: –Decisions If Switch Case –Loops for while do break Here we briefly consider the loops

Copyright Curt Hill Flowchart Representation Sequential DecisionIteration

Copyright Curt Hill Starting Example Suppose we want to average three numbers In a console program we could use the following code: double a,b,c; cin >> a >> b >> c; double avg = (a+b+c)/3; This works for three, but is it the best way for fifty?

Copyright Curt Hill The Better Way Is a loop –Do the same thing over and over C++ has three loops: –while –for –do while One way to solve this problem is with a while –There are others as well

Copyright Curt Hill The While Equivalent Instead we use the following code: int count = 0; double sum = 0, temp; while(count > temp; sum += temp; count++; } double avg = sum/50; This works for fifty values and is a lot less code

Copyright Curt Hill While Loops A general loop Not always the best choice Execute a statement while a test is true Pieces: –A reserved word – while –A parenthesized condition –The body of the loop – one statement

Copyright Curt Hill A General Thought Loops always have several pieces which we see in the above example An initialization –Setting count and sum to zero A test –Count less than 50 The body of the loop –Reading the value and summing it An advancement –Incrementing count Different loops handle these in different ways

Copyright Curt Hill Example Revisited The above is not the best example of a while A for would do it in a nicer way: double sum = 0,temp, count; for(count=0;count > temp; sum += temp; } double avg = sum/50;

Copyright Curt Hill For Considered A counting loop Several pieces: Reserved word – for Parenthesized controls –An initialization –A test –An advancement –These are separated by semicolons One statement –May also be a compound statement

Copyright Curt Hill For Action The initialization is executed only once and is done first Next comes the test The statement is then executed if the test is true The advancement is then done Then go back to the test

Copyright Curt Hill More for thoughts In the C family of languages the for is the most general of loops We can replace any while with a for We usually use a while when the advancement is not a counting operation or not at the end of the body Thus we use more for statements than any other loop

Copyright Curt Hill While Revisited Suppose a cash register machine program We take in the purchase price and the amount the customer has given us Compute the change Make the change in terms of numbers of bills and coins the teller has to give Try first part of this with a while

Copyright Curt Hill Cash Register Program double charge,tender; cin >> charge >> tender; double change = tender-charge; int twenties = 0; while(change >= 20.00){ change -= 20; twenties++; } if(twenties>0){ cout << “Give them” << twenties << “ twenties.”; }

Copyright Curt Hill Example Revisited The loop would be duplicated for tens, fives, etc. Both the while and for are leading decision loops –They may execute the body of the loop zero times, if the condition is false –This is most common form of loop –This is desirable here, we may not give them one of some bill denomination Leading decision loops must initialize before the first test

Copyright Curt Hill Another Situation Sometimes we end up in duplication in initialization and body of loop Consider the case of reading in the age of a hospital patient, that we attempt to verify int age; cin >> age; while(age 120){ cout > age; }

Copyright Curt Hill Another Loop This type of situation may be best handled with a trailing decision loop This checks the condition at the end of the loop rather than the beginning Particularly handy for when the initialization is part of the loop processing C/C++’s trailing decision loop is called the do while

Copyright Curt Hill do while loop Try this one more time: int age; do { cout >> “Enter age”; cin >> age; } while(age 120); The initialization is part of the loop itself

Copyright Curt Hill Flowchart Representation Leading decision loopTrailing decision loop

Copyright Curt Hill Determinism The first for and first while were deterministic –Before we executed the test the first time we could compute exactly how many times through the loop would be executed –Counting loops usually deterministic The last while and do while were non- deterministic –There is no way to predict the number of times through the loop –Often use whiles or do whiles for non- deterministic loops

Copyright Curt Hill Conclusion This presentation has focused on comparisons not details or syntax Next we consider the individual loops and their details –for –while –do while