πŸš€ Day 26 of My Automation Journey – Conditions, Inputs & Do-While Loop

Today’s session was a mix of core Java fundamentals + tricky logic + real-world usage πŸ’‘
We explored how conditions work, how loops behave without braces, user input handling, and even touched JSON/XML basics.

Let’s break it down step by step πŸ‘‡

πŸ”Ή 1. Condition Must Be Boolean (True/False)

In Java, every condition must return either true or false.

❌ Invalid Example

int i = 1;
while(i) {   // ❌ Compilation Error
    System.out.println(i);
}

πŸ‘‰ Why error?

  • Java does NOT allow non-boolean conditions
  • Unlike C/C++, numbers are not treated as true/false

βœ… Correct Example

int i = 1;
while(i <= 5) {
    System.out.println(i);
    i++;
}

βœ” Condition β†’ i <= 5 β†’ returns boolean
βœ” Hence valid

πŸ”Ή 2. True / False Direct Conditions

You can directly use true or false in conditions.

βœ… Example

if(true) {
    System.out.println("Always runs");
}

βœ” Output:

Always runs

❌ Example

if(false) {
    System.out.println("Never runs");
}

βœ” Output:

(no output)

πŸ‘‰ This is useful for testing or debugging

πŸ”Ή 3. Removing Curly Braces { }

Java allows skipping { } if only one statement is present.

βœ… Example

for(int i = 1; i <= 5; i++)
    System.out.println(i);

βœ” Output:

1 2 3 4 5

⚠️ Tricky Case

for(int i = 1; i <= 5; i++)
    System.out.println(i);
    System.out.println("Done");

βœ” Output:

1 2 3 4 5
Done

πŸ‘‰ Only the first statement is inside loop
πŸ‘‰ Second statement runs once after loop

πŸ”Ή 4. Infinite Loop (By Default True)

If condition is always true β†’ loop never stops 😱

Example

for(;;) {
    System.out.println("Infinite Loop");
}

πŸ‘‰ This is equivalent to:

while(true) {
    System.out.println("Infinite Loop");
}

βœ” Used in:

  • Servers
  • Continuous monitoring systems

πŸ”Ή 5. break and continue

πŸ”Έ break β†’ stops loop completely

for(int i = 1; i <= 5; i++) {
    if(i == 3)
        break;
    System.out.println(i);
}

βœ” Output:

1
2

πŸ”Έ continue β†’ skips current iteration

for(int i = 1; i <= 5; i++) {
    if(i == 3)
        continue;
    System.out.println(i);
}

βœ” Output:

1
2
4
5

πŸ”Ή 6. Taking Input from User (Scanner)

To read input in Java, we use Scanner.

Example

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = sc.nextLine();

        System.out.println("Hello " + name);
    }
}

⚠️ Important Notes

❌ Java does NOT directly support char input
βœ” Workaround:

char ch = sc.next().charAt(0);

βœ” For String:

String name = sc.nextLine();

πŸ”Ή 7. Multiple Inputs Example

Scanner sc = new Scanner(System.in);

System.out.print("Enter Tamil mark: ");
int tamil = sc.nextInt();

System.out.print("Enter English mark: ");
int english = sc.nextInt();

int total = tamil + english;

System.out.println("Total mark: " + total);

βœ” Output Example:

Enter Tamil mark: 80
Enter English mark: 90
Total mark: 170

πŸ”Ή 8. JSON vs XML (Basic Understanding)

πŸ”Έ JSON (JavaScript Object Notation)

{
  "username": "Manikandan",
  "password": "1234"
}

βœ” Lightweight
βœ” Easy to read
βœ” Mostly used in APIs

πŸ”Έ XML (Extensible Markup Language)

<user>
    <username>Manikandan</username>
    <password>1234</password>
</user>

βœ” Structured format
βœ” Used in older systems / configs

πŸ”Ή 9. Do-While Loop

πŸ‘‰ Runs at least once, even if condition is false

Example

int i = 1;

do {
    System.out.println(i);
    i++;
} while(i <= 5);

βœ” Output:

1 2 3 4 5

πŸ”₯ Key Difference

Loop Type Condition Check
while Before execution
do-while After execution

🧠 Key Takeaways

βœ” Condition must always be boolean
βœ” Java does NOT allow non-boolean conditions
βœ” Without {}, only one statement is considered
βœ” Infinite loops happen when condition is always true
βœ” break β†’ exits loop
βœ” continue β†’ skips iteration
βœ” Scanner is used for user input
βœ” JSON is lightweight, XML is structured
βœ” do-while runs at least once

🏁 Conclusion

Today was all about understanding how Java actually behaves under the hood 🧠

These small concepts are very important when:

  • Writing automation scripts
  • Handling test data
  • Building logic-driven frameworks

πŸ”₯ Step by step, building strong fundamentals in automation!

πŸ€– A Small Note
I used ChatGPT to help structure and refine this blog while keeping the concepts aligned with my learning.