Science Fair Projects Ideas - While loop

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

While loop

In most computer programming languages, a while loop is a control structure that allows code to be executed repeatedly based on a given boolean condition.

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.

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.

For example, in the C programming language, the code fragment

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

first checks whether x is less 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 desired value, 3.

Demonstrating while loops

These while loops will calculate the factorial of a number:

In QBasic or Visual Basic:

 Dim Counter as Byte, Factorial as Long
 Counter = 5 'Value to take factorial of.
 Factorial = 1
 While (Counter > 0)
   Factorial = Factorial * Counter
   Counter = Counter - 1
 Wend
 Print Factorial 'Prints out the result.

In C or C++:

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

In Tcl (Tool command language)

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


In Java programming language:

 int counter = 5;
 int factorial = 0;
 while (counter > 0){
    factorial *= counter--;
 }
 System.out.println(factorial);
03-10-2013 05:06:04
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice