# W5 - WORKSHOP5~FORLOOPS CSIS1117A2009/10/22 OUTLINE ForLoops

This preview shows pages 1–8. Sign up to view the full content.

WORKSHOP 5 ~ FOR LOOPS CSIS 1117A 2009/10/22

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
OUTLINE For Loops Increment of a variable Workshop Exercises
FOR LOOPS Syntax for (var. init; condition; var. update) {…} Example: Print 10 * on the screen for (int i=1; i<=10; i=i+1)       cout << “*” << endl; (w5e0.cpp)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
SIMPLE FOR LOOP Calculate n * x for (int i=0; i<n; i++)  sum = sum + x; (w5e1.cpp)
MORE FOR LOOPS Example: Print the number from 0 to 9 for (int i=0; i<10; i=i+1)       cout << i << endl; (w5e2.cpp)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
FOR LOOPS We can not only increment the loop counter, but  also decrement the loop counter Example: Print the number from 10 to 1 for (int i=10; i>0; i=i-1)       cout << i << endl; (w5e3.cpp)
MORE COMPLICATED FOR LOOPS Calculate n! i.e. n(n-1)(n-2)…1 for (int i=0; i<n; i=i+1) result = result * (i+1); (w5e4.cpp – based on factorial.cc)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 05/04/2011.

### Page1 / 14

W5 - WORKSHOP5~FORLOOPS CSIS1117A2009/10/22 OUTLINE ForLoops

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online