Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.

Slides:



Advertisements
Similar presentations
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Advertisements

Repeating Actions While and For Loops
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.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Loops and Files.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Chapter 5 (Loop Statements)
Repetition (Loops) Want to do some repetitive sequence of actions: print vertical line of *s * Corresponding program: printf(“*\n”);
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 4: Control Structures II
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
REPETITION CONTROL STRUCTURE
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Week 4 – Repetition Structures / Loops
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Looping.
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
- Additional C Statements
Repetition and Loop Statements
Chapter 4 - Program Control
Control Statements Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Control Statements Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 Repetition Structures
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured programming concepts. An action or a series of actions The above flowchart would be considered an infinite loop because its repeats the action over and over again and never stops. To make a loop end we must have a condition that controls the loop. In other words, the loop must be designed so that before or after each iteration, it checks to see if it is done. Condition Body (action) false true Pretest loop Posttest loop true false

Repetition Two general categories of loops: event-controlled loop - this is where an event will stop the loop from executing (e.g. reading the end of a file.) counter-controlled (conditional) loop - this is used if the number of times an action to be repeated is known in advance. Condition Action false true Pretest loop Posttest loop true false Initialize Loop update Initialize Loop update In a pretest loop, the body can be done zero or more times. In a posttest loop, the body must be done at least once. Before a loop can start, a variable must be initialized. The value of a variable or expression is tested. Then something must happen inside the loop to update the variable, otherwise you could have an infinite loop.

Repetition Loops in C There are three loop statements in C: the while, the for, and the do…while. The first two are pretest loops and the do…while is a posttest loop. All of them can be used for even-controlled and counter-controlled loops. The while and do…while are most commonly used for event-controlled loops and the for is usually used for counter-controlled loops. All three of these loop constructs continue looping when the condition is true and terminate when it is false. The while loop The while loop uses an expression to control the loop. Since it is a pretest loop, it tests the expression before every iteration of the loop. Syntax: expression statements false true while (expression) statement no semicolon If the expression evaluates to true ( any nonzero value) the statements are executed and if it evaluates to false ( 0 ) the loop body is skipped and execution continues after the loop construct.

Repetition Example of the while statement: count_emp = 0; while( count_emp < 7) { printf(“Enter Hours >”); scanf(“%d”, &hours); printf(“Enter rate >”); scanf(“%lf”, &rate); pay = hours * rate; printf(“Pay is $%6.2f\n”, pay); count_emp = count_emp + 1; } printf(“\nAll employees processed\n”); loop control variable initialized before entering while statement loop repetition condition count_emp is tested before the start of each loop repetition. If the condition is true the loop body is executed again loop body executed once for each value of count_emp that is less than 7 control variable is incremented ( updated ) in the body of the loop when count_emp becomes 7, the condition evaluates to false and control passes to the statement that follows the loop body

Repetition The for loop The for statement is a pretest loop that uses three expressions. The first expression contains any initialization statements, the second contains the terminating expression (test ) and the third contains the updating expression. The for loop is used when your loop is to be executed a known number of times. You can do the same thing with a while loop. But a for loop is easier to read and more natural for counting loops. Syntax: statement false true for ( expr 1; expr 2; expr 3 ) statement expr3 expr1 expr2 expr 1 expr 2 expr 3 statement true false Expanded flowchart Flowchart

Repetition Example of the for statement: total_pay = 0.0 ; for( count_emp = 0 ; count_emp < number_emp ; count_emp++ ) { printf(“Enter Hours >”); scanf(“%d”, &hours); printf(“Enter rate >”); scanf(“%lf”, &rate); pay = hours * rate; printf(“Pay is $%6.2f\n”, pay); total_pay = total_pay + pay; } printf(“\nAll employees processed\n”); printf(“Total payroll is $%8.2f\n”, total_pay); exp 1 initialization expression - initializes the loop control variable - only done once exp 2 loop repetition condition count_emp is tested before the start of each loop repetition if the condition is true the loop body is executed again if the condition is false, the loop is exited and the next program statement after the for statement is executed loop body if the condition is true, the statements in the loop body are executed. After the last statement in the loop body is executed, the update expression is executed. expr 3 update expression the value of the control variable is changed and then the repetition condition is tested again 1st 2nd 3rd 4th semicolons

Repetition Example 2 of the for statement:

Repetition The do…while loop The do…while statement is a posttest loop. Like the while and for loops, it also uses an expression to control the loop, but it tests the expression after the execution of the body. Syntax: expression statements false do statement while (expression) ; true semicolon

Repetition Example of the do…while statement:

Repetition The Comma expression A comma expression is a complex expression made up of two expressions separated by commas. Although it can be used in many places, it is generally used only in for statements. The expressions are evaluated left to right. The value and type of the expression are the value and type of the right expression - the other expression is included for its side effect. The comma expression has the lowest priority of all expressions, priority 1. Example: for ( sum = 0, i = 1; i <= 20; i++ ) { scanf ( “%d”, &a ); sum += a ; } Comma expressions can be nested. When they are, all expression values other than the last are discarded. Results: while loop: Loop Count: 11 Number of tests: 11 do…while loop: Loop Count: 11 Number of tests: 10

Repetition The nested Loop A nested loop is a loop within a loop. Within each iteration of the first or outer loop and inner loop is executed one or more times. Example: #include void main(void) { int i, j, k, m = 0; for ( i = 1; i <= 5 ; i += 2 ) for ( j = 1 ; j <= 4 ; j++ ) { k = i + j ; printf( “ i = %3d, j = %3d, k = %3d\n”, i, j, k); } m = k + i; } Output: i = 1, j = 1, k = 2 i = 1, j = 2, k = 3 i = 1, j = 3, k = 4 i = 1, j = 4, k = 5 i = 3, j = 1, k = 4 i = 3, j = 2, k = 5 i = 3, j = 3, k = 6 i = 3, j = 4, k = 7 i = 5, j = 1, k = 6 i = 5, j = 2, k = 7 i = 5, j = 3, k = 8 i = 5, j = 4, k = 9

Repetition Other statements related to looping ( the break & continue) The break statement causes a loop to terminate. It is the same as setting the loop’s limit test to false. break can be used in any of the loop statements - while, for and do…while and in the selection switch statement. Good structured programming limits its use to the switch statement. The continue statement does not terminate the loop, but simply transfers to the testing statement in the while and do…while statements and transfers to the updating expression in a for statement. The use of the continue statement is also considered unstructured programming.