Tuesday, 14 May 2013

How to implement Loops

Let me explain basics

Their are three important loops are there: for,do,while.

For loop we use mostly in JavaScript, Hence I took this example.

Example programs in c language

for(i=1;i<=2;i++)
{
printf("Message for you ");
printf("\n");
}

Actually for loop is Algorithm, It will satisfy our needs.

How to understand this

Iteration1: i=1 -------------> ;1<=2 ------------> Condition satisfied ;Hence first time no increment

on Screen:Message for you

Iteration2: Now observe this two only(  ;i<=2; i++)
From Iteration2 it will start from last
i++---> You increment i, It contains 1 in previous Iteration, becz increment i=2

Now

2<=2 condition satisfied

on Screen:Message for you

Iteration3: Again  observe this two only(  ;i<=2; i++)

i++  -->You increment i, It contains 2 in previous Iteration, becz increment i=3

Now

3<=2 Condition not satisfied

exit from loop no on Screen message

on Screen
----------------------
Message for you
Message for you

If you are not understood FOR LOOP above please follow this, I have created easy way
http://javascriptwithshrinivas.blogspot.com/2013/06/how-to-understand-for-loops-in.html



No comments:

Post a Comment