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

Java Do-While Loop - How to End the Program Using Q Option

so i have this simple bank program. it has main menus which are: B - Check for Balance D - Make Deposit W - Make Withdraw Q - Quit their functions are basically their names. and there are two ways to terminate the program: by inputting 'Q' and…
Liyanna
  • 119
  • 10
4
votes
4 answers

How do I print 2 columns with looping from a file in bash

I have the following output.txt it consists only 2 columns to demonstrate: Test1 Test1-IS-OK Test2 Test2-IS-NOT Test3 Test3-IS-OK Test4 Test4-IS-OK Test5 Test5-IS-NOT Then my bash script has the following…
Kalib Zen
  • 655
  • 1
  • 5
  • 16
4
votes
3 answers

Non-function do while loop in C

I'm new to programming and was hoping I could get some help. I'm attempting to create a 'do while' loop in C as part of my CS50 problem sets. I'm attempting to use two different conditions under 'while', but for some reason they are not being…
Disierd
  • 53
  • 4
4
votes
3 answers

Use of Logical Operator in Loop Condition

In the below given code, why the || logical doesn't work, instead the loop terminates specifically when && is used ? int main() { char select {}; do { cout<<"Continue the loop or else quit ? (Y/Q): "; cin>>select; } while…
Rishi K.
  • 111
  • 1
  • 8
4
votes
4 answers

Why does while(int) end when int = 0?

I'm trying to find an input in C++ equivalent to Java's in.hasNextInt and I found this. #include #include int main () { std::vector myvector; int myint; std::cout << "Please enter some integers (enter 0 to…
Hnim Mahp
  • 57
  • 3
4
votes
2 answers

Execution of do while loop in the Java Calculator program

In my Java Program, I have used a Boolean variable 'decision' which should execute the actual calculator code in the 'do loop code block' only when the variable is true, but the do while loop is executed anyways even when the decision variable is…
Yogi Raj
  • 71
  • 1
  • 1
  • 5
4
votes
5 answers

Ending a for loop by pressing 'q'

I have been doing a project for my java class. For the project I have to have the user enter input and calculate their body mass index and body surface area the program is supposed to remain running until the user enters a "q". I cannot get my…
Brad
  • 49
  • 2
  • 7
4
votes
1 answer

Discord.js message after receiving emoji reaction

It it possible to make a Discord bot send a message once a previous message has received a reaction? I was thinking about something like: if(cmd === `${prefix}list`) { var i = 0; let embed = new Discord.RichEmbed() .addField("List",…
Ned
  • 41
  • 1
  • 1
  • 2
4
votes
3 answers

Using scanf for character input, but the do-while loop wont stop at the null character

I'm completely new to programming (1st term in uni) and I can't keep up with my lecturer. At the moment I'm stuck on this exercise (for much more time than I'm willing to admit). I've tried to find help on the internet (in this site and others as…
S. Kats.
  • 43
  • 5
4
votes
5 answers

Which one is better? do{} while(0); or goto xy;

I've two pieces of code: A do while loop: do { errorflag=0; ... if(cond1) { errorFlag=12; break; // Error Conditions } . . // Processing . if(cond2) { errorflag=56; …
Rajeev
  • 1,196
  • 3
  • 14
  • 21
4
votes
5 answers

I don't really understand the do { } while structure

I'm trying to learn Java, I studied Pascal in high school and it has the repeat until..; instruction. I want to solve an exercise where I'm supposed to keep entering numbers until the penultimate + antepenultimate numbers equal the last number I…
4
votes
5 answers

Minimizing the scope of a variable in do ... while loop [Java] - ERROR: Symbol cannot be found

I would like to minimize the scope of arr to a do ... while loop, so it can be destroyed once the loop is exited. If I declare arr within the do while loop, I get the error: symbol cannot be found I can declare it right before the cycle, but then…
sixtytrees
  • 1,156
  • 1
  • 10
  • 25
4
votes
3 answers

Indexing issue with a for loop

I have a for loop where I'm using the slide operator as I'm working with unsigned types. Essentially it boils down to for (std::size_t i = 6; i --> 0;){ cout << i; } But it outputs the numbers from 5 to 0 inclusive and omits 6. Why? Thank you…
4
votes
8 answers

Enumerator stuck in endless loop when removing excess items from a List

I have a script that takes an int[] array, converts it to a list and removes all further occurrences of the integers that already occurred at least 2 times. The problem I have is that when it gets into the loop where I am checking the count of each…
3therk1ll
  • 2,056
  • 4
  • 35
  • 64
4
votes
2 answers

Java Do While Statement with two conditions

I'm trying to learn java but I'm stuck trying to do a single program which concerns Do While Statement with two conditions. Specifically, I want a method to run until the user write "yes" or "no". Well, down there is my thing, what is wrong with it?…