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

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

C++ PROGRAMMING SKILLS Part 2 Programming Structures

In this chapter you will learn:

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)

REPETITION CONTROL STRUCTURE LOGO

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

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

Structured Programming. Dr. Mohamed Khedr Lecture 9

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

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?

Why Is Repetition Needed?

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

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

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Chapter 4 C Program Control

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

Iterative Constructs

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

Introduction. C provides two styles of flow control:

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

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

C++ Final Exam 2017/2018

Chapter 5: Control Structures II (Repetition)

LECTURE 5 Control Structures Part 2

DELHI PUBLIC SCHOOL TAPI

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

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.

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

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

Introduction to Programming

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

CHAPTER : 9 FLOW OF CONTROL

Chapter 2 - Control Structures

Control Structures of C++ Programming (2)

Score score < score < score < 65 Score < 50

Lecture 7 Tao Wang 1

Chapter 4 C Program Control

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

Chapter 2 - Control Structures

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

Chapter 2 - Control Structures

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

Dept. of CSE, IIT KGP

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

Looping. Arizona State University 1

Loops / Repetition Statements

Theory of control structures

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Increment and the While. Class 15

Programming Fundamentals

do, while and for Constructs

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

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

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Repetition Structures

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

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

Quiz Determine the output of the following program:

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

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

n Group of statements that are executed repeatedly while some condition remains true

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

Flow Control. CSC215 Lecture

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

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

SFU CMPT Topic: Control Statements

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

204111: Computer and Programming

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

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

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Chapter 5: Loops and Files

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

A Freshman C++ Programming Course

J. P. Cohoon and J. W. Davidson 1999 McGraw-Hill, Inc. Control Constructs. Mechanisms for deciding when and how often an action should be taken

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

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Computer Programming

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

3 The L oop Control Structure

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

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

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

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

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

Name SECTION: 12:45 2:20. True or False (12 Points)

Chapter 7. Additional Control Structures

Basic Statements in C++ are constructed using tokens. The different statements are

ECE 122. Engineering Problem Solving with Java

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

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Study Guide for Test 2

Control Statements. Musa M. Ameen Computer Engineering Dept.

Repetition, Looping. While Loop

Transcription:

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

Outline 1. C++ Iterative Constructs 2. The for Repetition Structure 3. Examples Using the for Structure 4. The while Repetition Structure 5. Examples Using the while Structure 6. The do/while Repetition Structure 7. The break and continue Statements 8. Nested control structures

1. C++ Iterative Constructs There are three constructs: while statement for statement do-while statement

2. The for Repetition Structure The general format when using for loops is for ( initialization; LoopContinuationTest; increment ) statement Example: for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl; Prints the integers from one to ten No semicolon after last statement

2. The for Repetition Structure Syntax for (ForInit ; ForExpression; PostExpression) Action Example for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl;

Evaluated once at the beginning of the for statements's execution If ForExpr is true, Action is executed ForInit ForExpr true false The ForExpr is evaluated at the start of each iteration of the loop After the Action has completed, the PostExpression is evaluated Action If ForExpr is false, program execution continues with next statement PostExpr After evaluating the PostExpression, the next iteration of the loop starts

