Information about While Loop

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 first evaluated - if the condition is true the code within the block is then 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

x = 0; while (x < 3) { x++; }

first checks whether x is larger than 3, which it is not, 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.

Demonstrating while loops

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

QBasic or Visual Basic

'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.

REALbasic

Dim counter as Integer = 5 Dim factorial as Integer = 1 While counter > 0 factorial = factorial * counter //Multiply counter = counter - 1 //Decrement Wend MsgBox Str( factorial ) // Prints out the result.

C or C++

unsigned int counter = 5; unsigned long factorial = 1; while (counter > 0) { factorial *= counter--; /*Multiply, then decrement.*/ } printf("%i", factorial);

Perl

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

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

$factorial *= $counter-- while $counter > 0;

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;

PHP

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

Tcl (Tool command language)

set counter 5 set factorial 1 while {$counter > 0} { set factorial [expr $factorial * $counter] incr counter -1 } puts $factorial

Java, C#

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

int counter = 5; long factorial = 1; while (counter > 0) factorial *= counter--; // Multiply, then decrement.

For Java the result is printed as follows: System.out.println(factorial);

The same in C# System.Console.WriteLine(factorial);

JavaScript

>
var counter = 5;
var factorial = 1;
while (counter)
{
  factorial *= counter--; //Multiply, then decrement.
}
document.body.appendChild(document.createTextNode(factorial));

Pascal

program Factorial; var Counter, Factorial: integer; begin Counter := 5; Factorial := 1; while Counter > 0 do begin Factorial := Factorial * Counter; Counter := Counter - 1; end; Write(Factorial); end.

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.

| count factorial | count := 5. factorial := 1. [ count > 0 ] whileTrue: [ factorial := factorial * (count := count - 1) ]

Python

counter = 5 factorial = 1 while counter > 0: factorial *= counter counter -= 1 print factorial

AutoIt

Dim $counter = 5, $factorial = 1 While $counter > 0 $factorial *= $counter $counter -= 1 WEnd MsgBox(0,"Factorial", $factorial)

Windows PowerShell

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

See also

Computer programming (often shortened to programming or coding) is the process of writing, testing, and maintaining the source code of computer programs. The source code is written in a programming language.
..... Click the link for more information.
In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements, instructions or function calls of an imperative or functional program are executed or
..... Click the link for more information.
In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements. A statement will have internal components (eg, expressions).
..... Click the link for more information.
In computer science, the Boolean datatype, sometimes called the logical datatype, is a primitive datatype having two values: one and zero (which are equivalent to true and false).
..... Click the link for more information.
conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified condition
..... Click the link for more information.
In logic and mathematics, a logical value, also called a truth value, is a value indicating the extent to which a proposition is true.

In classical logic, the only possible truth values are true and false.
..... Click the link for more information.
In most computer programming languages, a do while loop, sometimes just called a do loop, is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
..... Click the link for more information.
C

The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language.
..... Click the link for more information.
C++
Paradigm: Multi-paradigm
Appeared in: 1983
Designed by: Bjarne Stroustrup
Typing discipline: Static, unsafe, nominative
Major implementations: G++, Microsoft Visual C++, Borland C++ Builder
Dialects: ISO/IEC C++ 1998, ISO/IEC C++ 2003
..... Click the link for more information.
variable (IPA pronunciation: [ˈvæɹiəbl]) (sometimes called a pronumeral) is a symbolic representation denoting a quantity or expression.
..... Click the link for more information.
infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition or having one that can never be met.
..... Click the link for more information.
In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements, instructions or function calls of an imperative or functional program are executed or
..... Click the link for more information.
factorial of a non-negative integer is the product of all positive integers less than or equal to . For example,


and
where represents n factorial.
..... Click the link for more information.
QBasic

Appeared in: 1991 - 1998
Developer: Microsoft Corporation
OS: MS-DOS, Windows 95, Windows 98
License: MS-EULA
Website: www.microsoft.com

QBasic
..... Click the link for more information.
Visual Basic

