Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Similar documents
LECTURE 5 Control Structures Part 2

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Introduction. C provides two styles of flow control:

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Why Is Repetition Needed?

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Programming in C++ PART 2

REPETITION CONTROL STRUCTURE LOGO

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Control Structures of C++ Programming (2)

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

C++ Programming: From Problem Analysis to Program Design, Third Edition

In this chapter you will learn:

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Chapter 5: Control Structures II (Repetition)

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Theory of control structures

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Chapter 2: Functions and Control Structures

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Lecture 5 Tao Wang 1

INTRODUCTION TO COMPUTER SCIENCE - LAB

204111: Computer and Programming

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

Computer Programming: C++

Looping. Arizona State University 1

Unit 7. 'while' Loops

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

BIL101E: Introduction to Computers and Information systems Lecture 8

LECTURE 04 MAKING DECISIONS

Chapter 4: Making Decisions

Chapter 5: Loops and Files

Programming for Engineers Iteration

Computational Expression

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Chapter 4: Making Decisions

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

SFU CMPT Topic: Control Statements

Increment and the While. Class 15

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Programming Fundamentals

ECE 122. Engineering Problem Solving with Java

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

CSCE 206: Structured Programming in C++

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Repetition CSC 121 Fall 2014 Howard Rosenthal

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

2.11 Assignment Operators. Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Lecture Transcript While and Do While Statements in C++

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Computer Programming

Lecture 7: General Loops (Chapter 7)

Computer Programming

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Lecture 7 Tao Wang 1

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

COP 2000 Note Framework Chapter 5 - Repetition Page 1

Computer Programming I - Unit 5 Lecture page 1 of 14

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Add Subtract Multiply Divide

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Chapter 4 C Program Control

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Chapter 2 - Control Structures

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

CSI33 Data Structures

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Chapter 4: Control Structures I (Selection)

Introduction to Programming

Name Section: M/W T/TH Number Definition Matching (8 Points)

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

For Loop. Variations on Format & Specific Examples

C++ Programming Lecture 1 Software Engineering Group

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

C++ PROGRAMMING SKILLS Part 2 Programming Structures

EP578 Computing for Physicists

5. Selection: If and Switch Controls

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Transcription:

Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017

Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The number of repetitions is based on criteria defined in the loop structure, usually a true/false expression The three loop structures in C++ are: while loops do-while loops for loops Three types of loops are not actually needed, but having the different forms is convenient

while and do-while loops Format of while loop: while (expression) statement Format of do/while loop: do statement while (expression); The expression in these formats is handled the same as in the if/else statements discussed previously (0 means false, anything else means true) The statement portion is also as in if/else. It can be a single statement or a compound statement (a block ).

while and do-while loops We could also write the formats as follows (illustrating more visually what they look like when a compound statement makes up the loop body ): while (boolean expression) statement1; statement2; //... statementn;

while and do-while loops The same can be done for the do-while loop: do statement1; statement2; //... statementn; while (boolean expression);

How they work The expression is a test condition that is evaluated to decide whether the loop should repeat or not. true means run the loop body again. false means quit. The while and do/while loops both follow the same basic flowchart the only exception is that: In a while loop, the expression is tested first In a do/while loop, the loop body is executed first

Examples Both of these examples add all the numbers from 1 through 50. // while loop example //loop runs 50 times, condition checked 51 times int i = 1, sum = 0; while (i <= 50) sum += i; // means: sum = sum + i i++; // means: i = i + 1 cout <<"Sum of numbers from 1 through 50 is " <<sum <<endl;

Examples // do-while loop example //loop runs 50 times, condition checked 50 times int i = 1, sum = 0; do sum += i; // means: sum = sum + i i++; // means: i = i + 1 while (i <= 50); cout <<"Sum of numbers from 1 through 50 is " <<sum <<endl;

The for loop The for loop is most convenient with counting loops i.e. loops that are based on a counting variable, usually a known number of iterations Format of for loop: for (initialcondition; boolean Expression; iterativestatement) statement; Remember that the statement can be a single statement or a block, so an alternate format might be: for (initialcondition; boolean Expression; iterativestatement) statement1; statement2; //... statementn;

How it works The initialcondition runs once, at the start of the loop The testexpression is checked. (This is just like the expression in a while loop). If it s false, quit. If it s true, then: Run the loop body Run the iterativestatement Go back to the testexpression step and repeat Example: //loop runs 50 times, condition checked 51 times int i, sum = 0; for (i = 1; i <= 50; i++) sum += i; cout <<"Sum of numbers from 1 through 50 is " <<sum <<endl;

More examples This loop prints Hello 10 times. for (int i = 0; i <10; i++) cout <<"Hello" <<endl ; Loops can also be nested. This prints a rectangle for (int i = 0; i <10; i++) for (int j = 0; j <15; j++) cout <<"*"; cout <<endl;

Some notes on the for loop It should be noted that if the control variable is declared inside the for header, it only has scope through the for loop s execution. Once the loop is finished, the variable is out of scope: for (int counter = 0; counter <10; counter++) // loop body cout <<counter; // illegal. counter out of scope

Some Notes on the for loop This can be avoided by declaring the control variable before the loop itself. int counter; // declaration of control variable for (counter = 0; counter <10; counter++) // loop body cout <<counter; // OK. counter is in scope

Some Notes on the for loop For loops also do not have to count one-by-one, or even upward. Examples: for (i = 100; i >0; i--) for (c = 3; c <= 30; c+=4) The first example gives a loop header that starts counting at 100 and decrements its control variable, counting down to 1 (and quitting when i reaches 0). The second example shows a loop that begins counting at 3 and counts by 4 s (the second value of c will be 7, etc).

break and continue These statements can be used to alter the flow of control in loops, although they are not specifically needed. (Any loop can be made to exit by writing an appropriate test expression). break: This causes immediate exit from any loop (as well as from switch blocks). continue: When used in a loop, this statement causes the current loop iteration to end, but the loop then moves on to the next step. In a while or do-while loop, the rest of the loop body is skipped, and execution moves on to the test condition. In a for loop, the rest of the loop body is skipped, and execution moves on to the iterative statement.