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.

Top comments (0)