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

Why does the message print twice?

I am creating a simple Tic Tac Toe for C, and here is a particular function which I am having a problem with. This is supposed to let the user select 'X' or 'O', and for the most art it works. However, if I enter a wrong symbol, it prints the…
C_Intermediate_Learner
  • 1,800
  • 3
  • 18
  • 22
3
votes
4 answers

How to Write this While Loop as a For Loop?

I'm going through "C++ Primer 5th Edition" as recommended via this site. I'm stepping through For and While loops and have come to the conclusion that they're virtually the same. At least that's what I've gathered. I think someone explained it the…
Bit Deception
  • 297
  • 1
  • 3
  • 7
3
votes
1 answer

Monitor directory and change ownership of files upon creation

I have tried this method but it does not work... inotifywait -mr -e create /opt/freeswitch/storage/ 2>&-| sed 's/ CREATE //g' | while read file; do chown apache.apache $file done From command line inotifywait -mr -e create…
Arringar1
  • 395
  • 4
  • 9
  • 21
3
votes
2 answers

Perl, A hash of arrays: adding and removing keys, adding to an array, all in a while loop

I have a hash which should contain certain keys which are linked to their own arrays. To be more specific, the hash keys are quality values and the arrays are sequence names. If there already is an array for that quality, I'd want to add the…
user2500878
3
votes
5 answers

Is there any point where While loop is better than For loop?

I've read an article in the Effective Java about preferring For loops to While loops. For loops have a lot of advantages over While loops. But is there any disadvantages in preferring For to While?
Chechulin
  • 2,426
  • 7
  • 28
  • 35
3
votes
2 answers

bash: reading text from a string one character at a time

i have a script to read a file one character at the time this is the script i use INPUT=/path/ while IFS= read -r -n1 char; do echo $char done < "$INPUT" it works fine only i cant find out how to store every character into a variable im looking…
vista_narvas
  • 31
  • 1
  • 2
3
votes
2 answers

While statement not working C

I'm learning C, and I'm trying to make a basic calculator, but having some trouble with the while statements. I've tried doing it multiple ways but it never repeats itself, just finishes the script. Any ideas? // // main.c // Calculator // // …
Austen
  • 283
  • 1
  • 4
  • 16
3
votes
3 answers

Java: not exiting while loop?

import java.util.Scanner; public class OrigClass { public static void main (String[] args){ Scanner ScanObj = new Scanner(System.in); System.out.println("Set Password: "); String SetPass = ScanObj.nextLine(); System.out.println("Guess Password:…
Sam
  • 97
  • 2
  • 4
  • 11
3
votes
1 answer

jquery toggle one div in php while loop

I have this While loop in my php code where I echo rows inside one div that acts like a button and when I press that "button" I want to hide/show the connected div with the jQuery function "toggle". I only get it to work so that when I click any of…
3
votes
3 answers

Java, need a while loop to reach eof. i.e.while !eof, keep parsing

I currently have a working parser. It parses a file once(not what I want it to do) and then outputs parsed data into a file. I need it to keep parsing and appending to the same output file until the end of the input file. Looks something like…
john stamos
  • 1,054
  • 5
  • 17
  • 36
3
votes
1 answer

Error with query and while loop

Been trying to figure this out for a couple of days now. Havn't come up with any solution and I've searched and asked friends about it. But with no good results. When I user $db->query(); It gives me this error: Fatal error: Call to a member…
user2328659
  • 71
  • 1
  • 6
3
votes
3 answers

Flow control after breaking out of while loop in Python

I am pretty new to both programming and Python. A few times now, I have created what feels like an awkward program flow, and I am wondering if I am following best practices. This is conceptually what I have wanted to do: def pseudocode(): while…
Ben S.
  • 3,415
  • 7
  • 22
  • 43
3
votes
3 answers

Simulate game of craps

I'm trying to implement a function craps() that takes no argument, simulates one game of craps, and returns 1 if the player won and 0 if the player lost. Rules of the game: the game starts with the player throwing a pair of dice. If the player …
Snarre
  • 597
  • 5
  • 14
  • 22
3
votes
2 answers

While loop python

I am new to python and am having problems seeing why this code does not work. I would like it to return [10,122,2]. close = [5000,5010,5132,5134] def difference(): x = 0 data = [] while x < len(close): diff =…
Reno
  • 1,039
  • 3
  • 14
  • 22
3
votes
3 answers

how do i add auto numbering with each fetching row?

how do I add auto numbering with each fetching row? I want auto to add a new number as a serial number with each fetching row from the mysql database?
Edge Matrix
  • 101
  • 1
  • 3
  • 8