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

Stop a PHP while loop with function for condition

I'm using the following functions to get the final URL from a series of redirects... https://stackoverflow.com/a/4102293/1183476 It works great 99.8% of the time. I can't really pinpoint the exception, but I believe it has something to do with the…
Kirkland
  • 2,319
  • 4
  • 20
  • 27
3
votes
3 answers

PHP while loop not showing first item?

I'm just trying to show a basic list or items from my database but for some reason its not showing the first item so if there is only one thing in a category it doesn't show anything but if there's 2 it will show one item. I've added the code…
huddds
  • 1,045
  • 6
  • 27
  • 47
3
votes
2 answers

How do I count the number of entries in a python code using while loops?

I'm working on a homework assignment for my introductory python programming class and I am stuck on a problem. The instructions are to: Modify the find_sum() function so that it prints the average of the values entered. Unlike the average()…
Lizi Wiki
  • 33
  • 1
  • 1
  • 4
3
votes
1 answer

scanf("%[^\n]",command) inside while loop

I want to have an infinite loop getting command each loop, and this is my code while ( 1 ) { char * command[100]; printf("---| "); scanf( "%[^\n]",command); printf("%s\n",command); } for some reason it only inputs once and the…
JaemyeongEo
  • 183
  • 1
  • 3
  • 14
3
votes
2 answers

bash: read file line by line and sed to append

I have a text file that can have X number of fields, each separated by a comma. In my script I reading line by line, checking how many fields have been populated on that line and determining how many commas i need to append to the end of that line…
ssbsts
  • 844
  • 1
  • 8
  • 13
3
votes
3 answers

Python while loop popleft() - ERROR: Empty deque

I am looking for a more elegant solution to this loop. My deques are created dynamically and can vary in length. In the example below the list is only two items long and could be up to 3 items long. In my application, the lists can be up to 30…
ccdpowell
  • 629
  • 5
  • 14
  • 22
3
votes
4 answers

php echo table with while loop

I am creating a table and want it laid out a certain way and have to retrieve the data from the DB. I am trying to get it set up where it has the usernames across the top example... Jim Chris Allen Rick 7 8 4 …
Ricky
  • 5,201
  • 4
  • 19
  • 22
3
votes
4 answers

Delphi - How to make timer in milliseconds or nanoseconds with start/stop functions?

I am looking for a timer in milliseconds or nanoseconds in Delphi7. I have to check the speeds of three ISAM files with sequential search. The first ind file contains 50 strings like "record_0" to "record_50". The second - "record_0" to "record_500"…
Ezio_
  • 593
  • 3
  • 9
  • 23
3
votes
2 answers

C programming: how to implement an insertion sort?

Say I have a list of numbers: 89 12 18 4 6 and I want to implement an insertion sort and have it print every step of the sort onto the screen: Sort 1. 12 89 18 4 6 Sort 2. 4 12 89 18 6 Sort 3. 4 6 12 89 18 Sort 4. 4 6 12 18 89 here's the code…
Cass Iopeia
  • 31
  • 1
  • 2
3
votes
3 answers

Recursion (or while loops) in Scheme

(define (orderedTriples n) (set! i n) (set! j n) (set! k n) (while (>= i 0) (while (>= j 0) (while (>= k 0) (printf "(~a, ~a, ~a)" i j k) (set! k (- k 1))) (set! j (- j 1))) (set! i (- i 1)))) So my issue is...I am…
ellie0414
  • 83
  • 1
  • 3
  • 8
3
votes
8 answers

What is the difference between while (x = false) and while (!x) in Java?

Sorry, I'm new to Java, so this question might be unclear. I have been recently dealing with enclosing a try and catch statement in a while loop, because I wanted to make sure that getting input was enclosed from the rest of the program. I have come…
akbiggs
  • 34,001
  • 6
  • 25
  • 36
3
votes
5 answers

python loop exception

Hey I am a newbie at python and I need some help. I've written down the following code: try: it = iter(cmLines) line=it.next() while (line): if ("INFERNAL1/a" in line) or ("HMMER3/f" in line) : title = line line = it.next() …
user2002121
  • 75
  • 3
  • 8
3
votes
4 answers

Best Way to generate a list of three unique numbers between 0 and 9

It is all in the title basically. It is simple, but I don't know why my While loop is failing sometimes. Every now and then I would get a list that is of length 2 instead of 3. Here is my C# code: public List generateRequiredSecretCode() { …
J86
  • 14,345
  • 47
  • 130
  • 228
3
votes
4 answers

Whats wrong with this PHP While loop script

Basically I made it to create a SQL query to insert about 5000 random numbers in a database $i (currently 10 just to figure it out). I previously tried to do it solely in MYSQL, but failed and don't want to waste anymore time there. It randomises a…
3
votes
1 answer

do while condition vb6

I have a little vb6 program: Private Sub Form_Load() Dim varTemp As Variant Dim string1 As String Dim x As Integer x = 0 dialog.Filter = "toate fisierele(*.*) | *.*" dialog.Flags = cdlOFNAllowMultiselect Or cdlOFNLongNames…
peter
  • 143
  • 2
  • 4
  • 10
1 2 3
99
100