What is the meaning of loops and what's the difference? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the meaning of loops and what's the difference?

C++ explained to me about the while loop, for loop and do...while loop. But I'm still don't know the difference between all of it. Please guys, I need your help.

26th Oct 2017, 5:36 AM
Sherlock Hacker
Sherlock Hacker - avatar
3 Answers
+ 3
//#################### // for loop //#################### for(int i = 0; i < 10; i++) { // defines initial state, final state, and the action // to repeat in the loop definition. // ** LOOP BODY ** // } int i = 0; // initial state for(;i < 10; i++) // final state, action to repeat. { // initial state defined earlier, only final state and // action to repeat defined in loop definition. // ** LOOP BODY ** // } //#################### // while loop //#################### i = 0; // initial state while(i < 10) // final state { // while loop checks for final state BEFORE it // actually perform the iteration, like do...while // loop, action to repeat is written with the loop // body. // ** LOOP BODY ** // i++; // action to repeat } //#################### // do...while loop //#################### i = 0; // initial state do { // do...while loop checks for final state AFTER // it actually performs the iteration, like while // loop action to repeat is written with the loop // body. // Use do...while loop when you need // to do something first before checking it out, // e.g. to repeat asking for input while the input // given is still unqualified. // ** LOOP BODY ** // i++; // action to repeat } while(i <10) // final state //#################### // Conclusion //#################### The obvious difference between for loop and while or do...while loop is that for loops need not to place action to repeat within loop body. Each of the loop techniques has their time and place, practice, and you'll get to know which loop is to pick when. Hope that helps clear a bit of doubts. Hth, cmiiw
26th Oct 2017, 8:01 AM
Ipang
+ 3
You are free to use any form of loops. All of them are there for your convenience. Use 'while' when you have some condition, and want to execute a block of code while it is or isn't true. Use 'do-while' when you need a 'while' to be executed at least once. Use 'for' when you need to introduce a counter or iterator, and don't want it to exist outside of the loop… Same thing as with 'if' vs 'switch'.
26th Oct 2017, 6:08 AM
deFault
+ 2
At first there was assembler. It had only jumps to address. A lot of great bugs were introduced in that time. And then some wise man realized, that the jump doesn't make great readability, and makes code look like spaghetti. And so the macros were invented to organize the jumps into conditionals and loops. A lot of time passed since those times, the programs become more complicated, high level languages got invented. And those high level languages started to add some sugar to their syntax. And so complex constructions like if-then-else, switch, while, do-while and for were created to better describe the programmer intentions, and make their life easier. Jumps and go-to's got forgotten, new hipster things like for-each, range operators popped into existence, and here we are…
26th Oct 2017, 6:04 AM
deFault