Questions tagged [while-loop]

A while loop is a control structure used in many programming languages to continuously execute a set of instructions as long as a particular condition is met.

In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

The while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because the while loop checks the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare this with the do-while loop, which tests the condition after the loop has executed.

The syntax for the while loop for many computer languages is as follows:

while (true) {
    //do complicated stuff
    if (someCondition)
        break;
    //more stuff
}

(excerpted from http://en.wikipedia.org/wiki/While_loop, with minor changes)

There are several types of different while loops in modern computing. One of these loops is called a sentinel loop. This loop will run until a "sentinel" value is hit. An example follows in pseudo code (this example is a simple accumulator):

Initialize a data type to not the sentinel value 
while(data!=(sentinel value)){
do something

ask for more data (which will be put into the data variable)
}

See also: , , , and .

26278 questions
3
votes
1 answer

a while loop in oracle

Hi I wrote a while loop in Oracle SQL. I think the syntax is good, but it cannot be excute for some reason. Can someone help me to check where the problems is? Many thanks! It only uses one table "test_fruit", with colume "price". BEGIN WHILE…
user2679074
  • 77
  • 1
  • 1
  • 6
3
votes
3 answers

Converting for loops to while loops in python

I am struggling to find an efficient way to convert these for loops to a working set of while loops. Any Suggestions? I am using 2.7 def printTTriangle(height): for row in range(1,height+1): # print row T's for col in range(1,row+1): …
Greg Jennings
  • 53
  • 1
  • 2
  • 6
3
votes
2 answers

tcl how to read files

I have some problems to read correctly 2 files : filetest1.txt contains : chocolate coconut banana filetest2.txt contains : strawberry orange procedure : proc callme {file1 file2} { set file1 [open $file1 r] set file2 [open $file2 r] …
user2669068
  • 165
  • 1
  • 2
  • 11
3
votes
4 answers

C++ changing variable inside while loop

I'm new to C++, and trying to go through [Project Euler][1]. I got all the way to [Problem 4][2] (impressive I know) and am having trouble with what I think is the scope of my variables inside a while loop. If you don't know, the problem asks you to…
Daniel Forsyth
  • 313
  • 2
  • 4
  • 15
3
votes
2 answers

Breaking the endless loop from watch or immediate window

I wrote app that uses while loop. And in my code I forgot to put break in a specific condition. I am running the application from the debugger. Now I need to quit from the loop. Some people could say just stop it and put that break line in, but I…
Dilshod
  • 3,189
  • 3
  • 36
  • 67
3
votes
1 answer

understanding recursion vs loops ruby

I have the following recursion method. I get an error stack overflow. it stops at -9352. My questions, is stack overflow the same as an infinite loop? Because this will keep calling itself. But if I do a infinite loop with while, until, do,…
hken27
  • 413
  • 1
  • 5
  • 12
3
votes
3 answers

While won't start loop after y/n input C++

Problem: while doesn't work to start loop over when user inputs y. I have spent hours searching all forums including Stack Overflow for a clue and tried to figure this out (learning C++ on my own). Based on what I have read I have tried: a do loop…
3
votes
2 answers

return type after while in a try

Almost for the first time I'm trying to write imperative code in ocaml to try to answer a question on this website, but I'm facing a little problem. let f() = try while true do () done with _ -> 2 He doesn't like this, because he thinks…
bruce_ricard
  • 743
  • 1
  • 7
  • 15
3
votes
1 answer

Variables change values inside while cycle

I have started programming with python yesterday, so I'm quite a newbie! I have this function, which must check if an inserted value is a number if the number is no greater than 31 (see code below) During debugging, I have found out this bug I…
user2669155
  • 87
  • 2
  • 9
3
votes
2 answers

C++ How to exit out of a while loop recvfrom()

I'm trying to create a UDP broadcast program to check for local game servers, but I'm having some trouble with the receiving end. Since the amount of servers alive is unknown at all times, you must have a loop that only exits when you stop it. So in…
Daaksin
  • 834
  • 3
  • 13
  • 28
3
votes
1 answer

Using while loop for sqldatareader

I trying to get both policeid and fullname from my table named PoliceAccount when the handle column equal to the value of the dropdownlist and then put the value into a label and display it. By using the code provided below I keep getting the result…
XiAnG
  • 245
  • 2
  • 9
  • 25
3
votes
6 answers

Use a for-loop or a for-each loop?

Should we prefer the for-each loop instead of the traditional for-loops? Is the while-loop advantageous? List names = Arrays.asList("John", "Jeff", "Mary", "Elise"); //for-each loop for(String name: names){ log(name); } //traditional…
Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
3
votes
8 answers

Using while loop in java

I'm trying to read in and add up (only) positive integers until a negative integer is entered. The program is supposed to stop only when a negative integer is entered. The best I've been able to come up with is the code below but the problem is it…
Neo
  • 117
  • 2
  • 3
  • 10
3
votes
2 answers

PHP: display entries from Database in groups of five?

Is it possible and if so, how can I do it, to select all entries in a table in my database and then display five results at the time in one group. Meaning: A example is that I've 15 records total in my database, then I want to present my data like…
Emil Devantie Brockdorff
  • 4,724
  • 12
  • 59
  • 76
3
votes
2 answers

Sum of odd numbers in a range

What is the sum of the odd numbers from 1523 through 10503, inclusive? Hint: write a while loop to accumulate the sum and print it. Then copy and paste that sum. For maximum learning, do it with a for loop as well, using range. What I tried. I need…
Evan Dissanayake
  • 33
  • 1
  • 2
  • 6