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

Why does this cause an infinite while loop?

I'm completely new to coding and have started learning JavaScript recently. I don't understand why the following code causes an infinite loop. Why does the birthday(myAge) function not work within the loop to make the condition (myAge < 23)…
JosephByrne
  • 123
  • 7
3
votes
4 answers

Using a thread to iterate over an array to display one item/ second

I am trying to create a while or for loop that display an indexed string or int in order using a thread, so that it changes to the next index automatically. This is what I have so far. It is in c# but plan to use the same information for…
user2745076
3
votes
2 answers

Can't get txt file to print correctly

This is a homework assignment for my C Programming class. I am given a text file, with two data columns; the first column is the age; the second column is the avgprice. I'm able to read and print the values fine. However, for some reason the age and…
Nxt3
  • 1,970
  • 4
  • 30
  • 52
3
votes
4 answers

Social Security Number Check - Python

Writing a program that prompts the user to enter a social security number in the format ddd-dd-dddd where d is a digit. The program displays "Valid SSN" for a correct Social Security number or "Invalid SSN" if it's not correct. I nearly have it,…
user3105664
  • 179
  • 3
  • 6
  • 11
3
votes
3 answers

Why is my program looping too many times?

I'm a beginner in C and trying to create a program and having a problem with my main function. Problem: After asking how many integers they'd like to enter, for example: 4 numbers, the loop goes on 5 times essentially taking in 5 numbers. It also…
Abushawish
  • 1,466
  • 3
  • 20
  • 35
3
votes
1 answer

SQL WHILE Loops

I have been working on creating a nested while loop in SQL, but having issues with the while loop. I think the main issue is with my outer loop. Any suggestions? USE HW_DB; IF OBJECT_ID('dbo.PythagoreanTriangles') IS NOT NULL DROP TABLE…
ChaseHardin
  • 2,189
  • 7
  • 29
  • 50
3
votes
1 answer

How can I make this statement (within a while-loop) not print out twice to the console?

I am very new to Java, (and to programming altogether). I am sure that the solution to my problem is very simple, but I just can't figure it out. The code below is a small chunk of my program. But it is still compile-able, and still has the same…
3
votes
2 answers

Perl: Please explain to me the following behaviours of while()

1.Why does while (<$filehandle>) iterate through a file seemlessly, but it doesn't iterate through lists or arrays without manipulating the array itself (like using while ($_ = shift @array){print $_}? How does this happen? 2.How is it that while…
steelmonkey
  • 415
  • 2
  • 6
  • 19
3
votes
3 answers

Recursive Function and Non-recursive Function Returning Different Results

I'm creating two functions that are supposed to emulate and return the result of f(i) = 1/1 + 1/2 + 1/3 ... 1/i. One function is recursive, and I'm testing that the recursive function functions correctly by implementing a non-recursive version of…
3
votes
6 answers

While Loop that doesn't keep looping

For beginner practice I'm trying to create a simple loop that accepts a single character from the user, prints that character to the console and keeps doing that until the user enters 'R'. using System; using System.Collections.Generic; using…
rsgmon
  • 1,892
  • 4
  • 23
  • 35
3
votes
3 answers

Switch case to break an outer loop?

This isn't a specific problem that I actually am trying to implement, but it ocurred to me that I do not know how to do this, so I will give a simple example to illustrate: Suppose we have a while loop containing a switch statement, for…
OJFord
  • 10,522
  • 8
  • 64
  • 98
3
votes
8 answers

What is the functionality of the while loop inside the if statement? JavaScript

Below is a function that returns the prime factors of a given number in JavaScript. I did not write the function but have been studying it to extend my programming knowledge. My questions are about the while loop that is inside the following if…
JaredRogers
  • 33
  • 1
  • 1
  • 4
3
votes
1 answer

simple while loop in bash

I've started learning bash scripting. I wrote simple while loop, but it doesn't work. it's say that : command not found.does anybody knows why ? Here is my code: let x=5; while [$x -lt 10];do echo "x is : $x";let x=$x+1; done
user2806363
  • 2,513
  • 8
  • 29
  • 48
3
votes
3 answers

Creating infinite loop without breaking program

I have a game grid that updates in "steps"(Conway's game of life, though that isn't related to my problem in particular). I'm trying to create a Play button that runs the simulation automatically until the button is pressed again to pause the…
user3294629
  • 95
  • 1
  • 7
3
votes
1 answer

While Loop executing one last time when condition is set to false

Task: To check that if a user input string has the same first and last character. If yes or no, output to the screen, if the user enters "done", the loop is exited. Issue: While loop executes when condition is false What I've tried: Using different…
bradleyduncan
  • 49
  • 2
  • 10