0% found this document useful (0 votes)
11 views

Programming

The document discusses different loop statements in Java including switch-case, do-while, and while-do. It provides examples of how each works. Specifically, it notes that switch-case allows branching based on an expression's value, do-while checks the condition after the loop body executes (ensuring it runs at least once), and while-do checks the condition before the body (so the body may not execute at all if false initially). It then provides code examples using these statements to check for even/odd numbers, perform math operations based on an operator, and print numbers from 1 to 10 using a do-while loop.

Uploaded by

Jean Catalan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming

The document discusses different loop statements in Java including switch-case, do-while, and while-do. It provides examples of how each works. Specifically, it notes that switch-case allows branching based on an expression's value, do-while checks the condition after the loop body executes (ensuring it runs at least once), and while-do checks the condition before the body (so the body may not execute at all if false initially). It then provides code examples using these statements to check for even/odd numbers, perform math operations based on an operator, and print numbers from 1 to 10 using a do-while loop.

Uploaded by

Jean Catalan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Please explain how the syntax works on the following statements:

Switch Case
- It is a multi-way branch statement. It provides an easy way to dispatch execution to different parts
of code based on the value of an expression.
- The expression in a switch statement must be of type char, byte, short, int, or String.
Do - While Statement
- It is similar to the while loop, but the condition is tested after the execution of the loop’s body. This
means the loop body will execute at least once.
- The condition is a boolean expression. If it evaluates to true, the loop will continue to execute. If it
evaluates to false, the loop will terminate.
While - Do Statement
- The while loop repeatedly executes a block of statements as long as a particular condition is true.
- Similar to the do-while loop, the condition is a boolean expression. The difference is that in a
while loop, the condition is tested before the execution of the loop’s body. If the condition is false
at the start, the loop body will not execute at all.

2. Answer the following problem using the appropriate statement:


a. Using switch statements, write a java program that displays if "the number is an odd number" or "the
number is an even number" based on the user inputted number.

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
int num = scanner.nextInt();

switch (num % 2) {
case 0:
System.out.println("The number is an even number");
break;
case 1:
case -1:
System.out.println("The number is an odd number");
break;
}
}
}
b. Using a switch statement, write a Java program that will perform the operation for the two entered
integers based on the chosen operator of the user.

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first number:");
int num1 = scanner.nextInt();
System.out.println("Enter the second number:");
int num2 = scanner.nextInt();
System.out.println("Enter an operator (+, -, *, /):");
char operator = scanner.next().charAt(0);

switch (operator) {
case '+':
System.out.println("The result is: " + (num1 + num2));
break;
case '-':
System.out.println("The result is: " + (num1 - num2));
break;
case '*':
System.out.println("The result is: " + (num1 * num2));
break;
case '/':
if (num2 != 0) {
System.out.println("The result is: " + (num1 / (double) num2));
} else {
System.out.println("Error! Dividing by zero is not allowed.");
}
break;
default:
System.out.println("Error! Invalid operator. Please enter correct operator.");
break;
}
}
}

c. Write a program to print all natural numbers from 1 to 10 using while do and do while loop.

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}

You might also like