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

Similar documents
REPETITION CONTROL STRUCTURE LOGO

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

Why Is Repetition Needed?

Unit 7. 'while' Loops

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

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

Increment and the While. Class 15

Programming Language. Functions. Eng. Anis Nazer First Semester

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

Chapter 5: Control Structures II (Repetition)

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

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

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

C++ Final Exam 2017/2018

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

Control Structures of C++ Programming (2)

do { statements } while (condition);

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

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

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

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

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

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

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

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

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

Chapter 4: Control structures. Repetition

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Repetition and Loop Statements Chapter 5

Chapter 4: Control structures

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

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

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Chapter 6 Topics. While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data

Chapter 3. More Flow of Control

Control Structures. Repetition (Loop) Structure. Repetition (Loop) Structure. Repetition (Loop) Structure. CS225: Slide Set 8: C++ Loop Structure

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Loops / Repetition Statements

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

Lecture 7 Tao Wang 1

In this chapter you will learn:

Information Science 1

Chapter 4 - Notes Control Structures I (Selection)

C/C++ Programming Lecture 7 Name:

CIS 130 Exam #2 Review Suggestions

INTRODUCTION TO COMPUTER SCIENCE - LAB

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Programming Fundamentals

204111: Computer and Programming

Computer Programming I - Unit 5 Lecture page 1 of 14

Example 3. #include <iostream> using namespace std; int main()

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

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

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

Add Subtract Multiply Divide

Loops and Files. of do-while loop

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

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

Information Science 1

A Look Back at Arithmetic Operators: the Increment and Decrement

Control Structures II. Repetition (Loops)

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

Chapter 5: Loops and Files

University of Dublin

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

If Control Construct

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

CIS 3260 Intro to Programming in C#

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

int x = 5; double y = 3; // Integer division rounds the result down to the nearest whole number. cout << "1a: " << x / 3 << endl; //1

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

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

Control Statements Loops

LAB 4.1 Relational Operators and the if Statement

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Chapter 1 INTRODUCTION

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

Chapter Goals. Contents LOOPS

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

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Repetition Algorithms

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Chapter 5: Control Structures

CSCE 206: Structured Programming in C++

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

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

Looping. Arizona State University 1

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Data Structures And Algorithms

Chapter 3. Iteration

Transcription:

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

Repetition statements Control statements change the order which statements are executed Selection : some statements are executed, others are not (based on a condition) Repetition: statements are executed repeatedly Why do we need repetition: You want to find the average of 5 grades? You want to find the average of 10 grades? You want to find the average of 100 grades?...?

Repetion statements Write a program to calculate the average of 5 grades We can repeatedly write the statements: int a,b,c,d,e; double average; cout << "Enter five grades:"; cin >> a ; cin >> b ; cin >> c ; cin >> d ; cin >> e ; average = static_cast<double>(a+b+c+d+e)/5; cout << "Average = " << average; write a similar program for 100 grades?

Repetition writing statements repeatedly is not practical, why? size, effort, programmer time, debugging, maintenance In C++, we have 3 repetition statements while for do/while

while Operation flow chart of a while loop : Condition true Statement(s) false

form 1 while statement syntax while ( condition ) Statement

form 2 while statement syntax while ( condition ) Statements... while body

while Operation The statements inside the while statement are called "while body" Operation: The condition is checked first if the condition is true, the while body is executed after the body is executed, the condition is checked again if the condition is true, the while body is executed again... the process is repeated in other words, while the condition is true, the computer keeps repeating the statements

while statement Example: what is the output int c=0; while ( c <= 8 ) cout << c << " "; c++; cout << "bye";

while statement variable c is called Loop Control Variable (LCV) Notes: if the condition does not become false, we have an infinite loop... logical error we have to make sure that the condition at some point becomes false if c++ is removed, we have an infinite loop :)

General structure: while statement //... initialize loop variable while ( condition ).. //... update loop variable..

