While loop

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

Expression error: Unrecognized punctuation character "{".

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 while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.

For example, in the C programming language (as well as Java and C++, which use the same syntax in this case), the code fragment

<source lang="c"> x = 0; while (x < 3) {

  printf("x = %d\n",x);
  x++;

} </source>

first checks whether x is less than 3, which it is, so it increments x by 1. It then checks the condition again, and executes again, repeating this process until the variable x has the value 3.

Note that it is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that controls termination of the loop.

Equivalent constructs

<source lang="c"> while (condition) {

  statements;

} </source>

is equivalent to

<source lang="c"> if (condition) {

  do {
     statements;
  } while (condition);

} </source>

or

<source lang="c"> while (true) {

  if (!condition) break;
  statements;

}</source>

or

<source lang="c">

  goto TEST;

LOOPSTART:

  statements;

TEST:

  if (condition) goto LOOPSTART;

</source>

Also, in C and its descendants, a while loop is a for loop with no initialization or counting expressions, i.e.,

<source lang="c"> for ( ; condition; ) {

  statements;

} </source>

Demonstrating while loops

These while loops will calculate the factorial of the number 5:

Ada

<source lang="ada"> with Ada.Integer_Text_IO;

procedure Factorial is

 Counter   : Integer := 5;
 Factorial : Integer := 1;

begin

 while Counter > 0 loop
   Factorial := Factorial * Counter;
   Counter   := Counter - 1;
 end loop;
 Ada.Integer_Text_IO.Put (Factorial);

end Main; </source>

Bash

<source lang="bash"> counter=5 factorial=1 while [ $counter -gt 0 ]; do

   factorial=$((factorial * counter))
   counter=$((counter - 1))

done

echo $factorial </source>

QBasic or Visual Basic

<source lang="vb"> ' Initialize the variables Dim counter As Integer : counter = 5 Dim factorial As Long : factorial = 1

Do While counter > 0

 factorial = factorial * counter     ' Multiply
 counter = counter - 1               ' Decrement

Loop

Print factorial ' Prints out the result. </source>

  1. include<stdio.h>

main() { int d;

C or C++

<source lang="c"> unsigned int counter = 5; unsigned long factorial = 1;

while (counter > 0) {

 factorial *= counter--;    //Multiply and decrement

}

printf("%i", factorial); </source>

Java, C#, D

The code for the loop is the same for Java, C# and D:

<source lang="csharp"> int counter = 5; long factorial = 1;

while (counter > 1) {

  factorial *= counter--;

} </source>

For Java the result is printed as follows:

 <source lang="java">System.out.println(factorial);</source>

The same in C#

 <source lang="csharp">System.Console.WriteLine(factorial);</source>

And finally in D

 <source lang="d">writefln(factorial);</source>

JavaScript

<source lang="javascript"> var counter = 5; var factorial = 1;

while ( --counter > 1 ) {

 factorial *= counter;

}

document.body.appendChild(document.createTextNode(factorial)); </source>

Lua

<source lang="lua"> counter = 5 factorial = 1

while counter > 0 do

 factorial = factorial * counter
 counter = counter - 1

end

print(factorial) </source>

MATLAB

<source lang="matlab"> counter = 5; factorial = 1;

while (counter > 0)

 factorial = factorial * counter;      %Multiply
 counter = counter - 1;                %Decrement

end

factorial </source>

Mathematica

Block[{counter=5,factorial=1},          (*localize counter and factorial*)
        While[counter>0,                (*While loop*)
               factorial*=counter;      (*Multiply*)
               counter--;               (*Decrement*)
             ];
     factorial
    ]

Pascal

<source lang="pascal"> program Factorial1; var

 Counter, Factorial: integer;

begin

 Counter := 5;
 Factorial := 1;
 while Counter > 0 do
 begin
   Factorial := Factorial * Counter;
   Counter := Counter - 1
 end;
 WriteLn(Factorial)

end. </source>

Oberon, Oberon 2, Oberon 07, or Component Pascal

<source lang="oberon2"> MODULE Factorial; IMPORT Out; VAR

 Counter, Factorial: INTEGER;

BEGIN

 Counter := 5;
 Factorial := 1;
 WHILE Counter > 0 DO
   Factorial := Factorial * Counter;
   DEC(Counter)
 END;
 Out.Int(Factorial,0)

END Factorial. </source>

Perl

<source lang="perl"> my $counter = 5; my $factorial = 1;

while ( $counter > 0 ) {

   $factorial *= $counter--; # Multiply, then decrement

}

print $factorial; </source>

Very similar to C and C++, but the while loop could also have been written on one line:

 <source lang="perl">$factorial *= $counter-- while $counter > 0;</source>

While loops are frequently used for reading data line by line (as defined by the $/ line separator) from open filehandles:

<source lang="perl"> open IN, "<test.txt"; while ( <IN> ) {

 print;

} close IN; </source>

PHP

<source lang="php"> $counter = 5; $factorial = 1; while($counter > 0) {

 $factorial *= $counter; // Multiply first.
 $counter--; // then decrement.

} print $factorial; </source>

Python

<source lang="python"> counter = 5 factorial = 1

while counter > 0:

     factorial *= counter
     counter -= 1

print factorial </source>

Smalltalk

Contrary to other languages, in Smalltalk a while loop is not a language construct but defined in the class BlockClosure as a method with one parameter, the body as a closure, using self as the condition.

<source lang="smalltalk"> | count factorial | count := 5. factorial := 1. [ count > 0 ] whileTrue:

   [ factorial := factorial * (count := count - 1) ]

Transcript show: factorial </source>

Tcl (Tool command language)

<source lang="tcl"> set counter 5 set factorial 1

while {$counter > 0} {

 set factorial [expr $factorial * $counter] 
 incr counter -1 

}

puts $factorial </source>

Windows PowerShell

$counter = 5
$factorial = 1
while ($counter -gt 0) {
  $factorial *= $counter-- # Multiply, then decrement.
}
Write-Output $factorial

See also

cs:Cyklus while-do da:While-løkke de:While-Schleife es:Bucle while eu:While begizta ko:While 루프 hr:While petlja ja:While文 pt:Estrutura de repetição sv:While

If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...