Computer Programming -1-

Slides:



Advertisements
Similar presentations
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Advertisements

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.
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.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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: Loops and Files.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
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.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
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.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
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.
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.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
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.
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.
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.
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)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 5: Loops. Slide 5- 2 Outline Increment and Decrement while loop do-while loop for loop.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
REPETITION CONTROL STRUCTURE
Unit 3 Lesson 9 Repetition Statements (Loops)
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 4B More Repetition Richard Gesick
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Loops October 10, 2017.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
Alternate Version of STARTING OUT WITH C++ 4th Edition
A LESSON IN LOOPING What is a loop?
do/while Selection Structure
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Programming Fundamental
Presentation transcript:

Computer Programming -1- Lecture 8

Iteration Statements for loop while loop do-while loop There may be a situation, when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. Iteration Statements : for loop while loop do-while loop

The for statement (for loop) The For Loop Statement : is an iteration statement that executes a set of code repeatedly, given the initial value, the condition ,increment or decrement value.

The for statement (for loop) The syntax of a for loop in C++ is: for(initialization ; condition ; increase or decrease) { statement; } Example : For ( int i=1 ; i<=10 ; i++) cout << " * “ ; Output : **********

Examples : * Print numbers from 1 to 20 : for ( int j=1 ; j<=20 ; j++) cout << j << "\t" ; Output : 1 2 3 4 5 …………….. 20 * Print numbers from 20 to 1 : for ( int j=20 ; j>=1 ; j--) Output : 20 19 18 17 …………….. 1 * Even number : for ( int i=2 ; i<=20 ; i+=2 ) cout << i << "\t" ; Output : 2 4 6 8 10 …………….. 20 * Odd number : for ( int i=1 ; i<=20 ; i+=2) Output : 1 3 5 7 9 …………….. 19

- Write a C++ program by using for statement to print only odd numbers from 1 to 100 #include <iostream.h> int main () { for ( int i=1 ; i<100 ; i+=2 ) cout << i << endl ; return 0 ; }

- Write a C++ program by using for statement to print characters from A to Z #include <iostream.h> int main () { for ( char i='A' ; i<='Z' ; i++ ) cout << i << " , " ; return 0 ; }

- Write a C++ program by using for statement to print the Even numbers from 0 to (n) numbers. #include <iostream.h> int main() { int n; cout<<"Enter the number : "; cin>>n; for (int i=0 ; i<=n ; i+=2 ) cout << i << "\t"; return 0; }

- Write a program to calculate the sum of numbers from 1 to n numbers - Write a program to calculate the sum of numbers from 1 to n numbers? (n: any number enter by user ) #include <iostream.h> int main() { int n; float sum; sum=0; cout<<"Enter the number : "; cin>>n; for (int j=1 ; j<=n ; j++ ) sum=sum+j ; } cout << "the sum of numbers = "<< sum << endl; return 0;

Nested for loops Output: ***** **** *** ** * Example : #include <iostream.h> int main() { for(int i=5 ; i>=1 ; i--) for(int j=1 ; j<=i ; j++) cout << " * " << " " ; cout << endl; } return 0 ; Output: ***** **** *** ** *

Nested for loops ***** Output : Example : #include <iostream.h> int main() { for ( int i=5 ; i>=1 ; i--) for(int x=1 ; x<=5 ; x++) cout << "*" <<" "; cout << endl; } return 0 ; Output : *****

While Loop The while loop : allows programs to repeat a statement or series of statements, as long as a certain test condition is true.

While Loop a=1 while (a<=10) cout << " * " ; a++; The syntax of a While loop in C++ is: initializing while (condition) { statements; increment or decrement value; } Example : a=1 while (a<=10) cout << " * " ; a++; Output : **********

Examples : * Print numbers from 1 to 10 : int i=1; while(i<=10) { cout<<i<<" ,"; i++; } * Print numbers from 10 to 1 : int i=10; while(i>=1) i--;

- Write a C++ program by using while loop statement to print only even numbers from 0 to 100 #include<iostream.h> int main() { int i=0; while(i<=100) cout<<i<<endl; i+=2; } return 0;

- Write a C++ program by using while loop statement to find the sum of odd numbers from 1 to 100. #include<iostream.h> int main() { int i=1 , sum=0; while(i<=100) sum =sum + i; i+=2; } cout<<" the sum is : " << sum << endl; return 0;

- Write a C++ program by using while statement to find the square and cubic values of the numbers ( the numbers is entered by user ..until the user ask for termination ) #include<iostream.h> int main() { int a; cout << " To stop work of the program enter the number (0) " <<endl ; while(a != 0) cout << " enter the number : " ; cin >> a ; cout << " square = " << a*a <<endl; cout << " cubic = " << a*a*a <<endl; } return 0;

Do while Loop The do-while loop: is similar to the while loop, except that the test condition occurs at the end of the loop. The syntax of a While loop in C++ is: do { statement; increment or decrement value; } while(condition);

- Write a C++ program by using do while statement to print numbers from 1 to 10 #include<iostream.h> main() { int i=1; do cout<<i<<endl; i++; } while(i<=10); return 0;

- Write a C++ program by using do while statement to print only even numbers from 20 to 1 #include<iostream.h> main() { int i=20; do cout<<i<<endl; i-=2; } while(i>=1); return 0;

- Write a C++ program by using do while statement to request the user to enter any number and exit if input is equal 6 .. #include<iostream.h> main() { int a; do cout << " enter the number : " ; cin >> a ; } while(a != 6); return 0;

Deciding Which Loop to Use while: pretest loop; loop body may not be executed at all do-while: posttest loop; loop body will always be executed at least once for: pretest loop with initialization and update expression; useful with counters, or if precise number of repetitions is needed

Nested Loops A nested loop is a loop inside the body of another loop Inner (inside), outer (outside) loops: for (row=1; row<=3; row++) //outer { for (col=1; col<=3; col++)//inner cout << row * col <<“\t”; cout<<endl; }

Lines from Program 5-16

Nested Loops - Notes Inner loop goes through all repetitions for each repetition of outer loop Inner loop repetitions complete sooner than outer loop Total number of repetitions for inner loop is product of number of repetitions of the two loops.

Breaking Out of a Loop Can use break to terminate execution of a loop Use sparingly if at all – makes code harder to understand and debug When used in an inner loop, terminates that loop only and goes back to outer loop

The continue Statement Can use continue to go to end of loop and prepare for next repetition while, do-while loops: go to test, repeat loop if test passes for loop: perform update step, then test, then repeat loop if test passes Use sparingly – like break, can make program logic hard to follow