Repetition and Loop Statements

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Outline 5.1 REPETITION IN PROGRAMS
Chapter 5 Repetition and Loop Statements Instructor: Alkar & Demirer.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 5: Loops and Files.
CS 201 Selection Structures (2) and Repetition
Chapter 5 (Loop Statements)
Chapter 5 Repetition and Loop Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
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.
Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.
1 ICS103 Programming in C Ch5: Repetition and Loop Statements.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 5 Repetition and Loop Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville,
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
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.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Chapter 5 Repetition and Loop Statements. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.5-2 Figure 5.1 Flow Diagram of Loop Choice Process.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Repetition statements
CHAPTER 6: REPETITION AND LOOP STATEMENTS
ICS103 Programming in C Lecture 7: Repetition Structures
Programming Logic and Design Fourth Edition, Comprehensive
REPETITION STATEMENTS
Control Structures II (Repetition)
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
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.
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.
Chapter 4 Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Lesson #5 Repetition and Loops.
Chapter 5: Repetition and Loop Statements
Lec 6.
Control Statements Loops.
Chapter 8: More on the Repetition Structure
Chapter 6: Repetition Statements
Repetition and Loop Statements
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
Presentation transcript:

Repetition and Loop Statements Chapter 5 Repetition and Loop Statements 20061018 chap5

Repetition in Programs In most commercial software, you can repeat a process many times. When using an editor program, you can move the cursor to a program line and perform as many edit operations as you need to. Loop is a control structure that repeats a group of steps in a program. Three C loop control statements while for do-while 20061018 chap5

Flow Diagram of Loop Choice 20061018 chap5

Comparison of Loop Kinds 20061018 chap5

The while statement Counter-controlled loop (or counting loop) A loop whose required number of iterations can be determined before loop execution begins. The syntax while (loop repetition condition) statement; Loop repetition condition: the condition that controls loop repetition. Infinite loop: a loop that executes forever 20061018 chap5

Example count_emp = 0; /* no employees processed yet */ while (count_emp < 7) { /* test value of count_emp */ printf("Hours> "); scanf("%d", &hours); printf("Rate> "); scanf("%lf", &rate); pay = hours * rate; printf("Pay is $%6.2f\n", pay); count_emp = count_emp + 1; /* increment count_emp */ } printf("\nAll employees processed\n"); 20061018 chap5

Flowchart for a while Loop 20061018 chap5

Computing a Sum or a Product in a Loop Loops often accumulate a sum or a product by repeating an addition or multiplication operation. Accumulator A variable used to store a value being computed in increments during the execution of a loop. 20061018 chap5

Example 20061018 chap5

Compound Assignment Operators C provide special assignment operators variable op = expression; 20061018 chap5

The for Statement Three loop control components with the loop body. Initialization of the loop control variable, Test of the loop repetition condition, and Change (update) of the loop control variable. The for statement in C supplies a designed place for each of these three components . 20061018 chap5

Counting Loop by for Statement 20061018 chap5

for Statement 20061018 chap5

Increment and Decrement Operators The increment (i.e., ++) or decrement (i.e., --) operators are the frequently used operators which take only one operand. for(int i=0; i<100; i++) {…} for(int i=100; i>0; i--) {…} Side effect: the value of its operand is incremented/decremented by one. 20061018 chap5

Prefix and Postfix Increments The value of the expression in which the ++/-- operator is used depends on the position of the operator. 20061018 chap5

Example: Factorial Function 20061018 chap5

Inc. and Dec. Other Than 1 20061018 chap5

Conditional Loops In many programming situations, you will not be able to determine the exact number of loop repetitions before loop execution begins. Example: Monitor Gasoline Storage Tank (Fig. 5.9) 20061018 chap5

20061018 chap5

20061018 chap5

Loop Design Problem-Solving Questions for Loop Design What are the inputs? What are the outputs? Is there any repetition? Do I know in advance how many time steps will be repeated? How do I know how long to keep repeating the steps? Sentinel-Controlled Loops Endfile-Controlled Loops Infinite Loops on Faulty Data 20061018 chap5

Sentinel-Controlled Loops One way to do this is to instruct the user to enter a unique data value, called a sentinel value, after the last data item.(Fig. 5.10) 20061018 chap5

Endfile-Controlled Loops A data file is always terminated by an endfile character that can be detected by the scanf and fscanf functions. EOF stands for the endfile character. 20061018 chap5

Example of Endfile-Controlled Loops 20061018 chap5

Infinite Loops on Faulty Data Detect faulty data 20061018 chap5

Nested Loops Consist of an outer loop with one or more inner loops. (Fig. 5.13) 20061018 chap5

The do-while Statement When we know that a loop must execute at least one time. 20061018 chap5

Flag-Controlled Loops A flag is a variable used to represent whether or not a certain event has occurred. (Fig. 5.14) 20061018 chap5

20061018 chap5

Homework #5 Due: 2006/10/25 利用迴圈和 '*'可以畫出許多圖形 1.定義一個CENTER常數(#define CENTER 20)表示所有圖形的中心位置 2.實作出一個 function prototype 如下: void triangle(int n); 其作用是繪出 n 排星號的等腰三角形 若n為正時,則畫出三角形是正立的,如a. (n=4) 若n為負時,則畫出三角形是倒立的,如b. (n=-4) 3.主程式將輸入多個整數(最後以0結尾),印出其對應的圖案 並判斷每個整數若不在-9 與 9 的範圍內則不予理會之 a. b. * ******* *** ***** ***** *** ******* * 20061018 chap5

Summary Repetition and Loop Statements while, for, and do-while Counter-Controller Loop Sentinel-Controller Loop Endfile-Controller Loop Input Validation Loop General Conditional Loop while, for, and do-while Compound Assignment Operators 20061018 chap5