Chapter 4 Repetition Structures

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
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)
CS 201 Selection Structures (2) and Repetition
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 5: Control Structures II (Repetition)
Chapter 4: Control Structures II
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
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 Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
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)
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.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
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)
Chapter 5: Control Structures II
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
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.
Lecture 4B More Repetition Richard Gesick
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.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
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
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
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

Overview Repetition in Programs Counting Loops Conditional Loops Using while statement Using for statement Conditional Loops Sentinel-Controlled loops Flag-Controlled loop Nested loop Do-While loop break and continue statement A.AlOsaimi

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

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 We can use a while loop. /* computes the sum: 1 + 2 + 3 + ....+ 100 */ #include <iostream> using namespace std; int main() { int sum =0, i = 1; while (i <= 100) { sum = 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 designated place for each of these three components. A.AlOsaimi

The for Repetition Statement No semicolon (;) after last expression 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

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 – using while loop Example: Print the number from 1 to 10 1 2 3 4 5 6 7 8 9 10 A.AlOsaimi

Example 3– using for Example: Print the number from 1 to 10 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