MUST CS101 Lab11

35
Lab Session 11 Repetition (part I) Prepared by T.A/ Ayman S. Abdelaziz

Transcript of MUST CS101 Lab11

Lab Session 11 Repetition (part I)

Prepared by T.A/ Ayman S. Abdelaziz

Lab RulesPut 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 OUTLINEfor Loopwhile loopdo …while loopLab TaskQuiz 02

Types of OperationsAlgorithms can be constructed for

• Sequential Operation (Previous)• Conditional Operation (Previous)• Iterative Operation (Today)

Operation of the for loop

for loop syntaxfor ( 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 syntaxwhile (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 Tasks

Lab Task 1Print your name 3 times using 1. for loop 2. while loop 3. do … while loop

More Applications

#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 setwusing 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 12 83 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: 5Factorial 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; }

1273314490

#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: 11Enter divisor: 3Quotient is 3, remainder is 2Do another? (y/n): yEnter dividend: 222Enter divisor: 17Quotient is 13, remainder is 1Do another? (y/n): n

Lab Tasks

Lab Task 2Write 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.MWED: 01:00 to 05:00 P.M

You can also contact me through:http://fb.com/[email protected]

35