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

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

View Full Document Right Arrow Icon
WORKSHOP 5 ~ FOR LOOPS CSIS 1117A 2009/10/22
Background image of page 1

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

View Full DocumentRight Arrow Icon
OUTLINE For Loops Increment of a variable Workshop Exercises
Background image of page 2
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)
Background image of page 3

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

View Full DocumentRight Arrow Icon
SIMPLE FOR LOOP Calculate n * x for (int i=0; i<n; i++)  sum = sum + x; (w5e1.cpp)
Background image of page 4
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)
Background image of page 5

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

View Full DocumentRight Arrow Icon
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)
Background image of page 6
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)
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
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 Right Arrow Icon
Ask a homework question - tutors are online