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

while loop inside another while loop only runs one time

Hi I'm fairly new to coding and brand new to the stackoverflow community, so bear with me. I'm trying to make a code that will create the following output: a0 b0 b1 b2 a1 b0 b1 b2 a2 b0 b1 b2 with this code:
Rene Jorgensen
  • 169
  • 1
  • 8
3
votes
1 answer

how to use openmp with a break in a while-loop

I want to use openmp to accelerate the code like below. The code is just to explain the operations, not real. Iterator iterator(records); while(iterator.next()) { int current_id = iterator.current_row_index(); bool result =…
3
votes
2 answers

Grouping records from while loop | PHP

I'm trying to group down records by their priority levels, e.g. --- Priority: High --- Records... --- Priority: Medium --- Records... --- Priority: Low --- Records... Something like that, how do I do that in PHP? The while loop orders records by…
MacMac
  • 34,294
  • 55
  • 151
  • 222
3
votes
3 answers

Bash While loop Not Ending

I was playing with bash programming. I have written a simple bash program which takes input from reader. If reader typed the string "bye" the while loop ends. So the program is pretty simple I have written something like…
mirmdasif
  • 6,014
  • 2
  • 22
  • 28
3
votes
1 answer

Mutiple conditions in OS X bash script WHILE loop

Basically what i want to do is read in a variable from std input and check if the variable is 1 or 2. if it is not i want to inform the user and ask again, i thought a while loop was made sense (i am using OS X Mavericks in a bash shell) For a…
Tyler Durden
  • 575
  • 7
  • 23
3
votes
1 answer

while loop debugging in netlogo

I am having trouble understanding an error emerging from two consecutive while loops in a net logo project. ;;;;;;;;;;;;;;;;;;;;;;;; ;;; Global variables ;;; ;;;;;;;;;;;;;;;;;;;;;;;; globals [it] ;;;;;;;;;;;;;;;;;;;;;; ;;; Breedin…
3
votes
2 answers

Python : While True or False

I am not an experienced programmer, I have a problem with my code, I think it's a logical mistake of mine but I couldn't find an answer at http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/whilestatements.html . What I want is to check if the…
Tolga Varol
  • 1,036
  • 1
  • 16
  • 21
3
votes
4 answers

Check for existing file with a loop

I'd like to check for an existing file using a while loop. Now the problem is, that if use something like this: while (file.exists()) { text.setText("Some text appears"); text.setTextColor(Color.RED); } my program always seem to…
user3801167
  • 177
  • 1
  • 2
  • 7
3
votes
2 answers

Does a while(true) Infinite Loop Peg the CPU?

I have a situation where I need a constant loop running in a service, each time checking certain conditions, and then taking action as appropriate. From a design perspective, using a while (true) loop fits perfectly: while (true) { …
Phil Sandler
  • 27,544
  • 21
  • 86
  • 147
3
votes
1 answer

using var keyword in while loop definition

In JavaScript for loop I can use var keyword in loop definition something like that: for (var i=0; i<10; i++) ... I know scope of the variable i is not inside the loop but inside function where loop is declared. This it a better notation than…
Boris Šuška
  • 1,796
  • 20
  • 32
3
votes
3 answers

while loop chars C++

I'm working on a Programming assignment and I can't figure out why this while loop is looping infinitely. There has been other input before this loop occurs, but I have cin.ignore() to clear any junk out before this loop. I'm scratching my brain,…
beatlesfan42
  • 39
  • 1
  • 1
  • 4
3
votes
3 answers

while loop is not working with max(eno) query

I used the query: $sql = mysql_query("SELECT MAX(eno) FROM employee"); Which retrieve last record from database, but it is not working with while loop. If I used: $sql = mysql_query("SELECT * FROM employee"); This query then the while loop runs…
LOKESH
  • 1,303
  • 1
  • 16
  • 29
3
votes
2 answers

Morse Code Translator - Can't seem to convert from morse-to-english accurately

So I have an assignment due and I have spent ages trying to figure out a specific part of the problem, but keep coming up with nothing. I finally 'gave up' and decided to come here for a little assistance. The question reads as follows: "Write a…
user3879787
3
votes
5 answers

PHP Sum a value in while loop, but with conditions

I have two tables to be joined, 1 is user and 1 is attendance. TABLE : attendance id userId totalHours 1 1 0745 2 3 0845 3 1 0945 TABLE : user id name departmentId 1 John 2 2 Sean 2 3 Allan 2 Not…