Chapter 4 Repetition Structures

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Computer Science 1620 Loops.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 5: Control Structures II (Repetition)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
CS 201 Selection Structures (2) and Repetition
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
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,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Overview Go over parts of quiz? Another iteration structure for loop.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
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 4 Repetition Structures
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 5: Control Structures II (Repetition)
Control Structures II (Repetition)
Java Programming: Guided Learning with Early Objects
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
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.
Repetition Structures (Loops)
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.
Introduction to Functions
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 Repetition Structures
Arrays, For loop While loop Do while loop
Repetition and Loop Statements
Control Statements Loops.
Chapter 6: Repetition Statements
Computing Fundamentals
Control Structures Part 2
Control Structures Part 1
Let’s all Repeat Together
Chapter 5: Control Structures II (Repetition)
Repetition Statements (Loops) - 2
Control Statements Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Chapter 4 Repetition Structures
ICS103: Programming in C 5: Repetition and Loop Statements
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Chapter 4 Repetition Structures King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi Edited By: Fatimah Alakeel & Noor Alhareqi Chapter 4 Repetition Structures 1st Semester 1433 -1434

Types of Control Structures A.AlOsaimi

What is Meant by Loops? Why Do We Need Them? Group of instructions that a computer executes repeatedly while some condition remains true Why do we need them? To repeat statements until a certain condition becomes false. A.AlOsaimi

Loop Statementes Loop statements are: For loop While loop Do While loop Loop Counting loop For loop Counter-controlled while loop Conditional Loop Flag-Controlled while loop Sentinel-Controlled while loops A.AlOsaimi

Loop Statements .. … while statement T F do while statement T F for statement T F T T F F while (condition) { … } do { .. } while ( condition); for (initialize; condition; update) { .. } A.AlOsaimi

Counting Loop Definite repetition: know how many times the loop will execute Control variable used to count repetitions Two types of Counting loop: Counter-Controlled while loop For loop A.AlOsaimi

Counter–controlled while loop The loop shown below in pseudo code is called a counter- controlled while loop because its repetition is managed by a loop control variable whose value represents a count. Set loop control variable to an initial value While loop control variable < final value ... //Do something multiple times Increase loop control variable by 1 (or step size). A.AlOsaimi

The loop repetition condition controls the while loop. Counter–controlled while loop Example 1 This slide shows a program fragment that computes and displays the gross pay for seven employees. The loop body is the compound statements (those between { and }) The loop repetition condition controls the while loop. count_emp = 0; while (count_emp < 7) { cout<<"Hours:"; cin>>hours; cout<<"Rate:"; cin>>rate; pay = hours * rate; cout<<"Pay is:”<< pay; count_emp = count_emp + 1; //or count_emp++; } Cout<<"\nAll employees processed\n"; loop repetition condition count_emp = 1; while (count_emp <= 7) A.AlOsaimi

Counter–controlled while loop while Statement Syntax of the while Statement: Initialization. i.e. count_emp = 0; Testing(condition). i.e. count_emp < 7 Updating i.e. count_emp = count_emp + 1; The above steps must be followed for every while loop. If updating is skipped, it produce an infinite loop A.AlOsaimi

Counter–controlled while loop Example 2: Computing Sum If we want to compute , we need to do 1+2+3+...+100 #include <iostream> using namespace std; int main() { int sum =0, i = 1; while (i <= 100) { sum = sum + i; //or sum+=i; i = i + 1; //or i++; ++i; i+=1; } cout<<"Sum is :"<<sum <<endl; return 0;} A.AlOsaimi

The for loop A better way to construct a counting loop is to use the for statement. C++ provides the for statement as another form for implementing loops. As before we need to Initialize the loop control variable Test the loop repetition condition Update the loop control variable. An important feature of the for statement in C++is that it supplies a specific place for each of these three components. A.AlOsaimi

General Form of for statement for (initialize; loop repetition condition ; update) { //Steps to perform each iteration } First, the initialization expression is executed. Then, the loop repetition condition is tested. If the condition is true, the statement enclosed in { } are executed. After that the update expression is evaluated. Then the loop repetition condition is retested. The statement is repeated as long as the condition is true. For loop can be used to count up or down by any interval. If there’s more than one statement in the body of the for, braces are required to enclose the body of the loop. A.AlOsaimi

The for Repetition Statement for (initialize; condition; update) {..} No semicolon (;) after last expression A.AlOsaimi

for - Example 1 To compute the sum of 1 to 100: int sum = 0; int i; for (i = 1; i <= 100; i++) { sum = sum + i; } Note: i++ is the same as i = i + 1 and as i += 1. A.AlOsaimi

for - Syntaxes You can write: int i; OR for (i = 1; i <= 100; i++) OR for (int i = 1; i <= 100; i++) If the initialization expression declares the control variable (i.e., its type is specified before its name), the control variable can be used only in the body of the for statement— the control variable will be unknown outside the for statement. This restricted use of the control variable name is known as the variable’s scope. The scope of a variable specifies where it can be used in a program. A.AlOsaimi

for and while A.AlOsaimi

Example 2 – Print the number from 1 to 10 using while loop #include <iostream> using namespace std; int main() { int counter = 1; /* initialization */ while ( counter <= 10 ) /* repetition condition */ cout<< counter <<endl; /* display counter */ ++counter; /* increment */ } // end while return 0; } 1 2 3 4 5 6 7 8 9 10 A.AlOsaimi

using for #include <iostream> using namespace std; int main() { int counter; for ( counter = 1; counter <= 10;counter++ ) cout<< counter <<endl; /* display counter */ } // end for return 0; } 1 2 3 4 5 6 7 8 9 10 A.AlOsaimi

for statement A.AlOsaimi

Examples for using for statement A.AlOsaimi

Examples for using for statement A.AlOsaimi

for - Example 4 A.AlOsaimi