Paradigm: Event-driven
Developer: Microsoft
Typing discipline: Static, strong
Influenced by: QuickBASIC
Influenced: Visual Basic .NET
OS: Microsoft Windows

Visual Basic (VB
..... Click the link for more information.
REALbasic (RB) is an object-oriented dialect of the BASIC programming language developed and commercially marketed by REAL Software, Inc in Austin, Texas for Mac OS X, Microsoft Windows, and Linux.

History

REALbasic was created by Andrew Barry.
..... Click the link for more information.
C

The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language.
..... Click the link for more information.
C++
Paradigm: Multi-paradigm
Appeared in: 1983
Designed by: Bjarne Stroustrup
Typing discipline: Static, unsafe, nominative
Major implementations: G++, Microsoft Visual C++, Borland C++ Builder
Dialects: ISO/IEC C++ 1998, ISO/IEC C++ 2003
..... Click the link for more information.
Perl

Paradigm: Multi-paradigm
Appeared in: 1987
Designed by: Larry Wall
Latest release: 5.8.8/ January 31 2006
Typing discipline: Dynamic
Influenced by: AWK, BASIC, BASIC-PLUS, C, C++, Lisp, Pascal, Python, sed, Unix shell
..... Click the link for more information.
PHP

Paradigm: imperative, object-oriented
Appeared in: 1995
Designed by: Rasmus Lerdorf
Developer: The PHP Group
Latest release: 5.2.4/ 30 August 2007
Typing discipline: Dynamic, weak (duck typing)
Influenced by: C, Perl
Java, C++, Python
..... Click the link for more information.
TCL may mean:
  • Tcl (Tool Command Language), a computer programming language
  • Terminal Control Language, used to program VeriFone devices
  • Terminal Control Language, a Pick operating system command language

..... Click the link for more information.
Java

Paradigm: Object-oriented, structured, imperative
Appeared in: 1995
Designed by: Sun Microsystems
Typing discipline: Static, strong, safe, nominative
Major implementations: Numerous
Influenced by: Objective-C, C++, Smalltalk, Eiffel,[1]
..... Click the link for more information.
C#
Paradigm: structured, imperative, object-oriented
Appeared in: 2001 (last revised 2005)
Designed by: Microsoft Corporation
Typing discipline: static, strong, both safe and unsafe, nominative
Major implementations: .NET Framework, Mono, DotGNU
Dialects: 1.
..... Click the link for more information.
JavaScript
Paradigm: multi-paradigm
Appeared in: 1995
Designed by: Brendan Eich
Developer: Netscape Communications Corporation, Mozilla Foundation
Typing discipline: dynamic, weak, duck
Major implementations: SpiderMonkey, Rhino, KJS, JavaScriptCore
..... Click the link for more information.
Pascal is a structured imperative computer programming language, developed in 1970 by Niklaus Wirth as a language particularly suitable for structured programming. A derivative known as Object Pascal was designed for object oriented programming.
..... Click the link for more information.
Smalltalk
Paradigm: object-oriented
Appeared in: Development started in 1969
Publicly available in 1980
Designed by: Alan Kay, Dan Ingalls, Adele Goldberg
Developer: Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and Xerox PARC
..... Click the link for more information.
Closure may refer to:
  • Algebraic closure
  • Closure (computer science), an abstraction binding a function to its scope
  • Closure (mathematics), the smallest object that both includes the object as a subset and possesses some given property
  • Closure (logic)

..... Click the link for more information.
Python

Paradigm: Multi-paradigm
Appeared in: 1991
Designed by: Guido van Rossum
Developer: Python Software Foundation
Latest release: 2.5.1/ April 18 2007
Latest unstable release: 3.
..... Click the link for more information.
AutoIt is a freeware Microsoft Windows automation language. In its earliest release, the software was primarily intended to create automation scripts (sometimes called macros) for Microsoft Windows programs.
..... Click the link for more information.
Windows PowerShell, previously Microsoft Shell or MSH (codenamed Monad) is an extensible command line interface (CLI) shell and scripting language product developed by Microsoft. The product is based on object-oriented programming and version 2.
..... Click the link for more information.


This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus


page counter