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
1077
votes
21 answers

How to emulate a do-while loop?

I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work: list_of_ints = [ 1, 2, 3 ] iterator = list_of_ints.__iter__() element = None while True: if element: print element …
grigoryvp
  • 40,413
  • 64
  • 174
  • 277
235
votes
27 answers

What are some better ways to avoid the do-while(0); hack in C++?

When the code flow is like this: if(check()) { ... ... if(check()) { ... ... if(check()) { ... ... } } } I have generally seen this work around to avoid the above messy code flow: do { if(!check())…
Sankalp
  • 2,796
  • 3
  • 30
  • 43
228
votes
21 answers

Are "while(true)" loops so bad?

I've been programming in Java for several years now, but I just recently returned to school to get a formal degree. I was quite surprised to learn that, on my last assignment, I lost points for using a loop like the one below. do{ //get some…
JHarnach
  • 3,944
  • 7
  • 43
  • 48
219
votes
5 answers

Emulating a do-while loop in Bash

What is the best way to emulate a do-while loop in Bash? I could check for the condition before entering the while loop, and then continue re-checking the condition in the loop, but that's duplicated code. Is there a cleaner way? Pseudo code of my…
Alex
  • 10,470
  • 8
  • 40
  • 62
137
votes
5 answers

Do while loop in SQL Server 2008

Is there any method for implement do while loop in SQL server 2008?
Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138
116
votes
31 answers

'do...while' vs. 'while'

Possible Duplicates: While vs. Do While When should I use do-while instead of while loops? I've been programming for a while now (2 years work + 4.5 years degree + 1 year pre-college), and I've never used a do-while loop short of being forced to…
mphair
  • 1,450
  • 2
  • 9
  • 15
75
votes
4 answers

do-while loop in R

I was wondering about how to write do-while-style loop? I found this post: you can use repeat{} and check conditions whereever using if() and exit the loop with the "break" control word. I am not sure what it exactly means. Can someone please…
Tim
  • 1
  • 141
  • 372
  • 590
70
votes
5 answers

Do "nothing" while "condition"

While browsing the code for the Java 8 version of ForkJoinPool(which has a few interesting changes from Java 7) I ran across this construct (here): do {} while (!blocker.isReleasable() && !blocker.block()); I'm struggling with why you…
Erik Vesteraas
  • 4,675
  • 2
  • 24
  • 37
67
votes
7 answers

Elegant way for do ... while in groovy

How to do code something like this in groovy? do { x.doIt() } while (!x.isFinished()) Because there is no do ... while syntax in groovy. No 'do ... while()' syntax as yet. Due to ambiguity, we've not yet added support for do .. while to…
MariuszS
  • 30,646
  • 12
  • 114
  • 155
50
votes
2 answers

Why there is no do while loop in python

Why doesn't Python have a 'do while' loop like many other programming language, such as C? Example : In the C we have do while loop as below : do { statement(s); } while( condition );
Bahubali Patil
  • 1,127
  • 1
  • 16
  • 23
39
votes
2 answers

Why is while's condition outside the do while scope

More often than not we need loops like this do { Type value(GetCurrentValue()); Process(value); }while(condition(value)); Unfortunately this will not compile, because value's scope ends at }. Which means that I will have to declare it…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
33
votes
6 answers

Why can't you declare a variable inside the expression portion of a do while loop?

The following syntax is valid: while (int i = get_data()) { } But the following is not: do { } while (int i = get_data()); We can see why via the draft standard N4140 section 6.4: 1 [...] condition: expression attribute-specifier-seqopt…
user4351360
  • 461
  • 4
  • 6
32
votes
7 answers

does continue work in a do while?

I have a do while that looks like: User user = userDao.Get(1); do { // processing // get the next user // user = UserDao.GetNext(user.Id); if(user == null) continue; // will this work????????????? } while ( user != null) If it…
mrblah
  • 99,669
  • 140
  • 310
  • 420
28
votes
9 answers

Do .. While loop in C#?

How do I write a Do .. While loop in C#? (Edit: I am a VB.NET programmer trying to make the move to C#, so I do have experience with .NET / VB syntax. Thanks!)
Srikrishna Sallam
  • 724
  • 2
  • 8
  • 11
28
votes
31 answers

Test loops at the top or bottom? (while vs. do while)

When I was taking CS in college (mid 80's), one of the ideas that was constantly repeated was to always write loops which test at the top (while...) rather than at the bottom (do ... while) of the loop. These notions were often backed up with…
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
1
2 3
99 100