Jump to content

About for and while loops


Recommended Posts

Can you tell me what is the REAL difference of FOR LOOP and WHILE LOOP?


I know both of them has to be executed until the condition is false..same thing when you're saying "It will run a specified number of times". Ive been looking around for answers over the internet but people keeps on saying that :


while loop - used for looping until a condition is satisfied and when it is unsure how many times the code should be in loop <------ HUH? even in FOR LOOPS it will run until a condition is satisfied or results to false. And in FOR LOOPS you're not also sure how many times it will run, that is if you didn't use a number.


for loop - used for looping until a condition is satisfied but it is used when you know how many times the code needs to be in loop.


I hope someone can give me a clear and precise answer. Thanks!

Link to comment
Share on other sites

To be honest, it has been a long time and I forget the specific nerd nuances between the two loop types.


Off hand though, I know that with a WHILE loop, you don't have initialization and declaration of variables within the declaration of the loop ... as you do with FOR loops.


With WHILE, you have to declare your variables beforehand and the increment/decrement the variables WITHIN the statement block. Yes, this is nerdy stuff!




So let's translate that into English: WHILE loops look one way, FOR loops look another - but they do the same thing!




For most programmers, it comes down to personal preference whether to use a FOR loop vs. a WHILE.


... I wouldn't get too concerned about it.



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...