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

How can I increment a number in a while-loop while preserving leading zeroes (BASH < V4)

I am trying to write a BASH script that downloads some transcripts of a podcast with cURL. All transcript files have a name that only differs by three digits: filename[three-digits].txt from filename001.txt to.... filename440.txt. I store the…
Senkaku
  • 197
  • 2
  • 10
3
votes
5 answers

Saving values inside while loop

I am trying to read values of my country array string which reads csv file. InputStreamReader reader = new InputStreamReader(asset_stream); br = new BufferedReader(reader); String[] country = null; String cvsSplitBy = ";"; try { while ((line…
muktoshuvro
  • 440
  • 1
  • 7
  • 26
3
votes
2 answers

Java program appears stuck once while loop is entered

This is my first question to this site so I apologize and would appreciate feedback if I post something incorrectly! This is a homework question although I don't seem to be able to tag it that way. Anyways, the code appears to compile fine (using…
Amber
  • 41
  • 3
3
votes
3 answers

c# Invalid token 'while' in class, struct, or interface member declaration (while loop not working)

I'm new to c# but quite familiar with c, this error however doesn't make much sense to me, as I have specifically followed the required syntax shown on http://msdn.microsoft.com/en-us/library/2aeyhxcd.aspx using System; using…
3
votes
1 answer

Learning C (via K&R) using xcode

I'm learning C with The C Programming Language (K&R). Since I don't particularly want to bob back and forth between a text editor and running gcc, I've decided to use xcode as an IDE. So far, I've been able to follow the book's examples without a…
deeb
  • 1,332
  • 4
  • 15
  • 27
3
votes
4 answers

Unreachable code while loop

When I'm compiling this code public static void main(String [] args) { int x = 0; while(false) { System.out.println(hello); } } it is showing compile time error…
Bifrost
  • 417
  • 5
  • 23
3
votes
4 answers

C# integer validation

I have searched stackoverflow and haven't found anything that answers my question. Unfortunately it is not an easy question to word as a search query. I'm using c# and I have a menu that asks the user to pick an option from 1 - 4. I am validation…
user3158872
3
votes
1 answer

Terminating a while loop after 2 seconds for a lengthy process within the loop

I have a while loop which contains a process which takes roughly 1 second each time but occasionally takes over 5 minutes. I want the loop to terminate after 2 seconds in case this 5 minute scenario occurs. I have already tried import time t1 =…
Statisdisc
  • 33
  • 3
3
votes
2 answers

Indicating end-of-file

In the following simple code snippet, the end-of-file can be easily indicated by pressing ctrl-z. double x; while ( cin >> x ) { ++count; sum += x; } But when I try a more complicated program, such as reading in student records to do some…
user285372
3
votes
1 answer

How to accept input without the need to press enter Python 3

i'm wondering how to accept input without the need to press enter. i searched online, and i get something regarding raw_input, but i think that became obsolete after the arrival of python 3.0. sometimes, i run a while loop on a whole program since i…
amin
  • 429
  • 4
  • 8
  • 15
3
votes
9 answers

Average, max, and min program in C

So I'm coding in C, and I need to come up with code that will take n numbers from the user, and find their minimum, maximum, average, and sum of squares for for their values. So far I have the average and sum of squares portion, but the minimum and…
user3133973
  • 47
  • 1
  • 1
  • 3
3
votes
3 answers

Recursively print the reverse of a String in Java

For some reason when the string length is zero, it does not come out of the while loop. Can some one help me on this? static String str1 = ""; public static void reverse(String str) { while (str.length() > 0) { str1 = str1 +…
prc
  • 43
  • 2
3
votes
4 answers

Python - Implementing a numerical equation solver (Newton-Raphson)

I am warning you, this could be confusing, and the code i have written is more of a mindmap than finished code.. I am trying to implement the Newton-Raphson method to solve equations. What I can't figure out is how to write this equation in…
user2906011
  • 129
  • 2
  • 3
  • 7
3
votes
3 answers

.exe crashes when I enter a vaue for &records[*rCount].source

Update * I have now tried to return something from the function and still the .exe crashes! I am quite new to c so sorry if I am been a bit thick at not spotting why. struct packet* addRecord(int *rCount, struct packet *records){ int valid = 0; …
user3103598
  • 185
  • 1
  • 1
  • 10
3
votes
2 answers

python overwrite output on same line

I have the below foo = ['a', 'b', 'c', 'd', 'e'] from random import choice while True: print choice(foo) The output is: a c a e ... I want the terminal output to overwrite the old output on the same line thanks