LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
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 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
Control Structures in C++ while, do/while, for switch, break, continue.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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,
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 4: Control Structures II
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 – C Program Control
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Chapter 5- part 2 Control Statements: Loops 2
- Additional C Statements
3 Control Statements:.
Chapter 5 Control Statements: Loops 2
2.6 The if/else Selection Structure
EPSII 59:006 Spring 2004.
Dale Roberts, Lecturer IUPUI
Presentation transcript:

LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan

Content 2  Types of repetition statements  while repetition statement  Counter-controlled repetition  Sentinel-controlled repetition  Nested control statements  for repetition statement  do..while repetition statement  break and continue statements  Common errors

Repetition Statements  Three types  for statement and while statement Perform a group of actions zero or more times  do..while statement Perform a group of actions at least once 3

while Repetition Statements  Actions repeated while condition remains true  Syntax  One of the actions should causes condition to becomes false  Example 4 while (condition) { action1; action2;. actionN; } int product = 3; while ( product <= 30 ) product *= 3; int product = 3; while ( product <= 30 ) product *= 3;

while Repetition Statements (cont.)  Not providing action that causes condition to becomes false  Lead to infinite loop  Logic error  Example 5 int product = 3; while ( product <= 30 ) cout << product; int product = 3; while ( product <= 30 ) cout << product;

Counter-controlled Repetition  Uses a variable to control number of loops’ iterations  Control counting loops with integer values  Also known as definite repetition  Number of repetitions known beforehand  Requires  Name of loop control variable  Initial value of loop control variable  Condition to test of reaching final value  Update loop control variable 6 Initialize control var ; while (condition) { action1; action2;. actionN; update control var; }

7

Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) are entered by the user. Determine the class average on the quiz. 8

9 Variables used to store totals are normally initialized to zero before being used in programs

10

Nested Control Statement  Control statements can be one inside the another  Nested building blocks  Example # 1 A college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a program that analyzes the results. 11

12

13 25 // if result 1, increment passes; if/else nested in while 26 if( result ==1)// if/else nested in while 27 passes = passes +1; else// if result not 1, increment failures 30 failures = failures +1; // incrementstudentCounterso loop eventually terminates 33 studentCounter= +1; }// end while // termination phase; display number of passes and failures 38 cout<<"Passed "<< passes <<endl; 39 cout<<"Failed "<< failures <<endl; return0;// successful termination }// end function main

14

for Repetition Statement  Provide counter-controlled repetition details in a single statement  Syntax  for loop repeats actions until condition becomes false 15 for (initialization; loopContinuationCondition; update) action1; for (initialization; loopContinuationCondition; update) { action1; action2; … actionN; }

16

for Repetition Statement (cont.)  When loop counter is declared in initialization expression, it can ONLY be used inside for statement (local variable)  initialization and update expressions can be comma-separated lists of expressions 17 for(int i=0, j=0; i<4 && j<8; i++,j++) cout << “*”; for(int i=0, j=0; i<4 && j<8; i++,j++) cout << “*”;

for Repetition Statement (cont.)  initialization, condition and update expressions are optional, but separated semicolon are required  Omit initialization Initializes control variable earlier in program  Omit condition Always condition is true, may lead to infinite loop  Omit update Calculated as statement in for body 18

Examples Using for Statement  Vary control variable from 100 to 1 in increments by -1 (decrement by 1)  Vary control variable from 7 to 77 in steps of 7  Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 19 for(int i = 100; i >= 1; i-- ) for(int i = 7; i <= 77; i += 7 ) for(int i = 99; i >= 0; i -= 11 )

Examples Using for Statement (cont.)  Using a comma-separated list of expressions can be written as 20 int total =0; for ( int number = 2; // initialization number <= 20; // loop continuation condition total += number, number += 2 ) // total and increment ; // empty statement int total =0; for ( int number = 2; // initialization number <= 20; // loop continuation condition total += number, number += 2 ) // total and increment ; // empty statement int total =0; for ( int number = 2; number <= 20; number += 2 ) total += number; int total =0; for ( int number = 2; number <= 20; number += 2 ) total += number;

Example A person invests 1000$ in a savings account yielding 5% interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the formula: where pis the original amount invested ris the annual rate nis the number of years ais the amount of deposit at the end of the n year 21

22

23

do..while Repetition Statement  Similar to while statement but while statement tests loop- continuation before performing body of loop  do..while tests loop-continuation after performing body of loop  Loop body always executes at least once  Syntax 24 do { action1; action2;. actionN; } while (condition)

25 What is the output produced by this loop ?? int counter=1; do { cout << counter << endl; } while (++counter <= 10); int counter=1; do { cout << counter << endl; } while (++counter <= 10);

break Statement  Alter flow of control  Causes immediate exit from control structure  Used with while, for, do…while or switch statements  Escape early from a loop ( while, for, do…while )  Skip the remainder of switch 26

27

continue Statement  Used with while, for or do…while statements  Alter flow of control  Skips remainder of loop body of current iteration  Proceeds with next iteration of loop  With while and do…while statements  Loop-continuation test is evaluated immediately after continue statement  With for statement  Update expression is executed  Next, loop-continuation test is evaluated 28

29

Exercise  Write a program that asks for a number and reports the summation from 1 to the number. Use while statement.  Write another version of program using for statement.