5: Looping 'til the LLB Limit: Java's While & Do-While Loops

AP Computer Science A Aug 24, 2023

Hello again, KISJ masterminds! 🚀 Remember how we connected the sneaky seating arrangements to the Java ‘if’ statements? Today, we’ll associate a few more hilarious school antics with Java's while and do-while loops. Fasten your seatbelts because coding just got funnier!

The "While" Loop: Dancing on the Edge of 10 LLBs

Java's 'while' loop echoes that heart-thumping moment as the LLBs pile up. Remember those daredevils teetering on 9 LLBs? This loop keeps on trucking as long as its condition is met.

For instance, tracking your LLBs might look a bit like:

int LLBCount = 0;
while (LLBCount < 10) {
    // some "accidental" Korean chatter or a sneaky meme share
    LLBCount++;
    System.out.println("LLBs so far: " + LLBCount);
}

That's the thrill of living on the edge!

Do-While Loop: The Teacher's Growing Impatience

You know when you're deep in a Korean convo, and the teacher keeps warning you before they randomly snap? Here's how it'd go down in Java:

do {
    System.out.println("Continuing the Korean chat...");
    teacherAnger = my Random.nextint(20) + 1;
} while (teacherAnger < 15);

System.out.println("That's it! You’ve earned an LLB!");

This snippet keeps chatting in Korean until teacher randomly got angry and gives a student a LLB.

Break and Continue: Java's Interpretation of LLB Clutch

Break is Like getting your 9th LLB and hitting the brakes hard.

int LLBCount = 0;
while (LLBCount < 10) {
    if (LLBCount == 9) {
        System.out.println("Code red! Abandon mischief!");
        break;
    }
    LLBCount++;
}

By using break, you can break out of look like some students focusing in class after reciving 9th LLB and breaking out of their "loop of mischief."

Continue is like Momentarily goofing off (maybe a sneaky doodle?) but resuming focus quickly when teacher is around.

int funTimes = 0;
while (funTimes < 10) {
    if (funTimes == 5) { // Oops, teacher's radar pinged!
        System.out.println("I am working!");
        continue;
    }
    funTimes++;
}

By using continue, it will return to the start of the code without breaking out of the loop: like jumping web surfing routine when the teacher is watching you on the back.

However, the use of break and continue statements can make your code harder to read and debug if not used judiciously.


In conclusion, today's Java looping lessons might've felt like the thrill of accumulating LLBs at KISJ. Here's hoping both your loops and LLBs stay manageable! Keep coding, and see you next time! 🚀

Tags