DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

🧠 Programming Session Day 7: Mastering Loops, Conditions & Database Logic in Java

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
}
Enter fullscreen mode Exit fullscreen mode

🟒 Output:

6
7
5
1
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ % 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);
Enter fullscreen mode Exit fullscreen mode

🟒 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;
}
Enter fullscreen mode Exit fullscreen mode

⚠️ 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;
}
Enter fullscreen mode Exit fullscreen mode

🟒 Output:

6
7
5
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Skips the final 1.


βœ… Case 3: % 100 Instead of % 10

int num = 1576;
while (num > 0) {
    System.out.println(num % 100);
    num = num / 10;
}
Enter fullscreen mode Exit fullscreen mode

🟒 Output:

76
7
0
0
Enter fullscreen mode Exit fullscreen mode

βœ… Case 4: num = num / 100;

int num = 1576;
while (num > 0) {
    System.out.println(num % 10);
    num = num / 100;
}
Enter fullscreen mode Exit fullscreen mode

🟒 Output:

6
5
Enter fullscreen mode Exit fullscreen mode

βœ… Case 5: % 100 and / 100

int num = 1576;
while (num > 0) {
    System.out.println(num % 100);
    num = num / 100;
}
Enter fullscreen mode Exit fullscreen mode

🟒 Output:

76
15
Enter fullscreen mode Exit fullscreen mode

✨ 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");
}
Enter fullscreen mode Exit fullscreen mode

❓ 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");
}
Enter fullscreen mode Exit fullscreen mode

🟒 Output:

It's Tuesday!
Enter fullscreen mode Exit fullscreen mode

❌ 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");
}
Enter fullscreen mode Exit fullscreen mode

🟒 Output:

Monday
Tuesday
Sunday
Enter fullscreen mode Exit fullscreen mode

β›” 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");
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ 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");
}
Enter fullscreen mode Exit fullscreen mode

🟒 Output:

Option 2
Option 3
Enter fullscreen mode Exit fullscreen mode

βœ… 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")
Enter fullscreen mode Exit fullscreen mode

βœ… 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)