While loop - Biblioteka.sk

Upozornenie: Prezeranie týchto stránok je určené len pre návštevníkov nad 18 rokov!
Zásady ochrany osobných údajov.
Používaním tohto webu súhlasíte s uchovávaním cookies, ktoré slúžia na poskytovanie služieb, nastavenie reklám a analýzu návštevnosti. OK, súhlasím


Panta Rhei Doprava Zadarmo
...
...


A | B | C | D | E | F | G | H | CH | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

While loop
 ...
While loop flow diagram

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.

Overview

The while construct consists of a block of code and a condition/expression.[1] The condition/expression is evaluated, and if the condition/expression is true,[1] the code within all of their following in the block is executed. This repeats until the condition/expression becomes false. Because the while loop checks the condition/expression 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/expression after the loop has executed.

For example, in the languages C, Java, C#,[2] Objective-C, and C++, (which use the same syntax in this case), the code fragment

int x = 0;

while (x < 5) {
    printf ("x = %d\n", x);
    x++;
}

first checks whether x is less than 5, which it is, so then the {loop body} is entered, where the printf function is run and x is incremented by 1. After completing all the statements in the loop body, the condition, (x < 5), is checked again, and the loop is executed again, this process repeating until the variable x has the value 5.

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. For example:

while (true) {
    // do complicated stuff
    if (someCondition)
        break;
    // more stuff
}

Demonstrating while loops

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

ActionScript 3

var counter: int = 5;
var factorial: int = 1;

while (counter > 1) {
    factorial *= counter;
    counter--;
}

Printf("Factorial = %d", factorial);

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 Factorial;

APL

counter  5
factorial  1

:While counter > 0
    factorial × counter
    counter - 1
:EndWhile

  factorial

or simply

!5

AutoHotkey

counter := 5
factorial := 1

While counter > 0
    factorial *= counter--

MsgBox % factorial

Small Basic

counter = 5    ' Counter = 5
factorial = 1  ' initial value of variable "factorial"

While counter > 0
    factorial = factorial * counter
    counter = counter - 1
    TextWindow.WriteLine(counter)
EndWhile

Visual Basic

Dim counter As Integer = 5    ' init variable and set value
Dim factorial As Integer = 1  ' initialize factorial variable

Do While counter > 0
    factorial = factorial * counter
    counter = counter - 1
Loop     ' program goes here, until counter = 0

'Debug.Print factorial         ' Console.WriteLine(factorial) in Visual Basic .NET

Bourne (Unix) shell

counter=5
factorial=1
while ; do
    factorial=$((factorial * counter))
    counter=$((counter - 1))
done

echo $factorial

C, C++

int main() {
    int count = 5;
    int factorial = 1;

    while (count > 1)
        factorial *= count--;

    printf("%d", factorial);
}

ColdFusion Markup Language (CFML)

Script syntax

counter = 5;
factorial = 1;

while (counter > 1) {
    factorial *= counter--;
}

writeOutput(factorial);

Tag syntax

<cfset counter = 5>
<cfset factorial = 1>
<cfloop condition="counter GT 1">
    <cfset factorial *= counter-->
</cfloop>
<cfoutput>#factorial#</cfoutput>

Fortran

program FactorialProg
    integer :: counter = 5
    integer :: factorial = 1

    do while (counter > 0)
        factorial = factorial * counter
        counter = counter - 1
    end do

    print *, factorial
end program FactorialProg

Go

Go has no while statement, but it has the function of a for statement when omitting some elements of the for statement.

counter, factorial := 5, 1

for counter > 1 {
	counter, factorial = counter-1, factorial*counter
}

Java, C#, D

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

int counter = 5;
int factorial = 1;

while (counter > 1)
    factorial *= counter--;

JavaScript

let counter = 5;
let factorial = 1;

while (counter > 1)
    factorial *= counter--;

console.log(factorial);

Lua

counter = 5
factorial = 1

while counter > 0 do
  factorial = factorial * counter
  counter = counter - 1
end

print(factorial)

MATLAB, Octave

counter = 5;
factorial = 1;

