Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16943

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
While. . .do while structure Posted by Ijaj on 15 May 2010 at 6:35 PM
Hi,
I'm Ijaj Chowdhury from IUT CEE department.I wanna know when & where "While ....do while" is to be used.Can anyone clear my concept about this?Thank you!!!
Report
Re: While. . .do while structure Posted by jcpascal27 on 15 May 2010 at 10:39 PM
The difference between While and Do..While, is that "While" loops check the statements before executing the code inside them, and "Do..While" loops execute the code inside them before checking the statements.

for example:

#include <stdio.h>

int main(void) {
 int a,b;

 a = 0; b = 4;

 while (b < a) {
  printf("While 1\n");
 }

 do {
  printf("While 2\n");
 } while (b < a);

 return 0;
}


the first printf never gets displayed because the comparison between a and b is made before executing the instructions inside the loop, and of course 4 < 0 is false.
The second printf does get displayed because the comparison is made AFTER the code inside the loop is executed once.

so, when you use "Do..While" loops the code inside them is executed AT LEAST once. That's not necessarily the case with "While" loops.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.