Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)

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

Introducing C++ Elements. CSCE 1062 Outline Main algorithms’ constructs General form of a C++ program {section 2.5} C++ language elements {section 2.1}
Lecture Notes 3 Loops (Repetition)
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
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.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Chapter 6 Looping Dale/Weems. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Chapter 6 Looping.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Chapter 02 (Part III) Introduction to C++ Programming.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Overview Go over parts of quiz? Another iteration structure for loop.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Unary Not operator !  !true = false  !false = true.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
User-Defined Functions (cont’d) - Reference Parameters.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
REPETITION CONTROL STRUCTURE
while Repetition Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
לולאות קרן כליף.
Chapter 8 Repetition Statements
Conditional Construct
Counting Loops.
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
Looping III (do … while statement)
Let’s all Repeat Together
Decision I (if Statement)
Alternate Version of STARTING OUT WITH C++ 4th Edition
do/while Selection Structure
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Looping I (while statement)

CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)

CSCE 1063 Repetition  Repetition or looping provides for the repeated execution of part of the algorithm.

CSCE 1064 Exercise 1  Problem Analyse, and design, using a flowchart, an algorithm that outputs the word “Hello” 10 times each on a separate line.  Analysis  Input None  Output “Hello” 10 times  Intermediate variables i: counter used to indicate the current iteration number.

CSCE 1065 Exercise (cont’d) OUTPUT “Hello”, ‘\n’ START STOP  Design i <= 10 ? True False i = 1 i = i + 1 #include using namespace std; void main () { int i; i = 1; while (i <= 10) { cout << “Hello”<<endl; i = i +1; } }

CSCE 1066 while Statement  Syntax: while (loop repetition condition) statement;  E.g. i = 1; while (i <= 10) { cout << “*”; i = i + 1; } Intialising loop control variable Testing loop control variable Updating loop control variable

CSCE 1067 Listing 5.1 while loop segment example

CSCE 1068 Exercise 2  Problem Analyse, design, and implement an algorithm that calculates and outputs the following sum: sum = ……. + n up to any number (n) input by the user.  Analysis  Input n: upper limit  Output sum: sum of series  Intermediate variables i: the current iteration number

CSCE 1069 INPUT n OUTPUT sum START STOP  Design i <= n ? True False i = 1 sum = 0 sum = sum + i i = i + 1 #include using namespace std; void main () { int n, i, sum; cin >> n; i = 1; sum = 0; while (i <= n) { sum = sum + i; i = i +1; } cout << “The sum is “<<sum; }

CSCE Exercise 3 Analyse, design, and implement an algorithm that calculates the factorial of a given number (n), input by the user. Factorial is calculated using the following equation: factorial = 1 * 2 * 3 * … * n

CSCE #include using namespace std; void main() { int i, n, factorial = 1; cout << “Please enter a whole number:“; cin >> n; i = 2; while (i <= n) { factorial = factorial * i; i = i + 1; } cout << “The factorial is:“ << factorial << endl; }

CSCE Next lecture we will continue Looping control construct in C++