SlideShare a Scribd company logo
1 of 35
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
Lab Task 1
Print your name 3 times using
1. for loop
2. while loop
3. do … while loop
#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
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

What's hot

Teorical 1
Teorical 1Teorical 1
Teorical 1everblut
 
The Ring programming language version 1.5.3 book - Part 89 of 184
The Ring programming language version 1.5.3 book - Part 89 of 184The Ring programming language version 1.5.3 book - Part 89 of 184
The Ring programming language version 1.5.3 book - Part 89 of 184Mahmoud Samir Fayed
 
Chapter 5.3
Chapter 5.3Chapter 5.3
Chapter 5.3sotlsoc
 
The Ring programming language version 1.3 book - Part 59 of 88
The Ring programming language version 1.3 book - Part 59 of 88The Ring programming language version 1.3 book - Part 59 of 88
The Ring programming language version 1.3 book - Part 59 of 88Mahmoud Samir Fayed
 
Software Testing
Software TestingSoftware Testing
Software TestingLambert Lum
 
Architecture for Massively Parallel HDL Simulations
Architecture for Massively Parallel HDL Simulations Architecture for Massively Parallel HDL Simulations
Architecture for Massively Parallel HDL Simulations DVClub
 
Joy of Six - Discover the Joy of Perl 6
Joy of Six - Discover the Joy of Perl 6Joy of Six - Discover the Joy of Perl 6
Joy of Six - Discover the Joy of Perl 6trexy
 
The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 74 of 181
The Ring programming language version 1.5.2 book - Part 74 of 181The Ring programming language version 1.5.2 book - Part 74 of 181
The Ring programming language version 1.5.2 book - Part 74 of 181Mahmoud Samir Fayed
 
for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...hwbloom59
 

What's hot (20)

Teorical 1
Teorical 1Teorical 1
Teorical 1
 
The Ring programming language version 1.5.3 book - Part 89 of 184
The Ring programming language version 1.5.3 book - Part 89 of 184The Ring programming language version 1.5.3 book - Part 89 of 184
The Ring programming language version 1.5.3 book - Part 89 of 184
 
Chapter 5.3
Chapter 5.3Chapter 5.3
Chapter 5.3
 
The Ring programming language version 1.3 book - Part 59 of 88
The Ring programming language version 1.3 book - Part 59 of 88The Ring programming language version 1.3 book - Part 59 of 88
The Ring programming language version 1.3 book - Part 59 of 88
 
Unbounded
UnboundedUnbounded
Unbounded
 
Unbounded
UnboundedUnbounded
Unbounded
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Introducing to Asynchronous Programming
Introducing to Asynchronous  ProgrammingIntroducing to Asynchronous  Programming
Introducing to Asynchronous Programming
 
Hachiojipm11
Hachiojipm11Hachiojipm11
Hachiojipm11
 
EasyMock 101
EasyMock 101EasyMock 101
EasyMock 101
 
Architecture for Massively Parallel HDL Simulations
Architecture for Massively Parallel HDL Simulations Architecture for Massively Parallel HDL Simulations
Architecture for Massively Parallel HDL Simulations
 
Joy of Six - Discover the Joy of Perl 6
Joy of Six - Discover the Joy of Perl 6Joy of Six - Discover the Joy of Perl 6
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.7 book - Part 85 of 196The Ring programming language version 1.7 book - Part 85 of 196
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
The Ring programming language version 1.5.2 book - Part 74 of 181The Ring programming language version 1.5.2 book - Part 74 of 181
The Ring programming language version 1.5.2 book - Part 74 of 181
 
PHP 7
PHP 7PHP 7
PHP 7
 
for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...for this particular program how do i create the input innotepad 1st ? #includ...
for this particular program how do i create the input innotepad 1st ? #includ...
 
06 Loops
06 Loops06 Loops
06 Loops
 
Loops
LoopsLoops
Loops
 
basic program
basic programbasic program
basic program
 
Php arduino
Php arduinoPhp arduino
Php arduino
 

Similar to MUST CS101 Lab11

Similar to MUST CS101 Lab11 (20)

FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
C++ loop
C++ loop C++ loop
C++ loop
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
 
3 rd animation
3 rd animation3 rd animation
3 rd animation
 
c++ Lecture 4
c++ Lecture 4c++ Lecture 4
c++ Lecture 4
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 

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
  • 17.
  • 18. Lab Task 1 Print your name 3 times using 1. for loop 2. while loop 3. do … while loop
  • 19.
  • 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
  • 28.
  • 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