while (counter > 0)
    factorial = factorial * counter;      %Multiply
    counter = counter - 1;                %Decrement
end

factorial

Mathematica

Blockcounter>0,            (*While loop*)
        factorial*=counter;     (*Multiply*)
        counter--;              (*Decrement*)
    ;

    factorial

Oberon, Oberon-2, Oberon-07, Component Pascaledit

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.

Maya Embedded Languageedit

int $counter = 5;
int $factorial = 1;

int $multiplication;

while ($counter > 0) {
    $multiplication = $factorial * $counter;

    $counter -= 1;

    print("Counter is: " + $counter + ", multiplication is: " + $multiplication + "\n");
}

Nimedit

var
  counter = 5            # Set counter value to 5
  factorial = 1          # Set factorial value to 1

while counter > 0:       # While counter is greater than 0
    factorial *= counter # Set new value of factorial to counter.
    dec counter          # Set the counter to counter - 1.

echo factorial

Non-terminating while loop:

while true:
  echo "Help! I'm stuck in a loop!"

Pascaledit

Pascal has two forms of the while loop, while and repeat. While repeats one statement (unless enclosed in a begin-end block) as long as the condition is true. The repeat statement repetitively executes a block of one or more statements through an until statement and continues repeating unless the condition is false. The main difference between the two is the while loop may execute zero times if the condition is initially false, the repeat-until loop always executes at least once.

program Factorial1;
var
    Fv: integer;

    procedure fact(counter:integer);
    var
        Factorial: integer;

    begin
         Factorial := 1;

         while Counter > 0 do
         begin
             Factorial := Factorial * Counter;
             Counter := Counter - 1
         end;

         WriteLn(Factorial)
     end;

begin
    Write('Enter a number to return its factorial: ');
    readln(fv);
    repeat
         fact(fv);
         Write('Enter another number to return its factorial (or 0 to quit): ');
     until fv=0;
end.

Perledit

my $counter   = 5;
my $factorial = 1;

while ($counter > 0) {
    $factorial *= $counter--; # Multiply, then decrement
}

print $factorial;

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

open IN, "<test.txt";

while (<IN>) {
    print;
}

close IN;

PHPedit

$counter = 5;
$factorial = 1;

while ($counter > 0) {
    $factorial *= $counter--; // Multiply, then decrement.
}

echo $factorial;

PL/Iedit

declare counter   fixed initial(5);
declare factorial fixed initial(1);

do while(counter > 0)
    factorial = factorial * counter;
    counter = counter - 1;
end;

Pythonedit

counter = 5                           # Set the value to 5
factorial = 1                         # Set the value to 1

while counter > 0:                    # While counter(5) is greater than 0
    factorial *= counter              # Set new value of factorial to counter.
    counter -= 1                      # Set the counter to counter - 1.

print(factorial)                      # Print the value of factorial.

Non-terminating while loop:

while True:
    print("Help! I'm stuck in a loop!")

Racketedit

In Racket, as in other Scheme implementations, a named-let is a popular way to implement loops:

#lang racket
Zdroj:https://en.wikipedia.org?pojem=While_loop
Text je dostupný za podmienok Creative Commons Attribution/Share-Alike License 3.0 Unported; prípadne za ďalších podmienok. Podrobnejšie informácie nájdete na stránke Podmienky použitia.






Text je dostupný za podmienok Creative Commons Attribution/Share-Alike License 3.0 Unported; prípadne za ďalších podmienok.
Podrobnejšie informácie nájdete na stránke Podmienky použitia.

Your browser doesn’t support the object tag.

www.astronomia.sk | www.biologia.sk | www.botanika.sk | www.dejiny.sk | www.economy.sk | www.elektrotechnika.sk | www.estetika.sk | www.farmakologia.sk | www.filozofia.sk | Fyzika | www.futurologia.sk | www.genetika.sk | www.chemia.sk | www.lingvistika.sk | www.politologia.sk | www.psychologia.sk | www.sexuologia.sk | www.sociologia.sk | www.veda.sk I www.zoologia.sk