FACTOID # 157: People trust Swedes! Swedish companies are the world’s least-likely to be perceived as paying bribes.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > 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. Computer programming (often shortened to programming or coding) is the process of writing, testing, and maintaining the source code of computer programs. ... In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements or instructions of an imperative program are performed or executed. ... A statement is the minimal unit of structuring in imperative programming languages. ... 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). ... In computer science, conditional statements are a vital part of a programming language. ...


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. In logic and mathematics, a logical value, also called a truth value, is a value indicating to what extent a proposition is true. ... False is the antonym of the adjective true. ... 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. ...


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. An infinite loop is a sequence of instructions in a computer program which loops endlessly. ...


For example, in the C programming language, the code fragment C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ...

 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. In computer science and mathematics, a variable (IPA pronunciation: ) (sometimes called a pronumeral) is a symbolic representation denoting a quantity or expression. ...

Contents

Demonstrating while loops

These while loops will calculate the factorial of a number: For factorial rings in mathematics, see unique factorisation domain. ...


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. 

QBasic is an IDE and interpreter for a variant of the BASIC programming language which is based on QuickBasic. ... Visual Basic (VB) is an event driven programming language and associated development environment from Microsoft for its COM programming model. ...

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. 

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

C or C++

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

C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose, high-level programming language with low-level facilities. ...

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: Perl is a dynamic programming language created by Larry Wall and first released in 1987. ...

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

PHP (PHP:Hypertext Preprocessor) is a reflective programming language originally designed for producing dynamic web pages. ...

Tcl (Tool command language)

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

Tcl (originally from Tool Command Language, but nonetheless conventionally rendered as Tcl rather than TCL; and pronounced like tickle) is a scripting language created by John Ousterhout. ...

Java, Csharp

The code for the loop is the same for Java and Csharp: Java is an object-oriented applications programming language developed by Sun Microsystems in the early 1990s. ... The title given to this article is incorrect due to technical limitations. ...

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

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. 

Pascal is an imperative computer programming language, developed in 1970 by Niklaus Wirth as a language particularly suitable for structured programming. ...

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. For other uses, see Small Talk (disambiguation). ... For closure in computer science, see closure (computer science). ...

 | 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 

Python is a high-level programming language first released by Guido van Rossum in 1991. ...

AutoIt

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

AutoIt official logo AutoIt is a freeware Microsoft Windows automation language. ...

See also


  Results from FactBites:
 
While loop - Wikipedia, the free encyclopedia (497 words)
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.
Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test 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.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.