Lec_5.
Lec_5.
Control statements
• Control statements in Java are used to control the
flow of execution based on certain conditions. These
statements can alter the execution sequence, make
decisions, repeat blocks of code, or break out of
loops. The main types of control statements in Java
are:
• 1. Decision-Making Statements
• 2. Looping Statements
Decision-Making Statements
• Syntax –
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if no cases match
}
Example –
public class Main {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Other Day");
}
}
}
2. Looping Statements
• 1. for Loop
• The for loop is used when the number of
iterations is known beforehand.
• Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute
}
Example –
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
}
}
• 2 - while Loop
• The while loop executes the block of code as
long as the condition is true.
• Syntax:
while (condition) {
// Code to execute
}
• Example –
public class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}
}
}
• 3 - do-while Loop
• The do-while loop executes the block of code
at least once, even if the condition is false.
• Syntax:
do {
// Code to execute
} while (condition);
• Example –
public class Main {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Count: " + i);
i++;
} while (i <= 5);
}
}
• break Statement
• The break statement is used to exit a loop prematurely.
• Example:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop
}
System.out.println("Number: " + i);
}
}
}
• continue Statement
• The continue statement skips the current iteration and
moves to the next.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip this iteration
}
System.out.println("Number: " + i);
}
}
}
• Enhanced for Loop (for-each)
• The enhanced for loop is used to iterate over
arrays or collections.
• Syntax:
• for (type variable : collection/array) {
• // Code to execute
• }
• Example-
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int number : numbers) {
System.out.println(number);
}
}
}
• Practice Questions –
• Grading System:
Write a Java program to accept marks of a student and display their grade based on
the following criteria:
• Marks >= 90: Grade A
• Marks >= 75 and < 90: Grade B
• Marks >= 50 and < 75: Grade C
• Marks < 50: Fail
• Leap Year Check:
Write a program to check if a given year is a leap year. (Hint: A year is a leap year if it
is divisible by 4, but not divisible by 100, except when it is also divisible by 400.)
• Odd or Even:
Write a program to check whether a given number is odd or even using conditional
statements.
• Number Classification:
Accept a number from the user and check whether it is positive, negative, or zero.
• Triangle Validity:
Write a program to check if three given sides can form a valid triangle. (Hint: The
sum of any two sides of a triangle must be greater than the third side.)
• Questions Based on Loops
• Prime Number Check:
Write a program to check if a given number is prime. (Hint: A number is prime if it is
divisible only by 1 and itself.)
• Factorial Calculation:
Write a program to calculate the factorial of a number using a for loop.
• Multiplication Table:
Write a program to generate and display the multiplication table of a number entered by
the user.
• Fibonacci Series:
Write a program to print the first n terms of the Fibonacci series, where n is entered by
the user.
• Number Pattern:
Write a program to generate the following number pattern using nested loops for n
rows:
1
12
123
1234
12345
• Write a program to check whether a given number is a palindrome (e.g., 121, 1331 are
palindromes, but 123 is not).
public class Main {
public static void main(String[] args) {
int num = 121, reversed = 0, temp = num;
if (sum == num) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}
}
• Write a program to find the greatest common divisor (GCD) of two
numbers using loops.
public class Main {
public static void main(String[] args) {
int num1 = 56, num2 = 98, gcd = 1;
System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);
}
}
• Write a program to find all prime numbers between 1 and 100.
public class Main {
public static void main(String[] args) {
for (int num = 2; num <= 100; num++) {
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(num + " ");
}
}
}
}
• Print the following number pyramid for n = 5:
1
121
12321
1234321
123454321
public class Main {
public static void main(String[] args) {
int n = 5;
if (pizzaChoice == 5) {
System.out.println("Thank you for visiting Pizza Shop!");
break; // Exit the program
}
switch (pizzaChoice) {
case 1:
pizzaPrice = 8;
System.out.println("You selected Margherita Pizza.");
break;
case 2:
pizzaPrice = 10;
System.out.println("You selected Pepperoni Pizza.");
break;
case 3:
pizzaPrice = 9;
System.out.println("You selected Veggie Pizza.");
break;
case 4:
pizzaPrice = 12;
System.out.println("You selected BBQ Chicken Pizza.");
break;
default:
System.out.println("Invalid choice! Please select again.");
continue;
}
// Size Choice
System.out.println("\nSelect the size of your pizza:");
System.out.println("1. Small - $2");
System.out.println("2. Medium - $4");
System.out.println("3. Large - $6");
totalCost += toppingPrice;
}
// Final total
System.out.printf("\nYour total cost is: $%.2f\n", totalCost);