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_db
for student data -
staff_db
for faculty info -
fees_db
for payment records -
attendance_db
for 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) -
enum
types
β 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
/default
mistakes - Pythonβs switch alternative
Top comments (0)