Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9
Java Controls structures
Loop/Condition Syntax Example
While while (condition) { int count = 1;
// code to be executed repeatedly while (count <= 10) { } System.out.println(count); count++;}
Do-While do { int count = 11;
// code to be executed repeatedly do { } while (condition); System.out.println(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) { } System.out.println("Positive number");} If-Else if (condition) { int number = -3; // code to execute if condition is true if (number > 0) { } else { System.out.println("Positive number");} // code to execute if condition is false else if (number == 0) { } System.out.println("Zero");} else { System.out.println("Negative number");} Loop/Condition Syntax Example
For for (initialization; condition; for (int i = 1; i <= 5; i++) {
increment/decrement) { System.out.println(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; System.out.println("The number is one."); case value2: break; // statements to be executed for case 2: value2 System.out.println("The number is two."); break; break; // ... more cases case 3: default: System.out.println("The number is three."); // statements to be executed if none break; of the above cases match default: } System.out.println("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 java.util.Scanner; System.out.print("Guess a number between 1 and 20: "); public class Guessing { guess = scanner.nextInt();
public static void main(String[] args) { // Check if the guess is correct
if (guess == numberToGuess) { // The random number guessedCorrect = true; int numberToGuess = 7; System.out.println("Congratulations! You guessed the number!");}} // Get user input while (guessedCorrect == false); Scanner scanner = new Scanner(System.in); int guess; // Close the scanner scanner.close(); // Start the game loop }} boolean guessedCorrect = false; Number Guessing Game (V2) package test; // Check if the guess is correct import java.util.Scanner; if (guess == numberToGuess) { public class Guessing { guessedCorrect = true; System.out.println("Congratulations! You public static void main(String[] args) { guessed the number!"); // The random number } else if (guess < numberToGuess) { int numberToGuess = 7; System.out.println("Your guess is too // Get user input low. Try again."); Scanner scanner = new Scanner(System.in); } else { int guess; System.out.println("Your guess is too // Start the game loop high. Try again."); boolean guessedCorrect = false; } do { } while (guessedCorrect == false); System.out.print("Guess a number between 1 and 20: "); guess = scanner.nextInt(); // Close the scanner scanner.close(); }} Number Guessing Game (V3) package test; // Check if the guess is correct import java.util.Scanner; if (guess == numberToGuess) { public class Guessing { guessedCorrect = true; public static void main(String[] args) { System.out.println("Congratulations! You // The random number guessed the number!"); int numberToGuess = 7; } else if (attempts == 3) { // Get user input System.out.println("You made 3 incorrect Scanner scanner = new Scanner(System.in); attempts: GAME OVER, the number is: int guess; "+numberToGuess); int attempts = 0; break;} // Start the game loop else if (guess < numberToGuess) { boolean guessedCorrect = false; System.out.println("Your guess is too do { low. Try again."); System.out.print("Guess a number between } else { 1 and 20: "); System.out.println("Your guess is too guess = scanner.nextInt(); high. Try again."); attempts++; } } while (guessedCorrect == false); // Close the scanner scanner.close(); }} 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++) { System.out.println(number + " * " + i + " = " + (number * i)); } } } Traffic light signal exercice using Switch-case
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); System.out.println("Enter traffic light signal (r, y, or g): "); char signal = scanner.next().charAt(0); // Get first character switch (signal) { case 'r': System.out.println("Stop!"); break; case 'y': System.out.println("Caution!"); break; case 'g': System.out.println("Go!"); break; default: System.out.println("Invalid signal!");} scanner.close();}