Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of...
Transcript of Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of...
![Page 1: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/1.jpg)
CS-1301
Lecture 7
Repetition Structure
Department of Computer Science
College of Arts and Science
Mr. Asad Abd Elrashid
1
![Page 2: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/2.jpg)
Content
2
Introduction.
For loop.
While loop.
Do-while loop.
Challenge.
![Page 3: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/3.jpg)
Introduction
3
Loops are used to execute any part of program
repeatedly.
Types of loops
For While Do - while
![Page 4: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/4.jpg)
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.
![Page 5: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/5.jpg)
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
![Page 6: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/6.jpg)
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";
}
![Page 7: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/7.jpg)
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;
![Page 8: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/8.jpg)
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;
}
![Page 9: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/9.jpg)
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;
}
![Page 10: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/10.jpg)
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;
}
![Page 11: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/11.jpg)
Challenge
11
Write a C++ program to print numbers from(5 - 1) by
using for loop?
![Page 12: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/12.jpg)
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;
}
![Page 13: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/13.jpg)
While loop
13
General syntax of :
while (condition)
{
statement;
Increment or decrement;
}
![Page 14: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/14.jpg)
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
![Page 15: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/15.jpg)
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; }
![Page 16: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/16.jpg)
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;
}
![Page 17: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/17.jpg)
Do - while loop
17
General syntax of :
do
{
Statement or statements;
increment or decrement;
}
while(condition );
![Page 18: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/18.jpg)
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;}
![Page 19: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/19.jpg)
challenge
19
1- Change the previous program to work with while
loop?
![Page 20: Repetition Structure - faculty.psau.edu.sa fileCS-1301 Lecture 7 Repetition Structure Department of Computer Science College of Arts and Science Mr. Asad Abd Elrashid a.Khalil@psau.edu.sa](https://reader033.fdocuments.in/reader033/viewer/2022041421/5e1f4031e3972a5c40771688/html5/thumbnails/20.jpg)
Thank you for your attention
20
Lecturer : Aasd Abd Elrashid