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

Accessing datetime.now() values in Python

I want to be able to implement a condition in my program where it would only run for N number of hours, maybe the user could specify N, but let's not jump ahead. I figured I could use datetime.now() and store the value below in a variable, time >>>…
codaamok
  • 717
  • 3
  • 11
  • 21
3
votes
2 answers

Java comparing string to regex - while loop

I want the user to enter a string and if the string does not match my regex expression then I want a message to be outputted and the user to enter a value again. The problem is even when the string matches the regex it is treating it as if it does…
Colin747
  • 4,955
  • 18
  • 70
  • 118
3
votes
1 answer

Time complexity with while loops

I am not sure at all if I understand calculations of the time complexity. I am given these loops, and these are my calculations. However, I am not sure at all what to say about the big O's here. Loop 1: lst = [] i=1 …
nanachan
  • 1,051
  • 1
  • 15
  • 26
3
votes
1 answer

Empty While loop- Arduino

The following code prints some text once and waits for the interrupt to continue printing. The while loop is used to wait till the interrupt occurs. My issue is that the code works fine when a delay is added inside the while loop, but fails when…
mic
  • 1,165
  • 1
  • 9
  • 8
3
votes
3 answers

C# - Loop foreach until true

New to C#. But due to circumstances at work I have to "learn on the fly." Been struggling the last 2 days with my code and I consumed as many questions here and articles on MSDN as I could but I think they confused me further. I launch app A using…
Matches
  • 43
  • 1
  • 4
3
votes
1 answer

Create VBA loop for formula that contains a string search and sumifs

I have a formula that searches for a column header on another sheet, then once its found it goes through some sumifs. My data roughly looks like this: 1 8 10 11 12 Column E ... …
Kara
  • 135
  • 1
  • 3
  • 12
3
votes
5 answers

While(1) in constructor or using threads?

Is it recommed to put a while loop, which never ends in a constructor? Or should I use threads to get the same result? Is it good when a constructor never terminates? Or is it more secure to avoid segmentation faults? Hope you understand my bad…
user163408
3
votes
2 answers

How do I close a while loop and then execute another block of code? While loop is always repeating

starting out learning Python so please bear with me if this question seems very obvious. I am trying to create a high scores program in which the program would use list methods to create and mantain a list of ausers best scores for a computer game.…
TKA
  • 51
  • 2
3
votes
4 answers

BASH echo with sed

I have: Location.txt (content) some_line1 some_line2 some_line3 region.txt (content) region1 region2 region3 uri.txt (content) /some_text/some_text1/***/ /some_text/***/some_text2/ /***/some_text/some_text3/ I need to create files…
merlin.metso
  • 107
  • 1
  • 1
  • 8
3
votes
3 answers

Android Java While loop not executing

In my javacode I am using while loop where a value is adding together and making it to a single digit .. for example if it is 2010 then 2+0+1+0 = 3 and if it is 2345 then 2+3+4+5 = 14 then 1 + 4 = 5 like that ... but at the same time this process…
roshanpeter
  • 1,334
  • 3
  • 13
  • 32
3
votes
3 answers

Check regexp condition inside while loop (shell)

I have problem with running my simple shell script, where I am using while loop to read text file and trying to check condition with matching regexp: #!/bin/sh regex="^sometext" cat input.txt | { while read string do if [[ $string ~= $regex…
P.S.
  • 33
  • 3
3
votes
3 answers

Perl while loop when parse a string

I get a question about parse a vector has strings like this: "chr1-247751935-G-.:M92R,chr1-247752366-G-.:R236G," "chr1-247951785-G-.:G98K," "chr13-86597895-S-78:M34*,chr13-56891235-S-8:G87K,chr13-235689125-S-7:M389L," I want to get: "M92R…
user2917442
  • 43
  • 1
  • 4
3
votes
3 answers

Python: While Loop Check Throughout Loop if True

I have a very simple problem: I have made a while loop, and in the middle of it, set the initial condition to false. This, however, does not stop the loop and runs entirely through, (somewhat obviously,) until it attempts to unsuccessfully go…
5813
  • 1,073
  • 3
  • 14
  • 28
3
votes
4 answers

My switch (within while loop) in C goes straight to default before running properly

Here is the while loop and switch in question (track1 defines a bigger loop not shown here): while (track6 ==1) { printf ("would you like to play again? Y or N?\n"); scanf ("%c", &response); switch (response) { …
3
votes
4 answers

php - count total num rows inside while loop

I've got this piece of code: $i=0; $start_date = date("Y/m/d"); $end_date = date('Y/m/d', strtotime($start_date . " -7 days")); while($days7=mysql_fetch_assoc($q)): $next_date = strtotime($i--." days",…
oliverbj
  • 5,771
  • 27
  • 83
  • 178