programming session:
🌐 Why Do We Connect Many Databases in a Server?
In real-world applications, one server connects multiple databases because:
✅ Different modules need different data.
✅ Easy to scale (add/remove DBs).
✅ Security: Finance DB ≠ Student DB.
✅ Performance: Less load per DB.
✅ Backup and Recovery made easier.
🎓 Example: A college server may have:
-
students_dbfor student data -
staff_dbfor faculty info -
fees_dbfor payment records -
attendance_dbfor daily logs- 📌 Example: Amazon may have separate DBs for Products, Orders, and Customers.
🔁 Reversing a Number using while
Let's take this number:
int num = 1576;
while (num > 0) {
System.out.println(num % 10); // prints last digit
num = num / 10; // removes last digit
}🟢 Output:
6
7
5
1👉 % 10 → gets the last digit
👉 / 10 → removes it
🔢 Counting Digits in a Number
int num = 1576;
int count = 0;
while (num > 0) {
count++;
num = num / 10;
}
System.out.println("Total Digits: " + count);🟢 Output: Total Digits: 4
🔁 Different Variations of While Loop
✅ Case 1: while (num >= 0)
int num = 1576;
while (num >= 0) {
System.out.println(num % 10);
num = num / 10;
}⚠️ Infinite Loop: num becomes 0 → %10 = 0, /10 = 0 → stuck.
✅ Case 2: while (num > 1)
int num = 1576;
while (num > 1) {
System.out.println(num % 10);
num = num / 10;
}🟢 Output:
6
7
5📌 Skips the final 1.
✅ Case 3: % 100 Instead of % 10
int num = 1576;
while (num > 0) {
System.out.println(num % 100);
num = num / 10;
}🟢 Output:
76
7
0
0
✅ Case 4: num = num / 100;
int num = 1576;
while (num > 0) {
System.out.println(num % 10);
num = num / 100;
}🟢 Output:
6
5
✅ Case 5: % 100 and / 100
int num = 1576;
while (num > 0) {
System.out.println(num % 100);
num = num / 100;
}🟢 Output:
76
15✨ Useful for date, time, or currency formats.
🔀 What is switch-case?
Switch case is a clean way to write multiple fixed value conditions.
✅ Syntax:
String day = "Monday";
switch (day) {
case "Monday":
System.out.println("Weekday");
break;
case "Sunday":
System.out.println("Weekend");
break;
default:
System.out.println("Invalid");
}
❓ Why and Where to Use switch?
- When one variable has many fixed values
- Example: Menu options, Days of week, Payment modes
Use switch when:
- You check one value against many known cases
- The values are fixed like
"Low","Medium","High"
Use if when:
- You check ranges, like
x > 10 - You use logical operators:
&&,||,!
🔥 Switch Case vs If Statement
| Feature | switch |
if-else |
|---|---|---|
| Use case | Fixed values | Complex logic, ranges |
| Data types |
int, char, String
|
All types |
| Readability | Very clean | Can be messy if long |
| Flexibility | Limited | Highly flexible |
🧐 Why Keywords like switch, case are Lowercase?
➡️ To separate language keywords from user-defined names.
➡️ Capital letters are reserved for classes like String, Scanner.
✅ switch, case, break, if, for = keywords → lowercase
✅ Main, String, ArrayList = classes → Capital case
It’s a syntax rule across C, Java, C#, JS…
📆 Example: Switch Case with Days
String day = "Tuesday";
switch (day) {
case "Monday":
System.out.println("Weekday");
break;
case "Tuesday":
System.out.println("It's Tuesday!");
break;
default:
System.out.println("Invalid day");
}🟢 Output:
It's Tuesday!
❌ What If We Remove default?
No crash.
But if no case matches, nothing prints.
❌ What If We Remove break?
String day = "Monday";
switch (day) {
case "Monday":
System.out.println("Monday");
case "Tuesday":
System.out.println("Tuesday");
case "Sunday":
System.out.println("Sunday");
}🟢 Output:
Monday
Tuesday
Sunday⛔ This is called fall-through. Once matched, it keeps printing below cases.
🔁 Can I Move default or break?
✅ default can be at the top, middle, or bottom.
🚫 But break should be placed after each case to stop flow.
❓ Is Only int Allowed in Switch?
No!
Java supports:
-
int,byte,short,char -
String(from Java 7) -
enumtypes
⛔ float, double, boolean are NOT allowed.
❓ Why Use default?
To handle unexpected input or invalid values.
Like:
default:
System.out.println("Oops! Not a valid day");
💥 Learn from Errors — Missing break
int option = 2;
switch (option) {
case 1:
System.out.println("Option 1");
case 2:
System.out.println("Option 2");
case 3:
System.out.println("Option 3");
}🟢 Output:
Option 2
Option 3✅ Without break, everything falls through!
🐍 Why No Switch Case in Python?[TBD]
Python loves simplicity and readability.
Instead of switch, it uses if-elif-else.
But from Python 3.10, you can now use match-case like switch:
day = "Tuesday"
match day:
case "Monday":
print("Weekday")
case "Sunday":
print("Weekend")
case _:
print("Invalid")✅ Final Thoughts
In this blog,i am learned:
- Why servers connect to multiple DBs
- Reversing numbers using
while - Switch vs If — where, why, and when
- Keyword rules in Java
- How to learn from
break/defaultmistakes - Python’s switch alternative