Control Structures Repetition

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
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.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
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.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
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.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
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.
Computer Programming -1-
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Structures
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 7: Repeating a Known Number of Times
Loop Structures.
Quick Test What do you mean by pre-test and post-test loops in C?
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Control Statements Kingdom of Saudi Arabia
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.
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.
While and Do While Loops
Arrays, For loop While loop Do while loop
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Outline Altering flow of control Boolean expressions
LRobot Game.
Introduction to Algorithms and Programming COMP151
Loops in C.
Repetition Control Structure
Faculty of Computer Science & Information System
Chapter 6: Repetition Statements
Computing Fundamentals
Introduction to Programming – 4 Operators
Control Structures Part 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
Computer programming Lecture 3.
Logical Operators and While Loops
Objectives You should be able to describe: The while Statement
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Control Structures Selection
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Introduction to Algorithms - 2
Programming Fundamental
Programming Fundamental
Introduction to Algorithms - 2
Presentation transcript:

Control Structures Repetition Dr. Khizar Hayat Associate Prof. of Computer Science

Repetition Algorithm to sum five numbers input by the user Start count 0 sum 0 Repetition Read Num count count + 1 sum sum + Num count = 5? No Yes Display sum as total Stop

Loops Loops have as a purpose to repeat some statement(s) a certain number of times if a given condition is true. There are three types of loops in C++: while loops do-while loops for loops

The while loop Its format is: while (condition) { statement 1; statement 2; statement 3; : } This while loop executes as long as the logical expression between parenthesis (condition) is true. When the condition evaluates to false, the loop is exited , i.e. the execution continues with the statement following the loop block. The condition is tested at the beginning of the loop and if false, the loop body will not be executed at all. For just one statement inside the loop, no need of{}. Note that statement(s) inside a while loop may execute 0 or more times

The while loop Example //custom countdown using while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; while(n>0) cout<<n<<“,"; --n; //n=n-1; } cout<<“FIRE"; return(0);

Repetition Algorithm to sum five numbers input by the user begin Start sum 0 begin set sum0 set count0 while count!=5 get num set countcount+1 set sumsum+num end while display sum as the total end count  0 count = 5? Yes No Read Num Display sum as total count count + 1 Repetition sum sum + Num Stop

Algorithm to sum five numbers input by the user using the while loop #include<iostream> using namespace std; int main() { int sum=0,count=0,num; while(count!=5) cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; count=count+1; } cout<<“\nThe sum is"<<sum<<endl; return(0);

The for loop Note that statement(s) inside a for loop may execute 0 or more times

Repetition Algorithm to sum five numbers input by the user begin Start sum 0 begin set sum0 set count0 for (count=0;count!=5;count++) get num set countcount+1 set sumsum+num end for display sum as the total end count  0 count = 5? Yes No Read Num Display sum as total count count + 1 Repetition sum sum + Num Stop

The for loop for ( /*initialization*/ ; /*condition*/; /*increment or decrement*/) { statement(s); } The initialization step is executed first, and only once. This step allows you to optionally declare and initialize any loop control variables. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop. After the body of the for loop executes, the flow of control jumps back up to the increment/decrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

The for loop Example //custom countdown using while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; for(;n>0;--n) cout<<n<<“,"; } cout<<“FIRE"; return(0);

Algorithm to sum five numbers input by the user using the for loop #include<iostream> using namespace std; int main() { int sum=0,count=0,num; for(;count!=5;count++) cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; } cout<<“\nThe sum is"<<sum<<endl; return(0);

Algorithm to sum five numbers input by the user using the for loop #include<iostream> using namespace std; int main() { int sum=0,num,count; for(count=0;count!=5;count++) cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; } cout<<“\nThe sum is"<<sum<<endl; return(0);

Algorithm to sum five numbers input by the user using the for loop #include<iostream> using namespace std; int main() { int sum=0,num; for(int count=0;count!=5;count++) cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; } cout<<“\nThe sum is"<<sum<<endl; return(0);

The do-while loop do { statement(s); //executed at least once } while( /*condition*/) ; // Do not forget the semicolon here Unlike the for and while loops, the do-while loop guarantees at least one execution, even if the condition is false at the start. The do-while loop is usually used when the condition is within the loop statement itself.

Repetition Algorithm to sum five numbers input by the user begin Start sum 0 begin set count0 set sum0 do get num set countcount+1 set sumsum+num while count!=5 end do display sum as the total end count  0 Read Num count count + 1 sum sum + Num count = 5? No Repetition Yes Display sum as total Stop

Algorithm to sum five numbers input by the user using the do-while loop #include<iostream> using namespace std; int main() { int sum=0,count=0,num; do cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; count=count+1; } while(count!=5); cout<<“\nThe sum is"<<sum<<endl; return(0);

The for loop The countdown Example //custom countdown using while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; for(;n>0;--n) cout<<n<<“,"; } cout<<“FIRE"; return(0);

The while loop The countdown Example //custom countdown using while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; while(n>0) cout<<n<<“,"; --n; //n=n-1; } cout<<“FIRE"; return(0);

The do-while loop The countdown Example //custom countdown using do-while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; do cout<<n<<“,"; --n; //n=n-1; } while(n>0);//don’t forget the semicolon cout<<“FIRE"; return(0);

Examples Factorial of a number Ex 10 from notes (page 28) Max/min among n numbers