Skip to main content

While Statements


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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.