Fundamental Programming 310201 Fundamental Programming for Loops.

Slides:



Advertisements
Similar presentations
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Advertisements

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.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Loops and Files.
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.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Objectives You should be able to describe:
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5: Control Structures II (Repetition)
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
CPS120 Introduction to Computer Science Iteration (Looping)
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
 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.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
2.6 The if/else Selection Structure
Objectives You should be able to describe: The while Statement
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
Looping and Repetition
Presentation transcript:

Fundamental Programming Fundamental Programming for Loops

Fundamental Programming Repetition Statements we have now used two C++ repetition statement – while and do-while while tests the loop condition at the start of the loop – allowing for the possibility that we may not need to perform the loop do-while tests the loop condition at the end of the loop – useful when we need to perform a loop at least once

Fundamental Programming Repetition Statements in this class we introduce one more repetition statement - for for is just a convenience, you don’t need it - anything you can do with for, you can do with while for is convenient when you know in advance how many times you need to perform a loop – instead of…

Fundamental Programming for vs while cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; NbrLoops = 0; while (NbrLoops < NbrStudents) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; NbrLoops = NbrLoops +1; } notes: initialise loop control variable

Fundamental Programming for vs while cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; NbrLoops = 0; while (NbrLoops < NbrStudents) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; NbrLoops = NbrLoops +1; } notes: loop condition

Fundamental Programming for vs while cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; NbrLoops = 0; while (NbrLoops < NbrStudents) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; NbrLoops++; } notes: modify loop control variable (to avoid looping forever)

Fundamental Programming cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } for vs while notes: initialise loop control variable

Fundamental Programming cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } for vs while notes: loop condition

Fundamental Programming cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } for vs while notes: modify loop control variable (to avoid looping forever)

Fundamental Programming for ( ; ; ) { } for Loop Syntax notes: parentheses around for clause

Fundamental Programming for ( ; ; ) { } for Loop Syntax notes: statement performed once before entering loop for first time

Fundamental Programming for ( ; ; ) { } for Loop Syntax notes: semi-colons after loop initialisation and loop condition

Fundamental Programming for ( ; ; ) { } for Loop Syntax notes: condition tested at the start of each loop – including the very first loop

Fundamental Programming for ( ; ; ) { } for Loop Syntax notes: statement performed at the end of each loop

Fundamental Programming for Loop Operation for loop statements loop condition false true loop initialise statement loop completion statement

Fundamental Programming Activity for ( int Counter = 0; Counter < 5; Counter++ ) { cout << “Counter = “ << Counter << endl; } what output is produced by the following code:

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback Counter = 0 Counter = 1 Counter = 2 Counter = 3 Counter = 4 the output produced by this code is:

Fundamental Programming Loop Control Variables usually an integer, but can be a character can be declared within the for clause – instead of...

Fundamental Programming int NbrLoops; cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } Loop Control Variables

Fundamental Programming cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (int NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } Loop Control Variables

Fundamental Programming int NbrLoops; cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (int NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } Loop Control Variables notes: if you try to declare the same variable twice, you get a compilation error

Fundamental Programming Loop Control Variables loop control variables are not normally modified within the loop it’s a common source of error…

Fundamental Programming Activity for ( int Counter = 0; Counter != 5; Counter++ ) { cout << “Counter = “ << Counter << endl; Counter = Counter + 1; } what output is produced by the following code:

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback Counter = 0 Counter = 2 Counter = 4 Counter = 6 Counter = 8 : ( until you terminate the program! ) the output produced by this code is:

Fundamental Programming Activity write the for clause of a for loop to produce this output: Counter = 15 Counter = 14 Counter = 13 Counter = 12 Counter = 11 Counter = 10

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback the for clause of a for loop to produce this output is: for ( int Counter = 15; Counter >= 10; Counter-- )

Fundamental Programming Nested Loops we saw that an if-else statement can be embedded inside another if-else statement likewise, a for statement can be nested inside another for statement note: in fact, any selection statement (if- else, switch) or repetition statement (while, do-while, for) can be embedded inside another selection or repetition statement

Fundamental Programming Nested Loops with a nested loop, one performs one or more trips around the inner loop for each trip around the outer loop here’s an example...

Fundamental Programming Nested Loops for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ) { cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; } inner loop

Fundamental Programming Activity for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ) { cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; } activity: what does this code produce as output

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ) { cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; } feedback: this code produces the following output… Col 1 Col 2 Col3

Fundamental Programming Nested Loops for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ) { cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; } outer loop inner loop

Fundamental Programming Nested Loops for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ) { cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; } note: here, three trips around the inner loop are performed for each trip around the outer loop

Fundamental Programming Activity for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ) { cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; } activity: what is the output produced by this code?

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback for ( int RowNbr = 1; RowNbr <= 4; RowNbr++ ) { cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; } feedback: the output produced by this code is Row 1: Col 1 Col 2 Col 3 Row 2: Col 1 Col 2 Col 3 Row 3: Col 1 Col 2 Col 3 Row 4: Col 1 Col 2 Col 3

Fundamental Programming Looping Examples Chapter 7 of the textbook provides many examples of looping there are also many examples in the Study Guide use tracing to check that you understand the mechanics of looping statements

Fundamental Programming Increment and Decrement the textbook would use ++RowNbr instead of RowNbr++ in a for loop for ( int RowNbr = 1; RowNbr <= 5; ++RowNbr ) the above for clause has the exact same effect as the one below for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ )

Fundamental Programming Increment and Decrement the difference between ++RowNbr and RowNbr++ is quite subtle – it can be seen from: RowNbr = 1; cout << RowNbr++ << endl; cout << RowNbr; RowNbr = 1; cout << ++RowNbr << endl; cout << RowNbr; output

Fundamental Programming Increment and Decrement recall the syntax of cout: cout ; below, the expression is simply the value of variable RowNbr cout << RowNbr; below, the value of RowNbr is incremented after it’s value is used in the expression RowNbr = 1; cout << RowNbr++ << endl; 1 output

Fundamental Programming Increment and Decrement below, the value of RowNbr is incremented before it’s value is used in the expression RowNbr = 1; cout << ++RowNbr << endl; the statements below have exactly the same effect – they simply increment the value of RowNbr RowNbr++; ++RowNbr; 2 output

Fundamental Programming Summary the for loop is a convenience, it’s not needed - anything you can do with a for loop you can do with a while loop the for loop is useful when the number of required trips around a loop is known before entering the loop consequently, the for loop is useful when using arrays – the topic of a future class