Data Analytics
27K subscribers
1.16K photos
24 videos
26 files
977 links
Dive into the world of Data Analytics – uncover insights, explore trends, and master data-driven decision making.
Download Telegram
# 📚 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
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