# 📚 Java Programming Language – Part 2/10: Operators & Control Flow
#Java #Programming #OOP #ControlFlow #Coding
Welcome to Part 2 of our Java series! Today we'll explore operators and control flow structures.
---
## 🔹 Java Operators Overview
Java provides various operators for:
- Arithmetic calculations
- Logical decisions
- Variable assignments
- Comparisons
### 1. Arithmetic Operators
### 2. Relational Operators
### 3. Logical Operators
### 4. Assignment Operators
---
## 🔹 Control Flow Statements
Control the execution flow of your program.
### 1. if-else Statements
### 2. Ternary Operator
### 3. switch-case Statement
### 4. Loops
#### while Loop
#### do-while Loop
#### for Loop
#### Enhanced for Loop (for-each)
---
## 🔹 Break and Continue
Control loop execution flow.
---
## 🔹 Practical Example: Number Guessing Game
---
### 📌 What's Next?
In Part 3, we'll cover:
➡️ Methods and Functions
➡️ Method Overloading
➡️ Recursion
#JavaProgramming #ControlFlow #LearnToCode🚀
#Java #Programming #OOP #ControlFlow #Coding
Welcome to Part 2 of our Java series! Today we'll explore operators and control flow structures.
---
## 🔹 Java Operators Overview
Java provides various operators for:
- Arithmetic calculations
- Logical decisions
- Variable assignments
- Comparisons
### 1. Arithmetic Operators
int a = 10, b = 3;
System.out.println(a + b); // 13 (Addition)
System.out.println(a - b); // 7 (Subtraction)
System.out.println(a * b); // 30 (Multiplication)
System.out.println(a / b); // 3 (Division - integer)
System.out.println(a % b); // 1 (Modulus)
System.out.println(a++); // 10 (Post-increment)
System.out.println(++a); // 12 (Pre-increment)
### 2. Relational Operators
System.out.println(a == b); // false (Equal to)
System.out.println(a != b); // true (Not equal)
System.out.println(a > b); // true (Greater than)
System.out.println(a < b); // false (Less than)
System.out.println(a >= b); // true (Greater or equal)
System.out.println(a <= b); // false (Less or equal)
### 3. Logical Operators
boolean x = true, y = false;
System.out.println(x && y); // false (AND)
System.out.println(x || y); // true (OR)
System.out.println(!x); // false (NOT)
### 4. Assignment Operators
int c = 5;
c += 3; // Equivalent to c = c + 3
c -= 2; // Equivalent to c = c - 2
c *= 4; // Equivalent to c = c * 4
c /= 2; // Equivalent to c = c / 2
---
## 🔹 Control Flow Statements
Control the execution flow of your program.
### 1. if-else Statements
int age = 18;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
### 2. Ternary Operator
String result = (age >= 18) ? "Adult" : "Minor";
System.out.println(result);
### 3. switch-case Statement
int day = 3;
switch(day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
// ... other cases
default:
System.out.println("Invalid day");
}
### 4. Loops
#### while Loop
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
#### do-while Loop
int j = 1;
do {
System.out.println(j);
j++;
} while (j <= 5);
#### for Loop
for (int k = 1; k <= 5; k++) {
System.out.println(k);
}#### Enhanced for Loop (for-each)
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}---
## 🔹 Break and Continue
Control loop execution flow.
// Break example
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit loop
}
System.out.println(i);
}
// Continue example
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println(i);
}
---
## 🔹 Practical Example: Number Guessing Game
import java.util.Scanner;
import java.util.Random;
public class GuessingGame {
public static void main(String[] args) {
Random rand = new Random();
int secretNumber = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess;
do {
System.out.print("Guess the number (1-100): ");
guess = scanner.nextInt();
if (guess < secretNumber) {
System.out.println("Too low!");
} else if (guess > secretNumber) {
System.out.println("Too high!");
}
} while (guess != secretNumber);
System.out.println("Congratulations! You guessed it!");
scanner.close();
}
}
---
### 📌 What's Next?
In Part 3, we'll cover:
➡️ Methods and Functions
➡️ Method Overloading
➡️ Recursion
#JavaProgramming #ControlFlow #LearnToCode
Please open Telegram to view this post
VIEW IN TELEGRAM
❤3
Data Analytics
# 📚 Java Programming Language – Part 9/10: Collections Framework #Java #Collections #DataStructures #Programming Welcome to Part 9 of our Java series! Today we'll explore the powerful Collections Framework - essential for handling groups of objects efficiently.…
### 4. Switch Expressions (Java 14)
---
## 🔹 Practical Example: Employee Processing
---
## 🔹 Best Practices
1. Prefer immutability with records and unmodifiable collections
2. Use Optional instead of null checks
3. Favor functional style with streams for data processing
4. Keep lambdas short and readable
5. Adopt modern features gradually in existing codebases
---
### 🎉 Congratulations!
You've completed our 10-part Java series! Here's what we covered:
1. Java Basics
2. Operators & Control Flow
3. Methods & Functions
4. OOP Concepts
5. Inheritance & Polymorphism
6. Interfaces & Abstract Classes
7. Packages & Access Modifiers
8. Exception Handling
9. Collections Framework
10. Streams & Modern Features
#JavaProgramming #CompleteCourse #ModernJava 🚀
What's next?
➡️ Build real projects
➡️ Explore frameworks (Spring, Jakarta EE)
➡️ Learn design patterns
➡️ Contribute to open source
Happy coding!👨💻 👩💻
String dayType = switch (day) {
case "Mon", "Tue", "Wed", "Thu", "Fri" -> "Weekday";
case "Sat", "Sun" -> "Weekend";
default -> throw new IllegalArgumentException();
};---
## 🔹 Practical Example: Employee Processing
public class Employee {
private String name;
private String department;
private double salary;
// Constructor, getters
}
List<Employee> employees = // ... initialized list
// Stream processing example
Map<String, Double> avgSalaryByDept = employees.stream()
.collect(Collectors.groupingBy(
Employee::getDepartment,
Collectors.averagingDouble(Employee::getSalary)
));
// Modern Java features
List<String> highEarners = employees.stream()
.filter(e -> e.salary() > 100000)
.map(Employee::name)
.sorted()
.toList(); // Java 16+ toList()
System.out.println(avgSalaryByDept);
System.out.println(highEarners);---
## 🔹 Best Practices
1. Prefer immutability with records and unmodifiable collections
2. Use Optional instead of null checks
3. Favor functional style with streams for data processing
4. Keep lambdas short and readable
5. Adopt modern features gradually in existing codebases
---
### 🎉 Congratulations!
You've completed our 10-part Java series! Here's what we covered:
1. Java Basics
2. Operators & Control Flow
3. Methods & Functions
4. OOP Concepts
5. Inheritance & Polymorphism
6. Interfaces & Abstract Classes
7. Packages & Access Modifiers
8. Exception Handling
9. Collections Framework
10. Streams & Modern Features
#JavaProgramming #CompleteCourse #ModernJava 🚀
What's next?
➡️ Build real projects
➡️ Explore frameworks (Spring, Jakarta EE)
➡️ Learn design patterns
➡️ Contribute to open source
Happy coding!
Please open Telegram to view this post
VIEW IN TELEGRAM