Repetition Control Structure

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is 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.
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.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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.
Chapter 5: Control Structures II (Repetition)
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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.
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.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
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 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.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
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.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Computer Programming -1-
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CS161 Introduction to Computer Science
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 8 Repetition Statements
Additional Control Structures
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Repetition Control Structure B.Ramamurthy Chapter 3 1/10/2019 B.Ramamurthy

Introduction Many application require certain operations to be carried out more than once. Such situations require repetition in control flow. In C++ repetition in execution can be realized using a “while”, “do-while” and a “for” statement. 1/10/2019 B.Ramamurthy

Topics for discussion Repetition structure (loop) design while loop : syntax, semantics, example Loop control for loop : syntax, semantics, example do-while : syntax, semantics, example Case Study 1 nested loops Case Study 2 Summary 1/10/2019 B.Ramamurthy

while loop : syntax while ( condition ) statement “condition” is a logical expression that evaluates to true or false. It could be a relational or Boolean expression. “statement” could a single statement or more than one statement bounded by { }. It is often referred to as the body of the loop. 1/10/2019 B.Ramamurthy

while loop : semantics 1) The condition is evaluated. 2) If it is true, then the body of the loop is executed. Then the control is transferred back to the condition for re-evaluation. 3) If the logical expression is false, the while loop is exited and control is transferred to the statement after the while statement. 1/10/2019 B.Ramamurthy

The while statement while (condition) statement; { statement block } yes no while (condition) statement; { statement block } 1/10/2019 B.Ramamurthy

while loop : example Problem: Write statements to determine the sum to n natural numbers. Print out the computed sum. int sum = 0; while (n > 0) { sum = sum + n; n = n - 1; } cout << “Sum of first ” << n << “ natural numbers is ” << sum << endl; 1/10/2019 B.Ramamurthy

Loop Design Loop design should consider: Initialization of conditions (sum = 0) Termination of the loop (when n == 0) Testing (at the top or bottom of the loop) (at the top , n > 0) Updating conditions ( n = n -1) Of course, the body of the loop. (sum = sum + n; n = n -1;) Body of the loop: Statements representing the process to be repeated. These are statements within the scope of a loop. 1/10/2019 B.Ramamurthy

More about “while” Initialize the conditions which are evaluated in the “while”. Conditions are evaluated at the “top” of while statement or before the execution of the body. “while” body is executed 0 or more times. Updating of the conditions are done inside the body of the “while”. Use “while” when the number of times a loop is executed is dependent on some condition set during execution of the body. Example: Input a list of positive number. The list is terminated by a negative number. 1/10/2019 B.Ramamurthy

Example Write statements that takes as input n, the side of a square and print and square of size n with asterisks. //PRE: N is a value between 2 and 20 //Output is *** 1/10/2019 B.Ramamurthy

Do-while - syntax Use this control structure: When a loop needs to be executed at least once. When the testing of the conditions needs to be done at the bottom. do statement while ( condition ); 1/10/2019 B.Ramamurthy

do-while semantics 1) Execute the statement. 2) Evaluate the expression. If it is TRUE then proceed to step 1) else exit the loop. NOTE: do-while is executed at least once. 1/10/2019 B.Ramamurthy

The do/while statement yes no do statement; while (condition) { statement block } while (condition) 1/10/2019 B.Ramamurthy

Example for do-while Usage: Prompt user to input “month” value, keep prompting until a correct value of moth is input. do { cout <<“Please input month {1-12}”); cin >> month; } while ((month < 1) || (month > 12)); 1/10/2019 B.Ramamurthy

For loop - syntax for (initialize exp; test exp; update exp) statement initialize exp : done only once at the start test exp: This is a condition that evaluates to TRUE or FALSE. update exp: This specifies how to update condition. Note: for loop is used when the number of times to be repeated is fixed/known apriori. 1/10/2019 B.Ramamurthy

For loop - Semantics 1) Initialize exp is executed. 2) Test exp is evaluated. If it is TRUE , body of for is executed else exit for loop; 3) After the execution of the body of the loop, update exp is executed to update condition; go to Step 2 above. 1/10/2019 B.Ramamurthy

The for statement initalize test increment/ decrement true statement(s) statement(s) 1/10/2019 B.Ramamurthy

For loop - example Trace the execution of the “for loop” specified. j = 10; for (i = 0; i < 3; i++) { cout<< “i = “ << i << “j = “ << j; j = j - 2; } 1/10/2019 B.Ramamurthy

For - Examples Problem 1: Write a For statement that computes the sum of all odd numbers between 1000 and 2000. Problem 2: Write a For statement that computes the sum of all numbers between 1000 and 10000 that are divisible by 17. Problem 3: Printing square problem but this time make the square hollow. 1/10/2019 B.Ramamurthy

Summary Loop design Repetition control structures: while, for, do-while Nested loops Beware of infinite looping problem Read chapter 3. 1/10/2019 B.Ramamurthy