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

MUST CS101 Lab11

  • 1.
  • 2.
    Lab Rules  Putyour 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  forLoop  while loop  do …while loop  Lab Task  Quiz 02
  • 4.
    Types of Operations Algorithmscan be constructed for • Sequential Operation (Previous) • Conditional Operation (Previous) • Iterative Operation (Today)
  • 5.
  • 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; }
  • 7.
  • 8.
    #include <iostream> using namespacestd; 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 thewhile loop
  • 10.
    while loop syntax while(test expression) statement 1; while (test expression) { statement 1; statement 2; statement 3; }
  • 11.
  • 12.
    #include <iostream> using namespacestd; 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 thedo…while loop
  • 14.
    do…while loop syntax do statement1; while (test expression); do { statement 1; statement 2; statement 3; } while (test expression);
  • 15.
  • 16.
    #include <iostream> using namespacestd; 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 Printyour name 3 times using 1. for loop 2. while loop 3. do … while loop
  • 20.
    #include <iostream> using namespacestd; 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 namespacestd; 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 namespacestd; 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 namespacestd; 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 namespacestd; 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 namespacestd; 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 namespacestd; 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 Writeequivalent 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  Willbe published today on our facebook group.
  • 31.
    Next Lab Repetition (part2) Nested Loops Other Control Statements (break & continue) More Advanced Applications (conditions within loops)
  • 32.
    How to findyour 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 Programmingin 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 contactme?  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 [email protected]
  • 35.