Lesson 3
Navigating Conditional Asteroids: Java Loops and Decisions
Introduction to the Lesson

Welcome to our exploration of how to combine Java loop structures with conditional statements. By merging these elements together, your loops will be supercharged, enabling them to perform versatile actions based on different conditions.

Reviewing Java Loop Structures

Before charging forward, it's essential to revisit the foundation: loops. Within the vast cosmos of Java, we've navigated two primary types of loops: the for and while loops.

A for loop iterates a predetermined number of times, much like a reliable spaceship following a set route:

Java
1for (int i = 0; i < 5; i++) { // Iterates five times 2 System.out.println(i); // Prints 0 to 4 3}

The while loop, on the other hand, repeats actions until a specified condition turns false:

Java
1int i = 0; 2while (i < 5) { // Condition for the loop to continue 3 System.out.println(i); // Prints 0 to 4 4 i++; // Increment the counter 5}
Reviewing Java Conditional Statements

Let's now revisit the if-else construct, which is Java's machinery for making decisions.

Java
1int asteroidsDistance = 10; 2 3if (asteroidsDistance > 15) { // If this condition is true 4 System.out.println("Navigate through the asteroids."); // This line executes 5} else { 6 System.out.println("Steer clear of the asteroids."); // Else, this line executes 7}

The if-else statement enables the spaceship to decide whether to navigate through the asteroids based on their distance.

Combining Loops and Conditional Statements

Next, consider how the for loop integrates with an if-else statement:

Java
1for (int i = 0; i < 6; i++) { // Our loop will iterate 10 times 2 if (i % 2 == 0) { // If the number is divisible by 2 (Even) 3 System.out.println(i + " is even."); // It'll print "X is even." 4 } else { // If not divisible by 2 (Odd) 5 System.out.println(i + " is odd."); // It'll print "X is odd." 6 } 7} 8// Prints: 9// 0 is even. 10// 1 is odd. 11// 2 is even. 12// 3 is odd. 13// 4 is even. 14// 5 is odd.

In a similar fashion, an if-else statement can be applied within a while loop:

Java
1int i = 0; 2while (i < 7) { // Our loop will iterate 10 times 3 if (i % 3 == 0) { // If the number is divisible by 3 4 System.out.println(i + " is divisible by 3."); // It'll print "X is divisible by 3." 5 } else { // If not divisible by 3 6 System.out.println(i + " is not divisible by 3."); // It'll print "X is not divisible by 3." 7 } 8 i++; // Increments the counter. 9} 10// Prints: 11// 0 is divisible by 3. 12// 1 is not divisible by 3. 13// 2 is not divisible by 3. 14// 3 is divisible by 3. 15// 4 is not divisible by 3. 16// 5 is not divisible by 3. 17// 6 is divisible by 3.
Real-life Examples

Picture this scenario: you've prepared a shopping list (an array). As you proceed through the market (akin to the for loop), you check items (the if condition) off your list. If discounts are available, you purchase the luxury goods; if not, you buy them only if they're essential.

Java
1String[] shoppingList = {"Potatoes", "Tomatoes", "Luxury Martian Rice"}; 2boolean discount = true; 3 4for (String item : shoppingList) { // Iterating over the shopping list 5 // If a discount is available on a luxury item, it will be purchased. 6 // Else, if it's an essential item, it will be purchased. 7 // Otherwise, the luxury item will be skipped. 8 if (item.equals("Luxury Martian Rice") && discount) { 9 System.out.println("Buy " + item + " with discount."); 10 } else if (!item.equals("Luxury Martian Rice")) { 11 System.out.println("Buy essential " + item); 12 } else { 13 System.out.println("No discount. Skip " + item); 14 } 15}

In another setting, consider a space game. As long as you have life points (represented by a while loop), your points increase if you hit an alien (an if condition)!

Java
1int score = 0; 2int lifePoints = 5; 3 4while (lifePoints > 0) { // As long as life-points remain 5 // Random generator for hit (true) or miss (false) 6 boolean isAlienHit = Math.random() > 0.5; 7 8 // If you hit, it awards 10 points. 9 // If you missed, it deducts a life point. 10 if (isAlienHit) { 11 System.out.println("Alien vessel hit! +10 points"); 12 score += 10; 13 } else { 14 System.out.println("Missed! -1 life point"); 15 lifePoints -= 1; 16 } 17} 18 19// Displays the final score when the game ends. 20System.out.println("Game Over. Your score is " + score);
Lesson Summary and Practice

By now, you would have learned how to combine Java loops and if-else statements effectively. Prepare yourself for some absorbing practice to solidify this exciting programming skill further! You're on the way to tackling more intricate problems in Java. Embark on a fulfilling journey!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.