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.
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
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
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
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 << " ";
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