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
7
votes
8 answers

sum of digits till the sum is one-digit number

I am a beginner java and trying to solve tricky problem input=777 output should be 3 7+7+7=21 , 2+1=3; From the above code if my input is 333 I am getting 9 as answer but when the sum is two digits(777=21) i am getting blank! public static void…
Akhil Maripally
  • 109
  • 2
  • 9
7
votes
5 answers

Do if(){ } while() statement

I am currently working on somebody's else code, with a statement like this if(x.start()) do if(y.foo(x)) { // Do things }while(x.inc()) here x is custom class that holds information on y and allows iteration through its element in a special…
Three Diag
  • 585
  • 2
  • 7
  • 21
7
votes
5 answers

Scope of declarations in the body of a do-while statement

In Why can't you declare a variable inside a do while loop? the OP asks why a declaration in the while-condition of a do-while loop isn't in scope in the do-statement. That would be very unnatural as C/C++ generally follow a…
sfjac
  • 7,119
  • 5
  • 45
  • 69
7
votes
3 answers

Optimize images in directory using multiple CPU cores

I have several PNG images in a directory and I'm using optipng to optimize and reduce image size. The problem is that it takes too long to optimize all files. I have a quad core processor and I noticed that optipng was using only a single core when…
Niresh
  • 611
  • 5
  • 16
7
votes
3 answers

Equivalent using for-loop instead do-while-loop

I was wondering instead of using a do-while loop, what is the equivalent for-loop or any other combination of loops in c?
deathshot
  • 93
  • 1
  • 2
  • 9
6
votes
3 answers

do-while loops with continue and with and without a label in Java

Let's look at the following do-while loop. It's quite obvious and there is no question about it. do { System.out.println("Hello world"); break; } while(false); It's quite obvious and just displays the string Hello world on the console and…
Lion
  • 18,729
  • 22
  • 80
  • 110
6
votes
4 answers

How can I avoid code duplication in C# while and do-while loops?

I have a loop inside a C# method that has the following structure. do { getUserInput(); if (inputIsBad) { doSomethingElse(); } } while (inputIsBad); alternately, with a while loop: getUserInput(); while…
6
votes
3 answers

assigned variable is not working in do while loop in using PHP

I am working on Do While loop in my project its working fine first time. Before while statement, I assigned a value to an array I could able to print the array successfully at bottom of the code, BUT its become 0 when I check at top of the loop.…
Asesha George
  • 2,232
  • 2
  • 32
  • 68
6
votes
4 answers

Random Number in a do-while loop with if statement

I am trying to get make a random number generator that generates a string of numbers between 1 and 9, and if it generates an 8, it should display the 8 last and then stop generating. So far it prints 1 2 3 4 5 6 7 8, but it does not generate a…
hannacreed
  • 639
  • 3
  • 15
  • 34
6
votes
2 answers

How can I break out of my do/while loop?

void GasPump::dispense() { bool cont = true; char stop; do{ cout << "Press any key, or enter to dispense.\n" << "Or press 0 to stop: \n"; cin.get(stop); gasDispensed = gasDispensed +…
user173424
6
votes
1 answer

Java: Try-Catch statement within a Do-While loop

I am trying to execute a bit of code that scans for a user input value. This action is contained in a custom method I wrote, named getTriangleDim(); the method reads in the users int value, makes sure it is within a certain range, and then returns…
user2993456
6
votes
4 answers

do-while and while comparison

do-while: do { i++; ++j; System.out.println( i * j ); } while ((i < 10) && (j*j != 25)); I am learning about do-while vs while at the moment and would like to rewrite the above java fragment (already declared and initialized) using a…
user3003605
  • 385
  • 4
  • 6
  • 13
6
votes
1 answer

Monitor a webpage behind authentication for a text

Here is the deal. I have a requirement to monitor certain password-protected webpage for changes and play sound alarm when size of the page is different from what I know (i.e. it was modified). Here is the chunk of code that i've come up…
Kirill
  • 63
  • 4
6
votes
5 answers

Is if(){do{};while();} exactly like while{}

Is if(a) { do { b(); } while(a); } exactly like while(a) { b(); } ?
user1365010
  • 3,185
  • 8
  • 24
  • 43
5
votes
2 answers

Java, How to close do-while loop using multiple character boolean expressions?

This is quite a beginner question but I'm wondering why my do...while loop is not closing. The program is supposed to loop while the user input is not 'C', 'c', 'F', or 'f'. It seems to close when just one boolean expression in the while section is…
DynoNap
  • 53
  • 4