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
5
votes
2 answers

Looking for a more elegant DO WHILE solution

So I have some code centered around a do while loop. string token; var count = 0; var checkDataLength= data.Length == 16; do { count = 0; token = GenerateToken(data, start, end); if (checkDataLength) { …
kurabdurbos
  • 267
  • 1
  • 14
5
votes
1 answer

Kotlin compiler can't figure out that variable is not nullable in do-while loop

I have the following method. Its logic is very simple, if right is set then call left while it has a value (not null). When I write it in the following way, it works. fun goNext(from: Node): Node? { var prev : Node = from var next : Node? =…
barsdeveloper
  • 930
  • 8
  • 28
5
votes
1 answer

Reading key/value parameters from a file into a shell script

I've got my script working almost. The goal of this is to take defined values from a file and then populate these valeus in a shell script at runtime. Please take a look at what i have here... The first file: ab.sh #!/bin/bash USER_TYPE=$1…
Tony Sawah
  • 55
  • 1
  • 4
5
votes
2 answers

do...while() repeating the last string twice

The following code splits the provided string/line down to the characters. Why does the loop repeats that last string twice? How to fix it? #include #include #include #include using namespace std; int…
Bot666
  • 61
  • 6
5
votes
3 answers

Do while loop with a cout statement

So I have a general question about the do/while loop. I'm learning C++ and I know that you can write something like that: do{ .... } while(a<10 && cout<<"message"); The point is, that i know this is possible in c++, but do we really do that? I…
N.Ba
  • 93
  • 1
  • 6
5
votes
2 answers

Do {} while ( , ) with comma operator, is that even possible?

Yesterday I read about the comma operator in Java for for-loops. Which worked as I expected them to. I thought of this construction but it does not work as expected. ';' expected } while((userInput < 1 || userInput > 3), wrongInput =…
Joop
  • 3,706
  • 34
  • 55
5
votes
5 answers

what does "do" do here? (java)

I saw this bit of code on the interents somewhere. I'm wondering what the do is for. public class LoopControl { public static void main(String[] args) { int count = 0; do { if (count % 2 == 0) { for…
David
  • 14,569
  • 34
  • 78
  • 107
5
votes
12 answers

When would a do-while loop be the better than a while-loop?

This is a highly subjective question, so I'll be more specific. Is there any time that a do-while loop would be a better style of coding than a normal while-loop? e.g. int count = 0; do { System.out.println("Welcome to Java"); count++; }…
AlexMTMorgan
  • 67
  • 1
  • 1
  • 10
5
votes
6 answers

Is it possible to have a while loop in c++ that makes the check in the middle of the loop instead of the beginning or end?

I want to have a while loop do something like the following, but is this possible in c++? If so, how does the syntax go? do { //some code while( expression to be evaluated ); // some more code } I would want the loop to be exited as…
adhanlon
  • 6,407
  • 13
  • 43
  • 41
5
votes
4 answers

Navigating console menu

I'm totally new and I don't know how else to ask this or what to even search for. The case is this: I want to navigate through a menu with several sub-menus. In this example I'll just use "options" and a "game" to illustrate what I mean. Say you…
fanatical
  • 103
  • 1
  • 1
  • 6
5
votes
3 answers

C++ do while loop

I have a vector holding 10 items (all of the same class for simplicity call it 'a'). What I want to do is to check that 'A' isn't either a) hiding the walls or b) hiding another 'A'. I have a collisions function that does this. The idea is simply…
KingJohnno
  • 602
  • 2
  • 12
  • 31
5
votes
3 answers

break program when user enters specific string into input

I want to make it so that the user inputs some string, and the program takes console input until user types "/done".. so here's how it would work: print to user: enter your string user enters: hello eclipse. hi test blah blah bla …
sega_one
  • 63
  • 3
  • 9
5
votes
0 answers

Pythonic do-while loop

While python doesn't explicitly allow do-while loops, there are at least 3 reasonable ways to implement them: 1) while True: #loop body if not expr(): break 2) x = True while x: #loop body x = expr() 3) def f(): #loop…
ChrisM
  • 572
  • 7
  • 7
4
votes
7 answers

Looping 10 times adding input, with for and do while loop in C#?

I'm new taking a basic C# course and I'm having trouble getting an assignment to work. I built a basic calculator and it works fine. Now I had to add a new button called "Sum" that will take an input from one of my boxes (number1Txtbox) and add it…
drowningincoffee
  • 93
  • 2
  • 3
  • 11
4
votes
2 answers

Bash Shell Do While Loop Infinite loop?

Basically this is my code: bay=$(prog -some flags) while [ $bay = "Another instance of this program is running, please exit it first" ] do echo "Awaiting Access to program" do ..... I have program which will only allow one instance to be run at one…
bikerben
  • 463
  • 5
  • 11
  • 18