While Statements

Submitted by: 
Language: 

Introduction:
This page will show you how to use the While statement.

What is the While statement?
The While statement is used to run a block of code/script constantly until its conditions become false.

When is a While statement used?
A while statement is great for multi-tasking in programs, for example; while the character is moving in a game it should run a script to update the screen graphics/player co-ordinates or location.

How to make a While statement:
To make a While statement there are only two things you need to think of;
1 - The conditions of when the While script should run.
2 - The script to be ran while the While statements conditions are true.

Examples:
Note: When I say "while the While statements conditions are true" I mean the entered condition, this does not mean you can enter a false condition. If you entered a false condition and put an invert (!) in front of the condition, the While statement would run only when the conditions are false and it would continue the program once the conditions become false (which would mean the conditions are true), makes sense? Probably not, here are some examples:
Note 2: Don't worry if you are unsure of the Try and Catch statement yet (or Threads). I have not gone over these yet but those tutorials will be up soon (if they are not already).

Standard While Statement:

  1. boolean thisTrue = true;
  2. while (thisTrue) {
  3. System.out.println("hi!");
  4. try {
  5. Thread.sleep(10000);
  6. } catch (InterruptedException e) {
  7. e.printStackTrace();
  8. }
  9. }

Gives the output:

hi!

every 10 seconds.

If we change the value of our boolean "thisTrue" to false, it does not give any output since the conditions are not true...

  1. boolean thisTrue = false;
  2. while (thisTrue) {
  3. System.out.println("hi!");
  4. try {
  5. Thread.sleep(10000);
  6. } catch (InterruptedException e) {
  7. e.printStackTrace();
  8. }
  9. }

So; for the inverted conditions. Lets say we have a false variable ("thisTrue") and we only want to run our While script while "thisTrue" is false and the script stops once the variable becomes true, we would simply use the invert operator (!)...

  1. boolean thisTrue = false;
  2. while (!thisTrue) {
  3. System.out.println("hi!");
  4. try {
  5. Thread.sleep(10000);
  6. } catch (InterruptedException e) {
  7. e.printStackTrace();
  8. }
  9. }

Again, this gives the output "hi!" every 10 seconds.

Finished!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.