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

Bash: Odd behavior reading from STDIN in a loop

I observe a behavior that I don't undrestand and would like someone to shed some light on it. I have two scripts, both read from STDIN. Reading a sequence of numbers from keyboard ( 1 enter 2 enter 3 enter ... ) Script A prints "x"…
Tulains Córdova
  • 2,559
  • 2
  • 20
  • 33
3
votes
5 answers

JQuery: Iterate through an array by using on('click')

I am trying to get into JQuery but am struggling a bit around. What I'm trying to do is go through an array one click at a time so that each time I click on the "next" button a new item is presented. Let's take a look at the following code:…
Paul
  • 51
  • 1
  • 1
  • 10
3
votes
2 answers

Move while jumping a square character

I need to be able to move my character while he's jumping.The thing is that I don't want the character to move in a traditional way. He is square and he should move like this when he's on the ground : I don't want him to stop moving before the…
rayan
  • 49
  • 4
3
votes
2 answers

Javascript while loop with if statements

I am trying to create a while loop in Javascript. I have a an array called numbers that contains 5 numbers. I also have a variable called bigone that is set to 0. I am trying to write a while loop that has an if statement that compares each value in…
Dolbyover
  • 89
  • 1
  • 2
  • 10
3
votes
1 answer

bash: progress output for rsync in a script

this script is a part of a linux live-cd installer. rsync -aq / /TARGET/ exclude-from=exclude.list &>> errors.log i would like to report back to a gui the progress. the gui (gtkdialog) responds to any number 0-100 (echo 1; echo 2; etc...) rsync -n…
user2225483
  • 109
  • 1
  • 3
  • 7
3
votes
1 answer

While loop not terminating even when condition is met-Java

I am not sure what is wrong and my Professor is stumped as well. The while loop condition only terminates if the condition is met right away. Once the loop is running, the met condition no longer stops it and it just keeps going. Its like the look…
3
votes
3 answers

how I can I stop the program from reading 'Enter' as an input?

Hi everyone I just finished my program which reads any single character and prints the ASCII value of that character however as it loops it starts to keep reading in enter as well as the other character. My other problem is I want my program to…
user2147550
3
votes
4 answers

Python: Make this code more compact?

How can I get rid of the excessive repetition in this code? Code: http://pastebin.com/13e2nWM9 The program calculates equations of motion (SUVAT Equations) based on data provided by the user. The section I refer to is: while True: a =…
Ricochet_Bunny
  • 537
  • 3
  • 8
  • 19
3
votes
3 answers

Why is this C code buggy?

On another question, Jerry Coffin pointed out the following: It's (probably) not really related to your question, but while (!feof(fileptr)){ is pretty much a guaranteed bug. I figured I would start a separate question since that comment is…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
3
votes
3 answers

Switch statement into while loop or while loop into case blocks?

I'm refactoring some code I wrote and I have to decide if I wanna put a switch statement into a while loop or repeat the while loop into each case blocks. The two different ways are something like the following pseudocode: Option 1) while (cnt > 0)…
3
votes
5 answers

String index out of bounds? (Java, substring loop)

This program I'm making for a COSC course isn't compiling right, I keep getting the error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2 at java.lang.String.substring(String.java:1765) at…
Brad
  • 31
  • 1
  • 1
  • 2
3
votes
3 answers

Compare variables foreach

This script scans the directory 'uploads' and list their subfolders. All subfolders has this structure YYYY-MM-DD_hh:mm:ss_text for example 2013-03-18_23:59:59_cam1 2013-03-18_09:22:12_cam1 2013-03-17_19:05:02_cam2 2013-03-17_12:30:28_cam4 I want…
Roman
  • 131
  • 2
  • 10
3
votes
1 answer

Call javascript everytime the while loop runs to give a unique waveform

I'm using the wavesurfer.js to generate a waveforms for audio clips, the problem I am having is that I have multiple audio files being pulled in a php while loop, and the javascript only seems to run for the first record returned.. I'll post what…
chriz
  • 1,580
  • 19
  • 27
3
votes
2 answers

How to move and number files?

I working with linux, bash. I have one directory with 100 folders in it, each one named different. In each of these 100 folders, there is a file called first.bars (so I have 100 files named first.bars). Although all named first.bars, the files are…
rubano
  • 31
  • 2
3
votes
5 answers

How to compare input with ASCII code?

I am trying to write a console program, which gets a number from the input and puts it into an array. The read in lasts as long as the user does not hit the "space" button. I have tried several things, but my program won't compare the input with the…
Grobi
  • 149
  • 1
  • 2
  • 10