Jump to content

Help in loops in c++

Go to solution Solved by LukeTim,

Hey People! So long time ago I missed a CS Class in which loops were taught. I worked around with the while and the for loops. But I didn't try to read DO WHILE LOOP, which costed me 4 points  in my today's exam. So, I just wanna have a small discussion over the working of it.

A small example (a small c++ program ) would help! :)

 

Thanks! 

The do while loop works the same as a while loop, only it is guaranteed to run at least once. In other words, the condition is checked after the loop has run, not before.

This is clear in the way it is written (in C/C++ at least):

i = 0;do {    i++;} while(i < 5)

The statement(s) to be executed in the loop come before the conditional.

To give an example of how while and do while can differ:

 

i = 2while(i < 2) //will not enter loop{    i++;}//i == 2do {    i++;} while(i < 2) //runs once, then terminates//i == 3

Hey People! So long time ago I missed a CS Class in which loops were taught. I worked around with the while and the for loops. But I didn't try to read DO WHILE LOOP, which costed me 4 points  in my today's exam. So, I just wanna have a small discussion over the working of it.

A small example (a small c++ program ) would help! :)

 

Thanks! 

 [spoiler=CORMAC]CPU:Intel celeron 1.6ghz RAM:Kingston 400mhz 1.99gb MOBO:MSI G31TM-P21 GPU:Will add one later on! CASE:local ROUTER D-Link 2750U, D-LINK 2730U MOUSE:HP,DELL,ViP KEYBOARD: v7 SPEAKERS:Creative 245  MONITOR:AOC E970Sw HEADSET: Sony MDRx05s UPS:conex ups avr 500va PSU:idk OD:Samsung super writemaster STORAGE:80 gb seagate+ Seagate 1TB OS:Windows xp sp3 themed to Windows 7 + Linux |Rest all pc in my house will be updated from time-time

COMING SOON

 

 

Link to comment
https://linustechtips.com/topic/526411-help-in-loops-in-c/
Share on other sites

Link to post
Share on other sites

Hey People! So long time ago I missed a CS Class in which loops were taught. I worked around with the while and the for loops. But I didn't try to read DO WHILE LOOP, which costed me 4 points  in my today's exam. So, I just wanna have a small discussion over the working of it.

A small example (a small c++ program ) would help! :)

 

Thanks! 

The do while loop works the same as a while loop, only it is guaranteed to run at least once. In other words, the condition is checked after the loop has run, not before.

This is clear in the way it is written (in C/C++ at least):

i = 0;do {    i++;} while(i < 5)

The statement(s) to be executed in the loop come before the conditional.

To give an example of how while and do while can differ:

 

i = 2while(i < 2) //will not enter loop{    i++;}//i == 2do {    i++;} while(i < 2) //runs once, then terminates//i == 3
Link to comment
https://linustechtips.com/topic/526411-help-in-loops-in-c/#findComment-6986999
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×