Repetition Statements (Loops) - 2

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

While loops.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
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.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
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.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
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 © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Follow up from lab See Magic8Ball.java Issues that you ran into.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Lecture 4b Repeating With Loops
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Unit 3 Lesson 9 Repetition Statements (Loops)
Chapter 5: Control Structures II (Repetition)
CS161 Introduction to Computer Science
Lecture 7: Repeating a Known Number of Times
Control Structures II (Repetition)
Chapter 5: Loops and Files.
Chapter 5: Repetition Structures
Chapter 2.2 Control Structures (Iteration)
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 4B More Repetition Richard Gesick
Control Structures - Repetition
Chapter 4 Repetition Structures
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Looping and Repetition
Iteration with While You can say that again.
Repetition Statements
Introduction to Object-Oriented Programming with Java--Wu
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
Control Structures Part 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Repetition Statements (Loops) - 2 www.hndit.com

The while loop www.hndit.com The while loop repeats a statement or group of statements as long as a control expression is true. Unlike a for loop, a while loop usually does not use a counter variable. The control expression in a while loop can be any valid expression. The program in Code List uses a while loop to repeatedly divide a number by 2 until the number is less than or equal to 1.

Code List #include <iostream.h> void main ( ) { float number; www.hndit.com Code List #include <iostream.h> void main ( ) { float number; cout << “Please enter the number to divide:”; cin >> number; while (number > 1.0) cout << number << endl; number = number / 2.0; }

While Loops General form: while (<Boolean expression>) www.hndit.com General form: while (<Boolean expression>) <statement> The parentheses around the Boolean is required. If the condition is true the body of the loop is executed again. If the loop condition is false, the program continues with the first statement after the loop. A while loop may not be executed… why?

Syntax and Semantics of while Statements www.hndit.com while (<Boolean expression>) <statement> { <statement 1> . <statement n> } ? statement true false

While Loops: Discussion www.hndit.com While Loops: Discussion The condition can be any valid Boolean Expression The Boolean Expression must have a value PRIOR to entering the loop. The body of the loop can be a compound statement or a simple statement. The loop control condition needs to change in the loop body If the condition is true and the condition is not changed or updated, an infinite loop could result. If the condition is true and never becomes false, this results in an infinite loop also.

While Tests Before the Loop www.hndit.com While Tests Before the Loop In a while loop, the control expression is tested before the statements in the loop begin. Figure shows a flowchart of the program in Code List. If the number provided by the user is less than or equal to 1, the statements in the loop are never executed.

www.hndit.com Figure

www.hndit.com Figure Comparison of a for loop with a while loop to accomplish the same task in a count controlled loop.

The while Loop Accumulator www.hndit.com Write code that computes the sum of the numbers between 1 and 10. int counter = 1; int sum = 0; while (counter <= 10) { sum = sum + counter; counter = counter + 1; }

Sentinel Values and Counters www.hndit.com Sentinel Values and Counters Sentinel Value A value that determines the end of a set of data, or the end of a process in an indefinite loop. While loops may be repeated an indefinite number of times. It is common to count the number of times the loop repeats. Initialize this “counter” before the loop Increment the counter inside the loop

Errors with while Loops www.hndit.com Errors with while Loops Do NOT place a ; (semicolon) directly after the command while in a while loop: int counter = 1; while(counter <= 10) ; //Don’t do this! { cout << counter << end1; counter ++; } This will prevent any lines of code within the loop from being repeated or iterated. This will result in a logic error, the compiler will NOT tell you that there is a syntax error. This could also result in an infinite loop.