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

Nested While loop in Python

I am a beginner in python programming. I wrote the following program but it doesn't execute as I want it to. Here is the code: b=0 x=0 while b<=10: print 'here is the outer loop\n',b, while x<=15: k=p[x] print'here is the…
Gillani
  • 119
  • 1
  • 1
  • 5
3
votes
1 answer

Python: While loop will not terminate

I have read a lot of topics regarding while loops and I can't find one that tells me what I have done wrong with my own code. I am doing the Learn Python the Hard Way and I wrote this code in order to satisfy the study drill #1 for exercise 33. I…
zkirkland
  • 12,175
  • 3
  • 16
  • 18
3
votes
2 answers

Can I use while to do nothing in php and if possible is it a good practice or not?

I was thinking of doing something like this in PHP function doSomething() { /* ... */ } while(my_array[] = doSomething()) { } instead of using extra variable : while(myValue = doSomething()) { myArray[] = myValue; } Is using while block to do…
Boris D. Teoharov
  • 2,319
  • 4
  • 30
  • 49
3
votes
1 answer

sqrt function weird behaviour in Python

In Python, I wrote a custom code sqrt(x, delta) to calculate the square root of a given number with a delta-close approximation. It uses a while loop and a binary-search-like algorithm. The code: from __future__ import division def sqrt(x,…
user1563285
3
votes
1 answer

Java - Output integers up to user input integer in a single line

I have an assignment to create a program that prompts user input of an integer, then creates a multiplication table based on the user input using while loops. The output multiplication table must include header row and column. What I'm stuck on is…
ajanic0le
  • 383
  • 2
  • 4
  • 8
3
votes
1 answer

My while loop is repeating over and over again

it is referencing a board to a game with 9 slots and once the slots get filled up the ##while loop keeps searching for a new spot when there aren't any empty spots and I don't know how to fix it, please help! :( computer = random.randint(0,…
user1861771
  • 33
  • 1
  • 1
  • 5
3
votes
5 answers

No condition in PHP while loop?

So, I'm building my first super basic CMS using PHP. I don't want to simply copy the code from the tutorial I'm watching, but really understand it. One thing that bothers me is the use of the while loop for fetching posts. In the code below, I can't…
user1781186
3
votes
2 answers

Accepting user input in a while loop

I'm trying to create a simple temperature conversion program that allows the user to convert temperatures as many times as they want until they decide to end the program by entering the letter 'e'. Everything else in the code works except the part…
Tydis
  • 65
  • 1
  • 2
  • 9
3
votes
2 answers

Puzzling recursion with php

I use this code with php: while (0 != $date1 || $this->counter < 5 ) { // if ($this->true_cahce_failure ) { $this->url= $this->adjust_url_with_www_add($this->url); // $this->counter=2; // } $this->cache_debug("Date".$date1."…
Dmitry Makovetskiyd
  • 6,942
  • 32
  • 100
  • 160
3
votes
3 answers

Why while loop is sticking at raw_input? (python)

In the following code i am trying to make a "more" command (unix) using python script by reading the file into a list and printing 10 lines at a time and then asking user do you want to print next 10 lines (Print More..). Problem is that raw_input…
Sourabh Saxena
  • 127
  • 1
  • 1
  • 7
3
votes
2 answers

While loop that prints a random number between 1 and 10 and stops when the number is N

This is what I've got, but this only generates a random number once and prints that number infinitely: import random x = random.randint(0,10) y = 7 while x != y: print(x)
Nikolai Stiksrud
  • 157
  • 2
  • 4
  • 7
3
votes
5 answers

easier way to write loops

Hello wrote a program that outprints this; 0 01 012 0123 01234 012345 01234 0123 012 01 0 Im pretty sure my method is long and stupid, heres how it looks like #include int main(void) { int first = -1, second = -1, third = -1, fourth =…
Winkz
  • 329
  • 2
  • 5
  • 19
3
votes
2 answers

Newbie stuck with easy "while" repetition in Python

The point of the program is to ask the user's name (automatically capitalizing the first letter). It will then ask for age and gender. If age is over 130 or negative, it will throw an error The program is supposed to print out all the information,…
3
votes
3 answers

Getting a syntax error on a while loop

I keep getting a syntax error on the while loop, and I'm not understanding why. def main(): n=1 i=1 flag=True num1=eval(input("Enter number") while i<9: n=n+1 num2=eval(input("Enter number", n)) r=r+1 …
user1726993
  • 43
  • 1
  • 1
  • 6
3
votes
4 answers

Try-Catch statement ends While loop reading through an XML file in C#

I have a while loop going through an XML file, and for one of the nodes "url", there are sometimes invalid values within it. I put a try-catch statement around this to catch any invalid values. The problem is, whenever an invalid value is grabbed…
Ryan Duffing
  • 664
  • 3
  • 11
  • 20
1 2 3
99
100