2. The for Repetition Structure For loops can usually be rewritten as while loops: initialization; while ( loopcontinuationtest){ statement increment; Initialization and increment as comma-separated lists for (int i = 0, j = 0; j + i <= 10; j++, i++) cout << j + i << endl;

3. Examples Using the for Structure Sum the numbers from 0 to 10 #include <iostram.h> void main ( ) { int sum = 0 ; for ( int i = 0; i < = 10; i++ ) { sum = sum + i ; cout << Summation = << sum ; Summation =

3. Examples Using the for Structure Sum the even numbers from 0 to 100 #include <iostram.h> void main ( ) { int sum = 0 ; for ( int i = 0; i < = 100; i+=2 ) { sum = sum + i ; cout << Summation = << sum ; Summation =

3. Examples Using the for Structure Sum the odd numbers from 0 to 100 #include <iostram.h> void main ( ) { int sum = 0 ; for ( int i = 1; i < = 100; i+=2 ) { sum = sum + i ; cout << Summation = << sum ; Summation =

3. Examples Using the for Structure Printing characters depending on user entry #include <iostram.h> void main ( ) { int n ; char ch; cout << Please enter the character: ; cin >> ch ; cout << Please enter the number of repetition: ; cin >> n ; for ( int i = 0; i < n ; i++ ) cout << ch;

4. The while Repetition Structure Logical expression that determines whether the action is to be executed Action to be iteratively performed until logical expression is false while ( Expression ) Action

4. The while Repetition Structure While Semantics Expression is evaluated at the start of each iteration of the loop Expression If Expression is true, Action is executed Action true false If Expression is false, program execution continues with next statement

4. The while Repetition Structure Repetition structure Programmer specifies an action to be repeated while some condition remains true Psuedocode while there are more items on my shopping list Purchase next item and cross it off my list while loop repeated until condition becomes false. Example int product = 2; while ( product <= 1000 ) product = 2 * product;

4. The while Repetition Structure Flowchart of while loop product <= 1000 true product = 2 * product false

5. Examples Using the while Structure Printing characters depending on user entry #include <iostram.h> void main ( ) { int n, i = 0 ; char ch; cout << Please enter the character: ; cin >> ch ; cout << Please enter the number of repetition: ; cin >> n ; while ( i < n ) { cout << ch ; i ++ ;

5. Examples Using the while Structure The summation of the numbers squared from 0 to 10 #include <iostram.h> void main ( ) { int sq_sum = 0, x = 0, y ; while ( x < = 10 ) { y = x * x ; sq_sum = sq_sum + y ; x ++ ; cout << The summation of the numbers squared from 0 to 10 << sq_sum ;

5. Examples Using the while Structure Factorial of a number #include <iostram.h> void main ( ) { int n, fact = 1 ; cout << Please enter a number << endl ; cin >> n ; while ( n > 0 ) { fact = fact * n ; n -- ; cout << The factorial of your number is << fact ;

6. The do/while Repetition Structure The do/while repetition structure is similar to the while structure, Condition for repetition tested after the body of the loop is executed Syntax: do { statement(s) while ( condition ); Example (letting counter = 1): action(s) do { cout << counter << " "; true while (++counter <= 10); condition This prints the integers from 1 to 10 false All actions are performed at least once.

7. The break and continue Statements Break Causes immediate exit from a while, for, do/while or switch structure Program execution continues with the first statement after the structure Common uses of the break statement: - Escape early from a loop - Skip the remainder of a switch structure

7. The break and continue Statements Continue Skips the remaining statements in the body of a while, for or do/while structure and proceeds with the next iteration of the loop In while and do/while, the loopcontinuation test is evaluated immediately after the continue statement is executed In the for structure, the increment expression is executed, then the loopcontinuation test is evaluated

7. The break and continue Statements #include <iostream.h> Void main() { int sum = 0, num; // Allow the user to enter up to 10 numbers for (int count=0; count < 10; ++count) { cout << "Enter a number to add, or 0 to exit: "; cin >> num; // exit loop if user enters 0 if (num == 0) break; // otherwise add number to our sum sum += num; cout << "The sum of all the numbers you entered is " << sum << "\n";

7. The break and continue Statements #include <iostream.h> void main ( ) { while (true) { // infinite loop cout << "Enter 0 to exit or anything else to continue: "; int num; cin >> num; // exit loop if user enters 0 if (num == 0) break; cout << "We're out!\n";

7. The break and continue Statements #include <iostream.h> void main ( ) { for (int count=0; count < =20; ++count) { // if the number is divisible by 4, skip this iteration if ((count % 4) == 0) continue; // If the number is not divisible by 4, keep going cout << count << endl; This program prints all of the numbers from 0 to 20 that aren t divisible by 4.

8. Nested Control Structures Accept 10 numbers from the user & print the max. one #include <iostram.h> void main ( ) { int num, largest = 0 ; for ( int i = 0; i < 10; i ++ ) { cout << Enter a number: ; cin >> num ; if ( num > largest) { largest = num ; cout << The largest number is << largest << endl ;

8. Nested Control Structures Multiplication Table of 5 #include <iostram.h> void main ( ) { cout << \ t 1 \ t 2 \ t 3 \ t 4 \ t 5 ; << endl ; for ( int i = 1 ; i < = 5 ; i ++ ) { cout << i ; cout << \ t ; for ( int j = 1 ; j < = 5 ; j ++ ) { cout << i * j << \ t << ; cout << endl;

8. Nested Control Structures Multiplication Table of n #include <iostram.h> void main ( ) { cout << Please enter a number: ; cin >> n ; for ( int i = 1 ; i < = n ; i ++ ) { cout << i ; cout << \ t ; cout << endl ; for ( int j = 1 ; j < = n ; j ++ ) { cout << i ; cout << \ t ; for ( int k = 1 ; k < = n ; k ++ ) { cout << j * k << \ t << ; cout << endl;

8. Nested Control Structures * * * * * * * * * * * * * * * * * * * * * * * * * for (int i=1; i<=5; i++){ for (int j=1; j<=5; j++){ cout<<"*"; cout<<endl;

8. Nested Control Structures 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 for (int i=1; i<=5; i++){ for (int j=1; j<=5; j++){ cout<<i; cout<<endl;

8. Nested Control Structures 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 for (int i=1; i<=5; i++){ for (int j=1; j<=5; j++){ cout<<j; cout<<endl;

8. Nested Control Structures * * * * * * * * * * * * * * * for (int i=1; i<=5; i++){ for (int j=1; j<=i; j++){ cout<< * ; cout<<endl;

8. Nested Control Structures 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 for (int i=1; i<=5; i++){ for (int j=1; j<=i; j++){ cout<<j; cout<<endl;

8. Nested Control Structures 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 for (int i=1; i<=5; i++){ for (int j=1; j<=i; j++){ cout<<i; cout<<endl;