SlideShare a Scribd company logo
Prepared by
T.A/ Ayman S. Abdelaziz
Lab Rules
 Put your cell phone in silent mode or switch it off.
 Shut down your PC monitor, tablet or laptop.
 Take a permission first before you leave or enter the
lab.
 Don’t write any attendance sheet, I will do this at the
end of our session.
 Don’t talk with your colleagues.
Session OUTLINE
 for Loop
 while loop
 do …while loop
 Lab Task
 Quiz 02
Types of Operations
Algorithms can be constructed for
• Sequential Operation (Previous)
• Conditional Operation (Previous)
• Iterative Operation (Today)
Operation of the for loop
for loop syntax
for ( initialization statement; test expression; update statement )
statement 1;
for ( initialization statement; test expression; update statement )
{ statement 1;
statement 2;
statement 3;
}
for loop syntax
#include <iostream>
using namespace std;
void main()
{
for(int a=10; a<20 ; a++)
cout<<"value of a: "<< a << endl;
}
Demonstrate for Loop
value of a: 10
value of a: 1 1
value of a: 1 2
value of a: 1 3
value of a: 1 4
value of a: 1 5
value of a: 1 6
value of a: 1 7
value of a: 1 8
value of a: 1 9
Operation of the while loop
while loop syntax
while (test expression)
statement 1;
while (test expression)
{ statement 1;
statement 2;
statement 3;
}
while loop syntax
#include <iostream>
using namespace std;
void main()
{
int a = 10;
while(a < 20)
{
cout<<"value of a: "<< a << endl;
a++;
}
}
Demonstrate while Loop
value of a: 10
value of a: 1 1
value of a: 1 2
value of a: 1 3
value of a: 1 4
value of a: 1 5
value of a: 1 6
value of a: 1 7
value of a: 1 8
value of a: 1 9
Operation of the do…while loop
do…while loop syntax
do
statement 1;
while (test expression);
do
{
statement 1;
statement 2;
statement 3;
}
while (test expression);
do…while loop syntax
#include <iostream>
using namespace std;
void main()
{
int a = 10;
do
{
cout<<"value of a: "<< a << endl;
a = a + 1;
} while(a < 20);
}
value of a: 10
value of a: 1 1
value of a: 1 2
value of a: 1 3
value of a: 1 4
value of a: 1 5
value of a: 1 6
value of a: 1 7
value of a: 1 8
value of a: 1 9
Demonstrate do…while Loop
MUST CS101 Lab11
Lab Task 1
Print your name 3 times using
1. for loop
2. while loop
3. do … while loop
MUST CS101 Lab11
#include <iostream>
using namespace std;
void main()
{
for(int n=10; n>0; n--)
{
cout<<n<<", ";
}
cout<<"FIRE!n";
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1 FIRE!
#include <iostream>
using namespace std;
void main()
{
int n;
cout<<"Enter the starting number> ";
cin>> n;
while(n>0)
{
cout<<n<<", ";
n--;
}
cout<<"FIRE!n";
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1 FIRE!
#include <iostream>
using namespace std;
void main()
{
int sum = 0;
for(int i=1; i<=3; i++)
{
sum += i;
}
cout<<"Sum is "<<sum<<endl;
}
Sum is 6
#include <iostream>
using namespace std;
int main()
{
int j; //define a loop variable
for(j=0; j<3; j++) //loop from 0 to 2,
cout << j * j << " "; //displaying the square of j
cout << endl;
return 0;
}
Slide 3- 23
0 1 4
#include <iostream>
#include <iomanip> //for setw
using namespace std;
int main()
{
int numb; //define loop variable
for(numb=1; numb<=3; numb++) //loop from 1 to 3
{
cout << setw(4) << numb; //display 1st column
int cube = numb*numb*numb; //calculate cube
cout << setw(6) << cube << endl; //display 2nd column
}
return 0;
}
1 1
2 8
3 27
#include <iostream>
using namespace std;
int main()
{
int numb;
long fact=1; //long for larger numbers
cout << "Enter a number: ";
cin >> numb; //get number
for(int j=numb; j>0; j--) //multiply 1 by
fact *= j; //numb, numb-1, ..., 2, 1
cout << "Factorial is " << fact << endl;
return 0;
}
Enter a number: 5
Factorial is 120
#include <iostream>
using namespace std;
int main()
{
int n = 99; // make sure n isn't initialized to 0
while( n != 0 ) // loop until n is 0
cin >> n; // read a number into n
cout << endl;
return 0;
}
1
27
33
144
9
0
#include <iostream>
using namespace std;
int main()
{
long dividend, divisor;
char ch;
do //start of do loop
{ //do some processing
cout << "Enter dividend: "; cin >> dividend;
cout << "Enter divisor: "; cin >> divisor;
cout << "Quotient is " << dividend / divisor;
cout << ", remainder is " << dividend % divisor;
cout << "nDo another? (y/n): "; //do it again?
cin >> ch;
}
while( ch != 'n' ); //loop condition
return 0;
}
Enter dividend: 11
Enter divisor: 3
Quotient is 3, remainder is 2
Do another? (y/n): y
Enter dividend: 222
Enter divisor: 17
Quotient is 13, remainder is 1
Do another? (y/n): n
MUST CS101 Lab11
Lab Task 2
Write equivalent statements for the following C++ program
fragment using while loop, and determine the output without
using any C++ compiler.
for(int j=0; j<3 ; j++)
cout << j * j << " ";
Home Assignment
 Will be published today on our facebook group.
Next Lab
Repetition (part 2)
Nested Loops
Other Control Statements (break & continue)
More Advanced Applications (conditions within loops)
How to find your course materials?
Via Facebook:
 Join this group:
https://www.facebook.com/groups/CS101.2015/
 And also this group:
http://facebook.com/groups/Must.CS101/
References
 Object-Oriented Programming in C++. 4th Edition, Robert
Lafore
 Introduction to Programming with C++, 2nd Edition, Y.
Daniel Liang
 Problem Analysis to Program Design, 3rd Edition
How to contact me?
 Office hours
TUE: 03:00 to 05:00 P.M
WED: 01:00 to 05:00 P.M
 You can also contact me through:
http://fb.com/ayman.shamel
Ayman_shamel@hotmail.com
35

More Related Content

PDF
Lecture 4
PPT
c++ Lecture 2
PDF
PDF
PPSX
Nested loops
PPTX
Theorical 1
PDF
Asynchronen Code testen
KEY
Yapcasia2011 - Hello Embed Perl
Lecture 4
c++ Lecture 2
Nested loops
Theorical 1
Asynchronen Code testen
Yapcasia2011 - Hello Embed Perl

What's hot (20)

PPTX
Teorical 1
PDF
The Ring programming language version 1.5.3 book - Part 89 of 184
PPTX
Chapter 5.3
PDF
The Ring programming language version 1.3 book - Part 59 of 88
PPS
Unbounded
PPT
Unbounded
ODP
Software Testing
PPTX
Introducing to Asynchronous Programming
PDF
Hachiojipm11
PDF
EasyMock 101
PDF
Architecture for Massively Parallel HDL Simulations
PPTX
Joy of Six - Discover the Joy of Perl 6
PDF
The Ring programming language version 1.7 book - Part 85 of 196
PDF
The Ring programming language version 1.5.2 book - Part 74 of 181
PPTX
PDF
for this particular program how do i create the input innotepad 1st ? #includ...
PPT
06 Loops
PPTX
Loops
PPTX
basic program
PDF
Php arduino
Teorical 1
The Ring programming language version 1.5.3 book - Part 89 of 184
Chapter 5.3
The Ring programming language version 1.3 book - Part 59 of 88
Unbounded
Unbounded
Software Testing
Introducing to Asynchronous Programming
Hachiojipm11
EasyMock 101
Architecture for Massively Parallel HDL Simulations
Joy of Six - Discover the Joy of Perl 6
The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.5.2 book - Part 74 of 181
for this particular program how do i create the input innotepad 1st ? #includ...
06 Loops
Loops
basic program
Php arduino
Ad

Similar to MUST CS101 Lab11 (20)

PPT
FP 201 - Unit 3 Part 2
PPTX
Oop object oriented programing topics
PPTX
C++ loop
PDF
4th_Ed_Ch03.pdf
PPTX
Cs1123 6 loops
PPTX
Lec7 - Loops updated.pptx
PDF
C++ TUTORIAL 3
PPT
12 lec 12 loop
PDF
C++ L03-Control Structure
PPT
ch5_additional.ppt
PDF
Chapter 3 Computer Programmingodp-1_250331_041044.pdf
PDF
04-Looping( For , while and do while looping) .pdf
PDF
Object Oriented Programming (OOP) using C++ - Lecture 5
PDF
3 rd animation
PDF
c++ Lecture 4
PPTX
lesson 2.pptx
PPTX
Presentation ghgytrt frtr54fdcvccf1.pptx
PDF
how to reuse code
PPT
Java căn bản - Chapter6
PPT
Week2 ch4 part1edited 2020
FP 201 - Unit 3 Part 2
Oop object oriented programing topics
C++ loop
4th_Ed_Ch03.pdf
Cs1123 6 loops
Lec7 - Loops updated.pptx
C++ TUTORIAL 3
12 lec 12 loop
C++ L03-Control Structure
ch5_additional.ppt
Chapter 3 Computer Programmingodp-1_250331_041044.pdf
04-Looping( For , while and do while looping) .pdf
Object Oriented Programming (OOP) using C++ - Lecture 5
3 rd animation
c++ Lecture 4
lesson 2.pptx
Presentation ghgytrt frtr54fdcvccf1.pptx
how to reuse code
Java căn bản - Chapter6
Week2 ch4 part1edited 2020
Ad

Recently uploaded (20)

PPTX
master seminar digital applications in india
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Types and Its function , kingdom of life
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Sports Quiz easy sports quiz sports quiz
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Pre independence Education in Inndia.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
PPH.pptx obstetrics and gynecology in nursing
master seminar digital applications in india
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
Cell Types and Its function , kingdom of life
O5-L3 Freight Transport Ops (International) V1.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Sports Quiz easy sports quiz sports quiz
O7-L3 Supply Chain Operations - ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
102 student loan defaulters named and shamed – Is someone you know on the list?
Pre independence Education in Inndia.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Anesthesia in Laparoscopic Surgery in India
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Renaissance Architecture: A Journey from Faith to Humanism
PPH.pptx obstetrics and gynecology in nursing

MUST CS101 Lab11

  • 1. Prepared by T.A/ Ayman S. Abdelaziz
  • 2. Lab Rules  Put your cell phone in silent mode or switch it off.  Shut down your PC monitor, tablet or laptop.  Take a permission first before you leave or enter the lab.  Don’t write any attendance sheet, I will do this at the end of our session.  Don’t talk with your colleagues.
  • 3. Session OUTLINE  for Loop  while loop  do …while loop  Lab Task  Quiz 02
  • 4. Types of Operations Algorithms can be constructed for • Sequential Operation (Previous) • Conditional Operation (Previous) • Iterative Operation (Today)
  • 5. Operation of the for loop
  • 6. for loop syntax for ( initialization statement; test expression; update statement ) statement 1; for ( initialization statement; test expression; update statement ) { statement 1; statement 2; statement 3; }
  • 8. #include <iostream> using namespace std; void main() { for(int a=10; a<20 ; a++) cout<<"value of a: "<< a << endl; } Demonstrate for Loop value of a: 10 value of a: 1 1 value of a: 1 2 value of a: 1 3 value of a: 1 4 value of a: 1 5 value of a: 1 6 value of a: 1 7 value of a: 1 8 value of a: 1 9
  • 9. Operation of the while loop
  • 10. while loop syntax while (test expression) statement 1; while (test expression) { statement 1; statement 2; statement 3; }
  • 12. #include <iostream> using namespace std; void main() { int a = 10; while(a < 20) { cout<<"value of a: "<< a << endl; a++; } } Demonstrate while Loop value of a: 10 value of a: 1 1 value of a: 1 2 value of a: 1 3 value of a: 1 4 value of a: 1 5 value of a: 1 6 value of a: 1 7 value of a: 1 8 value of a: 1 9
  • 13. Operation of the do…while loop
  • 14. do…while loop syntax do statement 1; while (test expression); do { statement 1; statement 2; statement 3; } while (test expression);
  • 16. #include <iostream> using namespace std; void main() { int a = 10; do { cout<<"value of a: "<< a << endl; a = a + 1; } while(a < 20); } value of a: 10 value of a: 1 1 value of a: 1 2 value of a: 1 3 value of a: 1 4 value of a: 1 5 value of a: 1 6 value of a: 1 7 value of a: 1 8 value of a: 1 9 Demonstrate do…while Loop
  • 18. Lab Task 1 Print your name 3 times using 1. for loop 2. while loop 3. do … while loop
  • 20. #include <iostream> using namespace std; void main() { for(int n=10; n>0; n--) { cout<<n<<", "; } cout<<"FIRE!n"; } 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 FIRE!
  • 21. #include <iostream> using namespace std; void main() { int n; cout<<"Enter the starting number> "; cin>> n; while(n>0) { cout<<n<<", "; n--; } cout<<"FIRE!n"; } 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 FIRE!
  • 22. #include <iostream> using namespace std; void main() { int sum = 0; for(int i=1; i<=3; i++) { sum += i; } cout<<"Sum is "<<sum<<endl; } Sum is 6
  • 23. #include <iostream> using namespace std; int main() { int j; //define a loop variable for(j=0; j<3; j++) //loop from 0 to 2, cout << j * j << " "; //displaying the square of j cout << endl; return 0; } Slide 3- 23 0 1 4
  • 24. #include <iostream> #include <iomanip> //for setw using namespace std; int main() { int numb; //define loop variable for(numb=1; numb<=3; numb++) //loop from 1 to 3 { cout << setw(4) << numb; //display 1st column int cube = numb*numb*numb; //calculate cube cout << setw(6) << cube << endl; //display 2nd column } return 0; } 1 1 2 8 3 27
  • 25. #include <iostream> using namespace std; int main() { int numb; long fact=1; //long for larger numbers cout << "Enter a number: "; cin >> numb; //get number for(int j=numb; j>0; j--) //multiply 1 by fact *= j; //numb, numb-1, ..., 2, 1 cout << "Factorial is " << fact << endl; return 0; } Enter a number: 5 Factorial is 120
  • 26. #include <iostream> using namespace std; int main() { int n = 99; // make sure n isn't initialized to 0 while( n != 0 ) // loop until n is 0 cin >> n; // read a number into n cout << endl; return 0; } 1 27 33 144 9 0
  • 27. #include <iostream> using namespace std; int main() { long dividend, divisor; char ch; do //start of do loop { //do some processing cout << "Enter dividend: "; cin >> dividend; cout << "Enter divisor: "; cin >> divisor; cout << "Quotient is " << dividend / divisor; cout << ", remainder is " << dividend % divisor; cout << "nDo another? (y/n): "; //do it again? cin >> ch; } while( ch != 'n' ); //loop condition return 0; } Enter dividend: 11 Enter divisor: 3 Quotient is 3, remainder is 2 Do another? (y/n): y Enter dividend: 222 Enter divisor: 17 Quotient is 13, remainder is 1 Do another? (y/n): n
  • 29. Lab Task 2 Write equivalent statements for the following C++ program fragment using while loop, and determine the output without using any C++ compiler. for(int j=0; j<3 ; j++) cout << j * j << " ";
  • 30. Home Assignment  Will be published today on our facebook group.
  • 31. Next Lab Repetition (part 2) Nested Loops Other Control Statements (break & continue) More Advanced Applications (conditions within loops)
  • 32. How to find your course materials? Via Facebook:  Join this group: https://www.facebook.com/groups/CS101.2015/  And also this group: http://facebook.com/groups/Must.CS101/
  • 33. References  Object-Oriented Programming in C++. 4th Edition, Robert Lafore  Introduction to Programming with C++, 2nd Edition, Y. Daniel Liang  Problem Analysis to Program Design, 3rd Edition
  • 34. How to contact me?  Office hours TUE: 03:00 to 05:00 P.M WED: 01:00 to 05:00 P.M  You can also contact me through: http://fb.com/ayman.shamel Ayman_shamel@hotmail.com
  • 35. 35