Skip to content

Instantly share code, notes, and snippets.

@OOPUniversity
Created November 9, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OOPUniversity/434a714a9d05ac642635 to your computer and use it in GitHub Desktop.
Save OOPUniversity/434a714a9d05ac642635 to your computer and use it in GitHub Desktop.
Demonstration code for post 'Going Loopy a While', outlining the basics of 'while' loops in Java.
/*
* Copyright (c) 2014.
*/
package main.java.com.oopuniversity.basics.loops;
/**
* Created by OOPUniversity on 11/15/2014.
*/
public class While {
public static void main(String [] args) {
while1();
while2();
while3();
}
public static void while1() {
System.out.println("While loop 1");
boolean keepRunning = true;
while ( keepRunning ) {
System.out.println("I am in a loop.");
keepRunning = false;
}
System.out.println("I am no longer in a loop.");
}
public static void while2() {
System.out.println("While loop 2");
boolean keepRunning = true;
int runCount = 0;
while ( keepRunning ) {
System.out.println("I am in a loop.");
runCount = runCount + 1;
if ( runCount > 4 ) {
keepRunning = false;
}
}
}
public static void while3() {
System.out.println("While loop 3");
int runCount = 0;
while ( runCount < 5 ) {
System.out.println("I am in a loop.");
runCount ++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment