Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.

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

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require 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)
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
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.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
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,
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
Lecture 7: Repeating a Known Number of Times
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
Chapter 5 Repetition.
For Loops October 12, 2017.
Outline Altering flow of control Boolean expressions
3 Control Statements:.
Repetition Control Structure
Computing Fundamentals
2.6 The if/else Selection Structure
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
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
statement. Another decision statement, , creates branches for multi-
REPETITION Why Repetition?
Looping and Repetition
Presentation transcript:

Computer Science Department LOOPS

Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The repetition continues while a condition is true. When condition becomes false, the loops ends and control passes to the statements following the loop. Types of Loops – for loop – while loop –do while loop

Computer Science Department for Loop for loop execute a section of code a fixed number of times. Its usually used when you know, before entering the loop, how many times you want to execute the code. The for allows us to specify three things about a loop in a single line. a)Setting a loop counter to an initial value. b)Testing the loop counter to determine whether its value has reached the number of repetitions desired. c)Increasing the value of loop counter each time the program segment within the loop has been executed.

Computer Science Department for Loop The general form of for statement is as under. for ( initialization expression; test expression; increment expression ) { statement 1; statement 2;. statement n; }

Computer Science Department for Loop Example void main ( ) { int j; for ( j=0; j < 15; j++ ) cout<< j; getche( ); }

Computer Science Department for Loop The Initialization Expression –The initialization expression is executed only once, when the loop first starts. It gives the loop variable an initial value. The Test Expression –The test expression usually involves a relational operator. It is evaluated each time through the loop, just before the body of the loop is executed. It determine whether the loop will be executed again or not. The Increment Expression –The increment expression changes the value of the loop variable, often by incrementing it. It is always executed at the end of the loop, after the loop body has been executed.

Computer Science Department Nesting of Loops Loops can also be nested like if statements. void main ( ) { int r, c, sum; for ( r = 1; r<= 3; r++) { for( c =1; c <= 2; c++) { sum = r + c; cout<<“r =”<< r; cout<<“ c =”<< c; cout<<“sum =”<<sum<<endl; }

Computer Science Department Variable Visibility The loop body, which consists of braces delimiting several statements, is called a block of code. Variable defined inside the block is not visible outside it. Visible means that program statements can access the variable.

Computer Science Department Variable Visibility Example void main ( ) { for ( int i=0; i < 10; i++) { int j=i; cout<< “hello world”<<j<<endl; } cout<< j; //Error: j is undefined. }

Computer Science Department while Loop The while statement, like if statement, test a condition. The syntax of while loop is while ( expression ) { statement 1; statement 2; }

Computer Science Department while Loop Example void main ( ) { int n = 99; while ( n != 0 ) { cout<<“ Enter any number or 0 to quit the program ”; cin >> n; } getche( ); }

Computer Science Department Tips The statement within the while loop would keep on getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop. The condition being tested may use relational or logical operators as shown in the following example. –while ( i <= 10 ) – while ( i <= 10 && j <= 15 ) – while ( j > 10 && ( b < 15 || c < 20))

Computer Science Department Tips It is not necessary that a loop counter must be an int. It can even be a float. void main ( ) { float a=10.0; while ( a <= 10.5 ) { cout<<a; a = a + 0.1; }

Computer Science Department Exercise The process of finding the largest number (i.e., the maximum of a group of numbers) is used frequently in computer applications. For example, a program that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. Write a pseudocode program, then a C++ program that inputs a series of 10 numbers, and determines and prints the largest of the numbers.