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
23
votes
4 answers

Are there cases where while/do-while must be used instead of for?

This is a long running debate between me and my teacher. Can there be a situation where a for loop absolutely cannot be used in place of a while/do-while loop? In other words, is there a specific case where a for-loop will not work in place of a…
REGAL
  • 326
  • 2
  • 9
22
votes
9 answers

Other ways to deal with "loop initialization" in C#

To start with I'll say that I agree that goto statements are largely made irrelevant by higher level constructs in modern programming languages and shouldn't be used when a suitable substitute is available. I was re-reading an original edition of…
Peter Oehlert
  • 16,368
  • 6
  • 44
  • 48
22
votes
1 answer

How to set up a WHILE loop with IF statement in MySQL?

I would like to create a stored routine for MySQL that figures out the number of business or working days for a month (Working Days are Monday thru Friday). It's a syntax error however I don't know what the syntax error is. All it tells me…
scrfix
  • 1,188
  • 3
  • 11
  • 24
21
votes
3 answers

Whats the difference between do while and while in VB.NET?

What's the difference between Do While where the statement is the first line in the loop block and just the single While in VB.NET? They don't seem to offer any difference in behavior.
jaffa
  • 26,770
  • 50
  • 178
  • 289
19
votes
7 answers

Using continue in a do-while loop

MDN states: When you use continue without a label, it terminates the current iteration of the innermost enclosing while, do-while or for statement and continues execution of the loop with the next iteration. I'm not sure why the following piece of…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
18
votes
8 answers

Which is faster in Java, while or using recursive method?

I have the following sample class with these two methods: Process.java: public class Process { public Process() { } public static void countRecursive(int num) { System.out.println("countRecursive: " + num++); if (num <=…
Oscar Jara
  • 14,129
  • 10
  • 62
  • 94
16
votes
5 answers

Use variables declared inside do-while loop in the condition

I was writing something like this code: do { int i = 0; int j = i * 2; cout<
José D.
  • 4,175
  • 7
  • 28
  • 47
16
votes
4 answers

Is there a way to perform a do-while?

I'm planning to use a do-while loop in MATLAB. Is there a way to do that?
Simplicity
  • 47,404
  • 98
  • 256
  • 385
15
votes
3 answers

How do I iterate over all bytes in an inputStream using Groovy, given that it lacks a do-while statement?

Given that Groovy does not have a do-while statement, how can I iterate over all bytes in an input stream? Per a previous version of the Groovy user guide: No 'do ... while()' syntax as yet. Due to ambiguity, we've not yet added support for do ..…
talnicolas
  • 13,885
  • 7
  • 36
  • 56
15
votes
4 answers

If statements in a do while loop with a yes or no ending

I am new to coding and I'm trying to do a long do while loop with nested if statements but I'm having issues with getting my loop to actually loop. Instead of getting help directly on my project, which has a very long code, I made a simple kind of…
JKisa
  • 159
  • 1
  • 6
13
votes
9 answers

Why use a "do while" loop?

I've never understood why using a do while loops is necessary. I understand what they do, Which is to execute the code that the while loop contains without checking if the condition is true first. But isn't the below code: do{ …
Stanni
  • 717
  • 4
  • 10
  • 24
13
votes
1 answer

Java loop efficiency

I'm comparing the efficiency of nested for, while and do-while loops in Java, and I've come across some strange results that I need help understanding. public class Loops { public static void main(String[] args) { int L = 100000; //…
JohnF
  • 339
  • 4
  • 11
13
votes
4 answers

Why do { } while(condition); needs semicolon at the end of it but while(condition) {} doesn't?

I always have problem with placing ; at the end of while or not placing it at the end of do while loops. So what's the reason? Why int numItemsToProcess = 3; while(numItemsToProcess > 0) { // process an item numItemsToProcess--; }…
s4eed
  • 7,173
  • 9
  • 67
  • 104
11
votes
2 answers

syntax error near unexpected token `<'

StudentAnwser=() inputScriptFile=001.sh while IFS= read -r line; do StudentAnwser+=( "$line" ) done < <( sh $inputScriptFile test.txt ) it returns a error foo.sh: line 22: syntax error near unexpected token `<' foo.sh: line 22: ` done < <( sh…
Billy Chan
  • 141
  • 1
  • 1
  • 9
11
votes
8 answers

Simple do while loop using while(true);

Many times in the examples of C programs, I came across these kind of loops. What do these kind of loops really do? do { while (...) // Check some condition if it is true. { calculation 1 } // Some new condition is…
Dev
  • 153
  • 1
  • 1
  • 6