repetition structure - faculty.psau.edu.sa filecs-1301 lecture 7 repetition structure department of...
TRANSCRIPT
CS-1301
Lecture 7
Repetition Structure
Department of Computer Science
College of Arts and Science
Mr. Asad Abd Elrashid
1
Content
2
Introduction.
For loop.
While loop.
Do-while loop.
Challenge.
Introduction
3
Loops are used to execute any part of program
repeatedly.
Types of loops
For While Do - while
For loop
4
The most important looping structure in C++.
General syntax of :
for (initial ; condition ; increment )
statement or statements ;
initial, condition, and increment are C++ expressions.
Cont..
5
• For loops are executed as follows:
1. initial is evaluated. Usually an assignment statement.
2. condition is evaluated. Usually a relational expression.
3. If condition is false (i.e. 0), fall out of the loop (go to step 6.)
4. If condition is true (i.e. nonzero), execute statement
5. Execute increment and go back to step 2.
6. Next statement
Ways to write for loop
6
#include <iostream.h>
int main () {
int count,x,y;
int ctd;
/* 1. simple counted for loop */
for (count =1; count <=20; count++)
cout<<“\n"<<count;
/* 2. for loop counting backwards */
for (count = 100; count >0; count--) {
x*=count;
cout<<"count=“<<count<<“x=“<<x<<“\n";
}
Cont..
7
/* 3. for loop counting by 5's */
for (count=0; count<1000; count += 5)
y=y+count;
/* 4. initialization outside of loop */
count = 1;
for ( ; count < 1000; count++)
cout<< count;
Cont..
8
/* 5. very little need be in the for */
count=1; ctd=1;
for ( ; ctd; ) {
cout<<count;
count++; ctd=count<1000;
}
/* 6. compound statements for initialization and increment */
for (x=0, y=100; x<y; x++, y--) {
cout<< x<<y <<”\n";
}
return 0;
}
Example (1)
9
* Demonstrates a simple for statement */
#include <iostream.h>
int count;
int main( void )
{
/* Print the numbers 1 through 20 */
for (count = 1; count <= 20; count++)
cout<<count<<"\n";
return 0;
}
Example (2)
10
Write a C++ program to print numbers from(1 - 5)?
#include <stdio.h>
int i;
int main( )
{
/* Print the numbers 1 - 5 */
for (i=1;i<6;i++)
cout<<i;
return 0;
}
Challenge
11
Write a C++ program to print numbers from(5 - 1) by
using for loop?
Example(3)
12
Write a C++ program to print numbers from(1 - 10) by using
for loop?
#include <iostream.h>
#include <conio.h>
int i;
int main( void )
{
for (i=1;i<=10;i++)
{ cout<<"\n”<< i;
cout<<"\t\t”<<i*i<<”\n";}
getch();
return 0;
}
While loop
13
General syntax of :
while (condition)
{
statement;
Increment or decrement;
}
Cont..
14
Executes as expected:
1. condition is evaluated
2. If condition is false (i.e. 0), loop is exited (go to
step 5)
3. If condition is true (i.e. nonzero), statement is
executed
4. Go to step 1
5. Next statement
Cont..
15
Note:for ( ; condition ; ) is equivalent to while (condition)
stmt; stmt;
for (exp1; exp2; exp3) stmt;
is equivalent to
exp1;
while(exp2) { stmt; exp3; }
Example(4)
16
#include <iostream.h>
int n,i=0,sum=0;
int main( )
{cout<<“enter the limits:\n”;
cin>>n;
while(i<=n){
cout<<i;
sum = sum+i;
i++;}
return 0;
}
Do - while loop
17
General syntax of :
do
{
Statement or statements;
increment or decrement;
}
while(condition );
Example
18
#include <iostream.h>
char ch=’ ’;
int a,b=0;
int main( )
do{
cout<<“enter any two integer numbers:\n”;
cin<<a<<b;
cout<<“sum=“<<a+b<<“\n”;
cout<<“Do you want to continue (Y/N):\n”);
cin>>ch;
}while(ch != ‘n’)
return 0;}
challenge
19
1- Change the previous program to work with while
loop?
Thank you for your attention
20
Lecturer : Aasd Abd Elrashid