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

While Loop Print List formatting

I wanted to create a list of factors from any given number only using the formula below. I am not allowed to use list therefore, I have imitate using strings as follows: for example and lets say we choose num=12: def factors(num): i=1 while…
SteveZrg
  • 51
  • 1
  • 7
3
votes
4 answers

conditional while loop in php?

I need to do a PHP while loop, but only if a variable is true. And I can't really put the while loop in an "if" statement, which seems like the obvious thing to do, since the code block is huge and it would be ugly and confusing. Do I need to break…
julio
  • 6,630
  • 15
  • 60
  • 82
3
votes
3 answers

Django while loop

I wonder if there's any way to do a while loop in django (I think that's what I'm after)? What I'm trying to do is a nestled ul/li list. The list is generated by a for loop in a for loop. But since some elements in the second for loop has more…
fredrik
  • 17,537
  • 9
  • 51
  • 71
3
votes
1 answer

Ruby - negative condition in while statement

RubyMine warns me about negative condition here: while !open_socket do sleep 1 end I know that unless is an opposite to if, but what is the opposite to while ?
Paul
  • 25,812
  • 38
  • 124
  • 247
3
votes
1 answer

ACF - Looping thru posts, then thru flexible content, then thru repeater

What I am trying to do is get images from a repeater field and fill my homepage with images that lead to posts. This is usually easy for me but the catch here is that there are multiple images for each post in a repeater field, which are in a…
ch4rlie
  • 102
  • 2
  • 9
3
votes
3 answers

Stop a infinite while loop pressing a key in Matlab

I have a while loop, infinite, and I want to stop it when I press a keyboard key. Pseudocode: While(1) do stuff; listening for key; if key is pressed break; end end The function waitforbuttonpress makes me press the key, so…
SamuelNLP
  • 4,038
  • 9
  • 59
  • 102
3
votes
2 answers

Simulating in R- how can I make this faster?

I am simulating something like Jim Berger's applet. The simulation works like this: I will generate a sample x of size n either from the null distribution N(0,1) or from the alternative distribution N(theta, 1). I will assume that the the prior…
Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66
3
votes
3 answers

Categorising a large mysql result with php while?

I'm using the following to grab my large result set from a mysql db: $discresult = 'SELECT t.id, t.subject, t.topicimage, t.topictype, c.user_id, c.disc_id FROM topics AS t LEFT JOIN collections AS c ON t.id=c.disc_id WHERE…
Ryan
  • 31
  • 1
3
votes
2 answers

While Loop not Breaking when Condition is Met

I've got several working while loops in my function but this one I just can't get to work as it's supposed to. I assumed that while($variable == false) would break out of the loop as soon as $variable = true; occurs. Here's a bare bones version,…
Works for a Living
  • 1,262
  • 2
  • 19
  • 44
3
votes
3 answers

Control the speed of a loop

I'm currently reading physics in the university, and im learning python as a little hobby. To practise both at the same time, i figured I'll write a little "physics engine" that calculates the movement of an object based on x,y and z coordinates. Im…
Jørgen
  • 3,467
  • 6
  • 33
  • 49
3
votes
3 answers

While loop won't stop in python

We are trying to make a python program to simulate games of craps and display the probability of winning. I have narrowed down the answer to the while loop and as the title suggests, when it hits the loop it won't exit. As far as I can tell the loop…
user3288723
  • 33
  • 1
  • 3
3
votes
1 answer

Python multiple comparison for string

shift = raw_input("Enter the shift the employee works in: ") shift = shift.upper() print shift while (shift != "A" or shift != "B" or shift != "C" ): shift = raw_input("Invalid Input- Please Enter a, b or c: ") …
user3347937
3
votes
1 answer

While read loop only processes the first line;

I have a file with multiple lines and want to do the following with each line.. Set three variables from an awk output. Do something with the variable data (in my example, echo them). My script successfully does what I want it to, however only for…
user3543468
  • 33
  • 1
  • 6
3
votes
6 answers

While loop with try catch fails at bad cin input

I can't seem to figure out why this falls into a loop after getting non-int input. I've tried cin.flush(), which doesn't seem to exist, cin.clear(), which seems like it should work, even cin.sync() after reading someone else post about it working,…
na.
  • 33
  • 1
  • 1
  • 3
3
votes
2 answers

Div value increment javascript

I am trying to make a div with an animated value increasing up to it's final value. Here's an example from themeforest: http://demo.themesvision.com/drupal/evolve/ (Happy customer, projects completed, etc) I've tried lots of different code lines…
Mind_meddler
  • 121
  • 1
  • 2
  • 10