1.
Control Structures Overview
Control structures allow you to dictate the flow of execution in a program. Java provides
various control structures to help make decisions, iterate through data, and manage
conditions.
2. Decision-Making: if, else, and switch
if Statement
The if statement allows a block of code to be executed only if a specific condition is true.
Syntax:
if (condition) {
// code to be executed if condition is true
Example:
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
if-else Statement
The if-else statement executes one block of code if the condition is true, and another block
if it is false.
Syntax:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Example:
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
else-if Ladder
You can chain multiple conditions using the else-if ladder.
Syntax:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if both conditions are false
Example:
int marks = 85;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 80) {
System.out.println("Grade B");
} else if (marks >= 70) {
System.out.println("Grade C");
} else {
System.out.println("Grade D");
}
switch Statement
The switch statement tests a variable against a set of values (called cases) and executes the
block of code corresponding to the matching case.
Syntax:
switch (variable) {
case value1:
// code block for case 1
break;
case value2:
// code block for case 2
break;
default:
// code block if no cases match
Example:
int day = 2;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}
3. Loops in Java
Loops are used to repeat a block of code multiple times.
for Loop
The for loop is used when the number of iterations is known beforehand.
Syntax:
for (initialization; condition; update) {
// code to be executed
Example:
for (int i = 1; i <= 5; i++) {
System.out.println(i); // Outputs 1 2 3 4 5
Explanation:
initialization: It sets a starting point (e.g., int i = 1).
condition: The loop continues as long as this condition is true (e.g., i <= 5).
update: It changes the variable after each iteration (e.g., i++).
while Loop
The while loop repeats the block of code as long as the condition is true. It is used when the
number of iterations is not known beforehand.
Syntax:
while (condition) {
// code to be executed
Example:
int i = 1;
while (i <= 5) {
System.out.println(i); // Outputs 1 2 3 4 5
i++;
do-while Loop
The do-while loop is similar to the while loop, but it guarantees that the code block will be
executed at least once, even if the condition is false.
Syntax:
do {
// code to be executed
} while (condition);
Example:
int i = 1;
do {
System.out.println(i); // Outputs 1 2 3 4 5
i++;
} while (i <= 5);
4. Break and Continue Statements
break Statement
The break statement is used to exit a loop or switch statement immediately, even if the
loop's condition has not been met.
Example:
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break; // Exit the loop when i equals 6
}
System.out.println(i); // Outputs 1 2 3 4 5
continue Statement
The continue statement is used to skip the current iteration of a loop and move on to the
next iteration.
Example:
for (int i = 1; i <= 10; i++) {
if (i == 6) {
continue; // Skip the rest of the loop when i equals 6
System.out.println(i); // Outputs 1 2 3 4 5 7 8 9 10
5. Command-Line Arguments in Java
Java allows you to pass arguments to your program through the command line. These
arguments are stored in the String[] args parameter of the main method.
Example:
public class CommandLineExample {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
} else {
System.out.println("No arguments provided.");
}
To run the program with an argument, use the following command:
java CommandLineExample Hello
This will print: First argument: Hello
6. Practice Problems (Day 3–4)
Problem 1: Even or Odd Number
Write a program to check if a number is even or odd using an if-else statement.
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
Problem 2: Simple Calculator Using switch
Write a simple calculator program using a switch statement.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
System.out.println("Choose operation: +, -, *, /");
char operator = sc.next().charAt(0);
switch (operator) {
case '+':
System.out.println("Result: " + (num1 + num2));
break;
case '-':
System.out.println("Result: " + (num1 - num2));
break;
case '*':
System.out.println("Result: " + (num1 * num2));
break;
case '/':
if (num2 != 0) {
System.out.println("Result: " + (num1 / num2));
} else {
System.out.println("Cannot divide by zero.");
break;
default:
System.out.println("Invalid operator.");
}
Problem 3: Factorial of a Number Using while Loop
Write a program to calculate the factorial of a number using a while loop.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int factorial = 1;
int i = 1;
while (i <= num) {
factorial *= i;
i++;
System.out.println("Factorial of " + num + " is " + factorial);
Day 3–4 Goals
Understand the decision-making structures (if, else, switch) in Java.
Familiarize yourself with loops (for, while, do-while).
Practice using command-line arguments.
Solve small problems to solidify your understanding of control structures and loops.