Wednesday, 20 August 2014

Functionality Difference Between for loop & while loop

The “for loop” loops from one number to another number and increases by a specified value each time.
It uses the following Structure:-

for(start value; continue or end condition; increase value statement)


let we will take an example:-






We first start by setting the variable i to 0. This is  where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++).It return zero 


The while loop can be used if you don’t know how many times a loop must run.

let we will take an example:-




First you must always initialize the counter before the while loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the variable “howmuch”. If the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive.




0 comments:

Post a Comment