Repetition and Loop Statements

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

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.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
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)
C++ Programming: Program Design Including Data Structures, Fourth Edition 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.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
Computer Programming -1-
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
REPETITION CONTROL STRUCTURE
while Repetition Structure
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
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.
Lecture 07 More Repetition Richard Gesick.
Computer Science 101 While Statement.
Lecture 4B More Repetition Richard Gesick
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.
Chapter 5 Repetition.
Chapter 4 Control structures and Loops
Control Structure Senior Lecturer
Outline Altering flow of control Boolean expressions
Lesson #5 Repetition and Loops.
Loops (iterations, repetitions)
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Control Statements Loops.
Chapter 5: Control Structures II (Repetition)
Repetition Statements (Loops) - 2
Control Statements Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 4 Repetition Structures
ICS103: Programming in C 5: Repetition and Loop Statements
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Repetition and Loop Statements

GCD of Two Numbers The greatest common divisor (GCD) of two integers is the product of the integers’ common factors. Write a program that inputs two numbers and find their GCD by repeated division. For example, consider the numbers 252 and 735. find the remainder of one divided by the other.

GCD of Two Numbers Now we calculate the remainder of the old divisor divided by the remainder found Repeat the process until remainder is zero The Divisor when remainder is zero is the GCD 21 is the GCD

GCD problem Input Output Logic Involved Two numbers GCD of the numbers Euclidean algorithm, binary GCD algorithm, repeated division method

Algorithm to Find GCD Step 1: Read the numbers from the user Step 2: Let dividend = number1 and divisor = number2 Step 3: Repeat step 4 to step 6 while remainder not equal to zero Step 4: remainder = number1 modulus number2 Step 5: dividend = divisor Step 6: divisor = remainder Step 7: GCD = divisor Step 8: print GCD

Weather Report Weather station in Chennai has recorded rainfall of first ten days in December 2015 using a rain gauge. Develop an algorithm and write a program to count and print the number of days in which rainfall was low, medium and high. The value given is in cm. If the value is less than 12 then it is low, 12 to 20 is medium and more than 20 is high.

Weather problem Input Output Logic Involved Rainfall in cm recorded for ten days, ten numbers Number of days low, medium and high rainfall Count the number of days with rainfall as low, medium and high

Algorithm for Weather Problem Step 1: Let counter =0, num_Of_Low = 0, num_Of_Med = 0, num_Of_High = 0 Step 2: If counter == 10 then goto Step 9 Step 3: Read rainfall recorded in cm Step 4: increment counter Step 5: if rainfall < 12 then increment num_Of_Low Step 6: if rainfall is between 12 and 20 then increment num_Of_Med Step 7:Step 5: if rainfall > 20 then increment num_Of_High Step 8: Goto Step 2 Step 9: Print num_Of_Low, num_Of_Med, num_Of_High

Implementation We have to learn how to repeat statements In some cases the number of times to repeat a statement is known, in weather report example it is ten times we have to repeat some statements In some other cases the conditions are not direct as a number but as a terminating condition that may be based on I/O. In our GCD problem, the statements are to be repeated till reminder becomes zero

Counter-controlled loop Used when we can determine prior to loop execution exactly how many loop repetitions will be needed to solve the problem A counter-controlled loop follows this general format: Set loop control variable to an initial value of 0 while (loop control variable < final value) . . . Increase loop control variable by 1

Syntax of a For loop for (loop control variable initialization; loop terminating condition; loop control variable update) All three components are optional But semicolons are mandatory

GCD Program Using While

GCD Program Using For

Weather Program Using While

Weather Program Using For

Do while Loop Similar to a while loop, except the fact that it is guaranteed to execute at least one time Syntax : do { statement(s); } while( condition ); Condition is checked at the end of execution

GCD Program Using Do While

Sentinel-Controlled Loops Consider program that calculates the sum of a collection of exam scores is a candidate for using a sentinel value. If the class is large, the instructor may not know the exact number of students who took the exam being graded. The program should work regardless of class size. The loop below uses sum as an accumulator variable and score as an input variable.

Steps in Sentinel-Controlled Loops Initialize sum to zero Get first score while score is not the sentinel Add score to sum Get next score

Break and Continue Statements Interrupt iterative flow of control in loops Break causes a loop to end Continue stops the current iteration and begin the next iteration

Iterative C code of Isogram Problem

Nested Loops Loops may be nested just like other control structures. Nested loops consist of an outer loop with one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, their loop control expressions are reevaluated

Print Pattern

Analysis Let ‘n’ is the largest number in the pattern The pattern consist of 2*n-1 rows Element in each row increases till ‘n’ is printed and then decreases Two loops are required One to print each row Other one should be used in each row to print elements of the row

Print Pattern Input Output Logic Involved Value of ‘n’ Print a pattern Nested loop single or multiple

Write a Program to Print the Pattern

Write a Program to Find the Value of Pi upto ‘n’ terms

Write a Program to Print the Pattern

Write a Program to Print the Pattern

Write a Program to Print the Pattern

Write a Program to Print the Pattern