Day 1: Introduction to Java

icon picker
Day 4: Control Statements in Java

Welcome back to our Java programming journey! Today, we'll be diving into control statements. Control statements in Java allow us to control the flow of our program, making it more dynamic and responsive. We will cover if-else statements, switch statements, various types of loops, and introduce the concepts of break and continue.

If-Else Statements

One of the most fundamental control structures in programming is the if-else statement. It allows us to execute different blocks of code based on certain conditions. Here's a basic example:
javaCopy code
public class IfElseDemo {
public static void main(String[] args) {
int number = 10;

if (number > 0) {
System.out.println("Number is positive.");
} else {
System.out.println("Number is non-positive.");
}
}
}

In this example, if number is greater than 0, it prints "Number is positive." Otherwise, it prints "Number is non-positive."

Switch Statements

Switch statements are used when you have multiple conditions to check against a single expression. It's often used for menu-driven applications or when you have a limited set of options. Here's a simple switch statement:
javaCopy code
public class SwitchDemo {
public static void main(String[] args) {
int dayOfWeek = 3;

switch (dayOfWeek) {
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("Invalid day");
}
}
}

In this example, the program prints "Wednesday" because dayOfWeek is set to 3.

Loops

Loops are essential for repetitive tasks. Java provides three main types of loops: while, for, and do-while.

While Loop

javaCopy code
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}

For Loop

javaCopy code
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}

Do-While Loop

javaCopy code
int count = 1;
do {
System.out.println("Count: " + count);
count++;
} while (count <= 5);

All three loops will produce the same output:
makefileCopy code
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Break and Continue

The break statement is used to exit a loop prematurely, while the continue statement is used to skip the current iteration and move to the next. Here's an example of break:
javaCopy code
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
System.out.println("Value of i: " + i);
}

The program will print values from 1 to 4 and then exit the loop when i becomes 5.

Exercise Questions

Write a Java program to check if a given number is even or odd using if-else statements.
Create a Java program that takes a day of the week (1-7) as input and prints the corresponding day name using a switch statement.
Write a Java program to calculate the factorial of a given number using a for loop.
Implement a Java program to find the sum of all even numbers from 1 to 100 using a while loop.
Create a Java program that prints the first 10 Fibonacci numbers using a do-while loop.

Exercise Answers

javaCopy code
public class EvenOrOdd {
public static void main(String[] args) {
int number = 7;

if (number % 2 == 0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}
}
}

javaCopy code
public class DayOfWeek {
public static void main(String[] args) {
int dayOfWeek = 3;

switch (dayOfWeek) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}

javaCopy code
public class Factorial {
public static void main(String[] args) {
int number = 5;
int factorial = 1;

for (int i = 1; i <= number; i++) {
factorial *= i;
}

System.out.println("Factorial of " + number + " is " + factorial);
}
}

javaCopy code
public class SumOfEvenNumbers {
public static void main(String[] args) {
int sum = 0;

for (int i = 2; i <= 100; i += 2) {
sum += i;
}

System.out.println("Sum of even numbers from 1 to 100: " + sum);
}
}

javaCopy code
public class Fibonacci {
public static void main(String[] args) {
int n = 10;
int first = 0;
int second = 1;
int count = 0;

do {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
count++;
} while (count < n);
}
}

That's it for Day 4 of our Java journey! Control statements are fundamental building blocks of any programming language, and mastering them will greatly enhance your ability to write efficient and responsive Java programs. Don't forget to try out the exercise questions to solidify your understanding. See you on Day 5!
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.