Questions tagged [do-while]

A do while loop, sometimes just called a do loop, is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.

The do-while loop can be found in most computer languages and can be thought of as a repeating if statement.

The syntax for the while loop for many computer languages is as follows:

do
{
    // loop body
} while (condition);

The do while construct consists of a block of code and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false.

Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed.

(excerpted from http://en.wikipedia.org/wiki/Do_while_loop )

2862 questions
10
votes
6 answers

The do-while statement

Possible Duplicate: When is a do-while appropriate? Would someone mind telling me what the difference between these two statements are and when one should be used over the other? var counterOne = -1; do { counterOne++; …
Shaz
  • 15,637
  • 3
  • 41
  • 59
9
votes
6 answers

Do While Loops Versus For Loops in Java for Counting

When it comes to counting, should a do-while loop be used, or a for loop? Because this: class Main { public static void main(String[] args) { int times = 1; do { System.out.println("I have printed " + times + " times."); …
Domani Tomlindo
  • 239
  • 1
  • 5
  • 12
9
votes
2 answers

Does Linux Bash have a do-while loop?

After some search on the Internet, it appears that Bash doesn't have a do-while loop. Is this correct? Is there a reliable source to confirm this (the lack of evidence that there is a do-while loop is not an argument that there isn't one, perhaps a…
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
9
votes
6 answers

Java do while, while

what behaviour can I expect when I run this code: do while(testA) { // do stuff } while(testB); Will it behave like: do { while(testA) { // do stuff } } while(testB); Or: if(testA) { do { // do stuff }…
Pindatjuh
  • 10,550
  • 1
  • 41
  • 68
9
votes
4 answers

What are some better ways to avoid the do-while(false); hack in Java?

When the code flow is like this: if(check()) { ... ... if(check()) { ... ... if(check()) { ... ... } } } I have generally seen this work around to avoid this messy code flow: do { if(!check()) break; …
Kenyakorn Ketsombut
  • 2,072
  • 2
  • 26
  • 43
8
votes
2 answers

Why the huge time difference between while and do..while in JavaScript

The while loop Test the condition and if it is true, then execute the code The do..while loop Execute first time. Then test and execute. So the difference between while and do..while is , programmatically In while, one test is carried out more than…
Sagar V
  • 12,158
  • 7
  • 41
  • 68
8
votes
4 answers

Why does the do-while loop not run as expected in this Java program?

I've been writing a program that will take an array of the names of music files and play them. I succeeded in doing that, but, I wanted to touch up on some things and make it a little nicer. I am trying to make the music play in a random order but…
Cal Dilworth
  • 133
  • 2
  • 8
8
votes
5 answers

A very simple java do...while loop

I am learning JAVA and typed the following DO...WHILE example. The program will quit if I type 'q'. It runs but why I get three rows of "Please a key followed by ENTER:"? class DWDemo { public static void main (String args[]) throws…
JACKY88
  • 3,391
  • 5
  • 32
  • 48
8
votes
9 answers

Cannot use variable in while clause that is declared inside the do/while loop

Why am I not allowed to use a variable I declared inside do and use it inside the while part of a do/while loop? do { index += index; int composite = index + 1; // more code here } while (UnsetBitmask(bitmasks, composite)); // cannot…
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
8
votes
8 answers

How can I write C's do-while(0) in Python?

In C there's a clever trick that lets you avoid pyramid-style code by turning: if (check1()) if (check2()) if (check3()) do_something(); into: do { if (!check1()) break; if (!check2()) break; if (!check3()) break; …
user2058002
8
votes
3 answers

In what situations can do-while be more efficient than while?

While vs. do-while While and do-while are functionally equivalent when the blocks are empty, although while seems more natural: do {} while (keepLooping()); while (keepLooping()) {} One typical use case of a while/do-while with an empty block is to…
assylias
  • 321,522
  • 82
  • 660
  • 783
7
votes
3 answers

do-while is the fastest loop in php?

I have profiled for, while and do-while loops with something simple: while ($var < 1000000) { ++$var; } do { ++$var; } while ($var < 1000000); for ($var = 0; $var < 1000000; ++$var) { //do nothing } by comparing microtime() before and after…
Gambo
  • 238
  • 1
  • 2
  • 9
7
votes
3 answers

how does do{} while(0) work in macro?

Though this topic has been discussed many times in this forum and all other forums, still I have doubts. Please help. How does the do{} while(0) in macro work in Linux kernel? For example, #define preempt_disable() do { } while (0) How does it…
iSegFault
  • 947
  • 2
  • 9
  • 18
7
votes
2 answers

Haskell - Do while loop

I'm new to Haskell and would be glad if someone would be willing to help me! I'm trying to get this program to work with a do while loop. The result from the second getLine command gets put into the varible goGlenn and if goGlenn doesn't equal…
Haskellnoob
  • 71
  • 1
  • 1
  • 2
7
votes
3 answers

Nodejs - Re-Calling function on error callback - Is there a non blocking way?

I got a function which makes a request to an API. Sometimes the API got some hiccups and isnt available for a second or two every now and then, resulting in an error on which I'd like to call the function again. Since there are another 70~80 lines…
Rockbob
  • 135
  • 1
  • 2
  • 11