Difference between revisions of "Convert this While to a For"

From CompSciWiki
Jump to: navigation, search
Line 3: Line 3:
 
|Problem= Almost any while loop can be converted into a for loop and vice versa.  In fact, the for loop can be described as a while loop that iterates for a set number of times. Take a look at the following sample program.  Change it so that it uses a while loop instead of a for loop.
 
|Problem= Almost any while loop can be converted into a for loop and vice versa.  In fact, the for loop can be described as a while loop that iterates for a set number of times. Take a look at the following sample program.  Change it so that it uses a while loop instead of a for loop.
  
 +
|SideSectionTitle=While and For Loops
 +
 +
|SideSection=
 +
[[Image:Wiki_loops03.jpg|center]]<BR>
  
  

Revision as of 10:23, 8 April 2010

Back to the Program-A-Day homepage

Problem

Almost any while loop can be converted into a for loop and vice versa. In fact, the for loop can be described as a while loop that iterates for a set number of times. Take a look at the following sample program. Change it so that it uses a while loop instead of a for loop.

 

While and For Loops

Wiki loops03.jpg

Solution

The solution...

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage