Main Menu

HOME

.Net
ASP
Assembly
C
C++
Delphi
HTML
Java
JavaScript
MySQL
PC interface
Powershell
Perl
PHP
VBScript
Visual Basic
XML

US Job listings




   Misc

   Amazon

   Links

The While loop


 

While loops

A while loop is the most basic type of loop. It will run as long as the condition is non-zero (true). For example, if you try the following, the program will appear to lock up and you will have to manually close the program down. A situation where the conditions for exiting the loop will never become true is called an infinite loop.

int a=1;
while(42) {
   a = a*2;
}
                                          

Here is another example of a while loop. It prints out all the exponents of two less than 100.

int a=1;
while(a<100) {
   printf("a is %d \n",a);
   a = a*2;
}
                                          

The flow of all loops can also be controlled by break and continue statements. A break statement will immediately exit the enclosing loop. A continue statement will skip the remainder of the block and start at the controlling conditional statement again. For example:

int a=1;
while (42) { // loops until the break statement in the loop is executed
   printf("a is %d ",a);
   a = a*2;
   if(a>100)
       break;
   else if(a==64)
       continue;  // Immediately restarts at while, skips next step
   printf("a is not 64\n");
}
                                          

In this example, the computer prints the value of a as usual, and prints a notice that a is not 64 (unless it was skipped by the continue statement).

Similar to If above, braces for the block of code associated with a While loop can be omitted if the code consists of only one statement, for example:

int a=1;
while(a < 100)
   a = a*2;
                                          

This will merely increase a until a is not less than 100.

It is very important to note, once the controlling condition of a While loop becomes 0 (false), the loop will not terminate until the block of code is finished and it is time to reevaluate the conditional. If you need to terminate a While loop immediately upon reaching a certain condition, consider using break.

A common idiom is to write:

int i = 5;
while(i--)
   printf("java and c# can't do this\n");
                                          

This executes the code in the while loop 5 times, with i having values that range from 4 down to 0. Conveniently, these are the values needed to access every item of an array containing 5 elements.


 

All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks

 

 

 



 




   Sponsors
 

   Software
500 Java Tips E-book
PHP editor
PERL editor
Beginning Java
Beginning Visual Basic
Learn VB.net
Learn VB 6
VB and databases
ASP image library
C++ builder programming
C++ fundamentals

   Source Code
start notepad from powershell(Powershell)
API disable ctrl alt and del .Not XP or 2000(VB)
echo keyboard input to screen(Darkbasic)
Enum example(C++)
Octal, hex and decimal(C++)
Launch Add New Hardware wizard(VB)
split a file into path info(php)
get the path of the favorites folder(VB.net)




Copyright © 2004 by programmershelp.co.uk