Java Controls structures
Loop/Condition Syntax Example
While while (condition) { int count = 1;
// code to be executed repeatedly while (count <= 10) {
} [Link](count);
count++;}
Do-While do { int count = 11;
// code to be executed repeatedly do {
} while (condition); [Link](count);
count++;}
while (count <= 10); // This condition is always false,
but the loop executes once
If if (condition) { int number = 5;
// code to execute if condition is true if (number > 0) {
} [Link]("Positive number");}
If-Else if (condition) { int number = -3;
// code to execute if condition is true if (number > 0) {
} else { [Link]("Positive number");}
// code to execute if condition is false else if (number == 0) {
} [Link]("Zero");}
else {
[Link]("Negative number");}
Loop/Condition Syntax Example
For for (initialization; condition; for (int i = 1; i <= 5; i++) {
increment/decrement) { [Link](i);
// code to be executed repeatedly } }
Switch-Case switch (expression) { int number = 1; // Change to 1, 2, or 3
case value1:
// statements to be executed for switch (number) {
value1 case 1:
break; [Link]("The number is one.");
case value2: break;
// statements to be executed for case 2:
value2 [Link]("The number is two.");
break; break;
// ... more cases case 3:
default: [Link]("The number is three.");
// statements to be executed if none break;
of the above cases match default:
} [Link]("The number is not 1, 2, or 3.");}
Number Guessing Game:
(Guess a number between
1 and 20)
V1. Use do-while loop to continue the game
until the user guesses the correct number
using the if test.
V2. Use if-else statements to check if the
guess is too high, too low, or correct.
V3. Track the number of attempts using a
counter variable and set the maximum
attempts at 3.
Number Guessing Game (V1)
package test; do {
import [Link]; [Link]("Guess a number between
1 and 20: ");
public class Guessing { guess = [Link]();
public static void main(String[] args) { // Check if the guess is correct
if (guess == numberToGuess) {
// The random number guessedCorrect = true;
int numberToGuess = 7; [Link]("Congratulations! You
guessed the number!");}}
// Get user input while (guessedCorrect == false);
Scanner scanner = new
Scanner([Link]);
int guess; // Close the scanner
[Link]();
// Start the game loop }}
boolean guessedCorrect = false;
Number Guessing Game (V2)
package test; // Check if the guess is correct
import [Link]; if (guess == numberToGuess) {
public class Guessing { guessedCorrect = true;
[Link]("Congratulations! You
public static void main(String[] args) { guessed the number!");
// The random number } else if (guess < numberToGuess) {
int numberToGuess = 7; [Link]("Your guess is too
// Get user input low. Try again.");
Scanner scanner = new Scanner([Link]); } else {
int guess; [Link]("Your guess is too
// Start the game loop high. Try again.");
boolean guessedCorrect = false; }
do { } while (guessedCorrect == false);
[Link]("Guess a number between
1 and 20: ");
guess = [Link](); // Close the scanner
[Link]();
}}
Number Guessing Game (V3)
package test; // Check if the guess is correct
import [Link]; if (guess == numberToGuess) {
public class Guessing { guessedCorrect = true;
public static void main(String[] args) { [Link]("Congratulations! You
// The random number guessed the number!");
int numberToGuess = 7; } else if (attempts == 3) {
// Get user input [Link]("You made 3 incorrect
Scanner scanner = new Scanner([Link]); attempts: GAME OVER, the number is:
int guess; "+numberToGuess);
int attempts = 0; break;}
// Start the game loop else if (guess < numberToGuess) {
boolean guessedCorrect = false; [Link]("Your guess is too
do { low. Try again.");
[Link]("Guess a number between } else {
1 and 20: "); [Link]("Your guess is too
guess = [Link](); high. Try again.");
attempts++; }
} while (guessedCorrect == false);
// Close the scanner
[Link]();
}}
Print a multiplication table for a
given number using the for loop
public class MultiplicationTable {
public static void main(String[]
args) {
int number = 7;
for (int i = 1; i <= 12; i++) {
[Link](number + " * " +
i + " = " + (number * i));
}
}
}
Traffic light signal exercice using Switch-case
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter traffic light signal (r, y, or g):
");
char signal = [Link]().charAt(0); // Get first
character
switch (signal) {
case 'r':
[Link]("Stop!");
break;
case 'y':
[Link]("Caution!");
break;
case 'g':
[Link]("Go!");
break;
default:
[Link]("Invalid signal!");}
[Link]();}