while statement Example: Write a loop that will display your name 99 times on the screen? int i=0; // initialize LCV while ( i < 99 ) cout << "Anis"; i++; // update LCV

while statement What is the output? int c = 0; while ( c <= 8 ) c++; cout << c << " "; cout << "bye";

while statement What is the output? int c = 0; while ( c <= 8 ) cout << c << " "; cout << "bye"; infinite loop

Examples Example: what is the output int c = 1, s = 0; while (c <= 5) s = s + c; c++; cout << s << endl << c << endl;

Examples Example: what is the output int c = 1, s = 0, g; while (c <= 5) cin >> g; s = s + g; c++; cout << s << endl << c << endl;

while example Write a program that reads 10 grades and displays their average. Questions: Variables? grade, sum, average... others? steps? read grade 1 then add to sum read grade 2 then add to sum read grade 10 then add to sum display average = sum /10

Example: grade average Start sum = 0 count = 0 count < 10 true Input grade add grade to sum increment counter false calculate average display average End

Example: Grade average int grade, sum = 0, count = 0; double average; cout << "Enter 10 grades:"; while ( count < 10 ) cin >> grade; sum = sum + grade; count++; average = static_cast<double>(sum) / 10; cout << "Average = " << average;

Example: Grade average V2 modify the program to exclude invalid grades int grade, sum = 0, count = 0; double average; cout << "Enter 10 grades:"; while ( count < 10 ) cin >> grade; if ( grade >= 0 && grade <= 100 ) sum = sum + grade; count++; else cout << "Error, invalid grade"; average = static_cast<double>(sum) / 10; cout << "Average = " << average;

loop examples Write a program that finds the average of a any number of grades. The user enters the number of grades first, then he/she enters the grades Questions: Variables: grade, sum, counter, number of grades Steps: read a number (N) read N grades and process them display average

loop example int grade, sum = 0, count = 0, N = 0; double average; cout << "Enter the number of grades:"; cin >> N; cout << "Enter " << N << " grades:\n"; while ( count < N ) cin >> grade; if ( grade >= 0 && grade <= 100 ) sum = sum + grade; count++; else cout << "Error, invalid grade"; average = static_cast<double>(sum) / N; cout << "Average = " << average;

loop example Previous program has some logical errors: What if the user enters a negative number of grades? What if the user enters a 0 for the number of grades? what are your suggestions? we can use a condition of course

Examples Example: what is the output int count = 1; int y = 10; while (count < 7) y = y 1; count++; cout << y << endl << count << endl;

int x = 1; int y = 0; while ( y <= 10 ) if ( x % 3!= 0 ) y = y + 3; else y = y 2; x++; cout << y; Examples Example: what is the output

More Notes: while statement loop control variable must be initialized before the loop, if not logical error int c; while ( c <= 20 ) cout << c << " "; c++; cout << "bye";

More Notes: while statement putting a semicolon after condition is a logical error int c = 0; while ( c <= 20 ) ; cout << c << " "; c++; cout << "bye";

loop types Loops can be classified into some types: Counter controlled : loop repeats for a certain number of times Sentinel controlled : loop repeats until the user enters a value to end the loop Flag controlled : loop repeats until a certain condition is met flag : is a boolean variable that is used to check for a certain case

loop types counter controlled: loop repeats for a certain number of times Example: Write a loop that will display your name 99 times on the screen? int i=0; // initialize LCV while ( i < 99 ) cout << "Anis"; i++; // update LCV

loop types sentinel controlled: keep repeating until user ends the loop example: keep repeating the game until the user quits...but you should never quit... :) char quit = 'n'; while ( quit!= 'y' ) // game commands cout <<"Do you want to quit " <<" (y for yes, n for no)? "; cin >> quit;

loop types flag controlled: loop repeats until a certain condition is met (flagged) example: keep guessing until the password is found bool found = false; while (! found ) // try to guess the password... if ( guess == password ) found = true;

Questions hit me...! with your questions ocourse