Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Computer Science 1620 Loops.
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
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)
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 5: Control Structures II (Repetition)
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 4 Program Control Statements
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
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.
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,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
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.
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.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Computer Programming -1-
Chapter 4 Repetition Structures
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Control Structures II (Repetition)
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Additional Control Structures
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 4: Control Structures I (Selection)
Control Structures Part 1
Chapter 5: Control Structures II (Repetition)
Control Statements Paritosh Srivastava.
Presentation transcript:

Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012

Last Time… Branching statements –specify alternate paths of execution, depending on the outcome of a logical condition –Do one thing if some condition is met or another if not (Decisions)

If …else if … else if (condition1) statement1 else if (condition2) statement2... else if (conditionN-1) statementN-1 else statementN

#include using namespace std; int main() { int x, y; cout << "Enter the first number: "; cin >> x; cout << "Enter the second number: "; cin >> y; if (x > y) cout<<x<<" is greater than " <<y<<endl; else if (x == y) cout<<x<<" is equal to "<<y<<endl; else cout<<x<<" is less than "<<y<<endl; return 0; }

Syntax: switch (expression) { case constant1: statements break; case constant2: statements break; … case constantN: statements break; default: statements } An integer expression, whose value we want to test Unique, possible values that we are testing for equality with expression Statements to be executed when expression becomes equal to constantN Statements to be executed when expression is not equal to any of the N constants The break statement causes the Program to exit the switch statement

Example char choice; cout << "Enter A, B, or C: "; cin >> choice; switch (choice) { case 'A': cout << "You entered A.\n"; break; case 'B': cout << "You entered B.\n"; break; case 'C': cout << "You entered C.\n"; break; default: cout << "You did not enter A, B, or C!\n"; }

int num; cout << "Enter a number: "; cin >> num; if (num >= 1 && num <= 5) { switch (num) { case 1: cout << "Recharging..."; break; case 2: cout << "Remaining Balance..."; break; case 3: cout << "Block number..."; break; default: cout << "Change Language..."; } else cout << "Invalid command"; return 0;

Looping A loop is a control structure that causes a statement or group of statements to repeat. C++ has three looping control structures: the while loop, the do-while loop, and the for loop. The difference between each of these is how they control the repetition.

Loop statements –specify computations, which need to be repeated until a certain logical condition is satisfied. –Do a certain action repeatedly based on a condition (Looping)

The while Loop Statement Repeatedly executes a statement if a certain condition is true. It is a pretest loop

While Loop Statement Each repetition is known as an iteration. The while loop is known as a pretest loop, which means it tests its expression before each iteration. An important characteristic of the while loop is that the loop will never iterate if the test expression is false to start with.

While Semantics

Example_01

Example_02 count = 0;// Initialization while (count < 3)// Loop Condition { cout << "Hi ";// Loop Body count++;// Update expression } –Loop body executes how many times?

Example_03 int i = 0; while(i<10) { cout<<i<<” “; i++; } cout<<”done!”; This outputs: done!

Example_04 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;

The do… while Loop Statement There are cases where we may want to execute the body of the loop at least once before testing the condition. In other words, there are times where we may need a posttest loop. This is where the do … while loop comes in.

do-while Loop Syntax

do-while Loop Example count = 0; // Initialization do { cout << "Hi "; // Loop Body count++; // Update expression } while (count < 3); // Loop Condition – Loop body executes how many times? – do-while loops always execute body at least once!

Example WhileDo While int x = 1; while (x < 0) cout << x << endl; int x = 1; do cout << x << endl; while (x < 0); Never executesExecutes once because the expression x<0 is not evaluated until the end of the loop

#include using namespace std; int main() { char reply; do { cout<<“Perform Any Operation Here”<<endl; cout << "Do you want to do it again? (y/n)"; cin >> reply; } while (reply == 'y' || reply == 'Y'); return 0; }

The for Loop Statement The for loop is ideal for working with counters because it provides built-in expressions that initialize and update variables. It’s a pretest loop.

#include using namespace std; // Counting Up void main() { int i; for (i=0; i<=10; i++) { cout<<i <<endl; }

The for Loop Statement

Break ing out of a loop Sometimes it’s necessary to stop a loop before it goes through all its iterations. The break statement causes a loop to terminate early.

The break Statements The break statement can also be placed inside a loop. When it is encountered, the loop stops and program jumps to the statement following the loop.

Example int count = 0; while (count++ < 10) { cout << count << endl; if (count == 5) break; } The while loop in the following program segment appears to execute 10 times, but the break statement causes it to stop after the fifth iteration.

#include using namespace std; int main() { int i = 0; while (true) { if (i % 5 == 0) cout << i << " "; i++; if (i > 25) break; } system("pause"); return 0; }

#include using namespace std; int main() { int max; cout << "Enter max: "; cin >> max; int sum = 0; for (int i = 0; i <= max ; i+=2) { sum += i; if (i > 10) break; } cout << "The sum is: " << sum; return 0; }

The continue Statements The continue statement, placed inside a loop, causes the loop to stop its current iteration and begin the next one. All statements in the body of the loop that come after it are ignored.

The continue statement The continue statement causes the current iteration of a loop to end immediately. 1)In a while loop, this means the program jumps to the test expression at the top of the loop. 2) In a do-while loop, the program jumps to the test expression at the bottom of the loop, which determines if the next iteration will begin. 3) In a for loop, continue causes the update expression to be executed, and then the test expression to be evaluated.