Day-33: Conditional Statements in java
Beginner: Repeated Reading: Base Twist: DON'T SAY I DON'T KNOW ALWAYS SAY LET ME TRY DON'T THINK ABOUT ENTIRE OUTPUT THINK ABOUT VERY NEXT STEP KNOWN TO UNKNOWN WRITE DOWN THE KNOWN STEPS THINK ABOUT VARIABLE, ADD IF IT IS NECESSARY MICRO TO MACRO 1). Normal Statements 2). Conditional Statements 3). Control Flow Statements Good Bad Ugly Gangers if GBU --> else if Gangers --> if statement should definitely have else if part - True else if statement should definitely have else part - True if it rains, take umbrella else else if part should definitely have if part - True else part should definitely have else if part - False else part should definitely have if part - True else part should definitely have either if or else if or both - True if(no1>no2) else if(no1>no2) else if(no2>no1) else else part is mandatory / Compulsory always - False How many else if block can one if block have? How many else block can one if block have? else block should be added ONLY at the end of all if blocks True / False if(sslc_1>sslc_2) --> if sslc_1 is greater than sslc_2 1). Basic if Condition (Check Even/Odd): public class EvenOddCheck { public static void main(String[] args) { int number = 10; if (number % 2 == 0) { System.out.println(number + " is even."); } else { System.out.println(number + " is odd."); } } } 2). if-else if Ladder (Grade Calculator) public class GradeCalculator { public static void main(String[] args) { int marks = 85; if (marks >= 90) { System.out.println("Grade: A"); } else if (marks >= 80) { System.out.println("Grade: B"); } else if (marks >= 70) { System.out.println("Grade: C"); } else { System.out.println("Grade: D"); } } } 3). Nested if (Login System) public class LoginSystem { public static void main(String[] args) { String username = "admin"; String password = "12345"; if (username.equals("admin")) { // Outer if if (password.equals("12345")) { // Nested if System.out.println("Login successful!"); } else { System.out.println("Wrong password."); } } else { System.out.println("Invalid username."); } } } Note: Uses String.equals() for comparison (not ==). Key Takeaways: 1). Basic if-else: Single condition check. 2). if-else if: Multiple conditions (evaluated top-down). 3). Nested if: Condition inside another condition. Common Pitfalls to Avoid: Using = (assignment) instead of == (comparison). Forgetting braces {} for multi-line blocks. Comparing strings with == (use .equals() instead). Re-call the previous statements: Objects will call Constructors by default. Child Object behaves as Parent Object is known as Inheritance. Child Object = Parent Object If both the statements are correct, My Question is: Should Child Object call Constructor in Parent Class? Yes / No Consider I have many constructors in Parent Class. In that case, which constructor will be called by Child Object? Usually which constructor will be called when we create Objects? [Consider we have multiple constructors] Constructor which has matching Arguments will be called. new SuperMarket(10, "Rice", 5); -------------------------- End of Blog ------------------------------

Beginner: Repeated Reading: Base Twist:
DON'T SAY I DON'T KNOW
ALWAYS SAY LET ME TRY
DON'T THINK ABOUT ENTIRE OUTPUT
THINK ABOUT VERY NEXT STEP
KNOWN TO UNKNOWN
WRITE DOWN THE KNOWN STEPS
THINK ABOUT VARIABLE, ADD IF IT IS NECESSARY
MICRO TO MACRO
1). Normal Statements
2). Conditional Statements
3). Control Flow Statements
Good Bad Ugly
Gangers
if GBU -->
else if Gangers -->
if statement should definitely have else if part - True
else if statement should definitely have else part - True
if it rains, take umbrella
else
else if part should definitely have if part - True
else part should definitely have else if part - False
else part should definitely have if part - True
else part should definitely have either if or else if or both - True
if(no1>no2)
else
if(no1>no2)
else if(no2>no1)
else
else part is mandatory / Compulsory always - False
How many else if block can one if block have?
How many else block can one if block have?
else block should be added ONLY at the end of all if blocks True / False
if(sslc_1>sslc_2) --> if sslc_1 is greater than sslc_2
1). Basic if Condition (Check Even/Odd):
public class EvenOddCheck {
public static void main(String[] args) {
int number = 10;
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
2). if-else if Ladder (Grade Calculator)
public class GradeCalculator {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
} else if (marks >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: D");
}
}
}
3). Nested if (Login System)
public class LoginSystem {
public static void main(String[] args) {
String username = "admin";
String password = "12345";
if (username.equals("admin")) { // Outer if
if (password.equals("12345")) { // Nested if
System.out.println("Login successful!");
} else {
System.out.println("Wrong password.");
}
} else {
System.out.println("Invalid username.");
}
}
}
Note: Uses String.equals() for comparison (not ==).
Key Takeaways:
1). Basic if-else: Single condition check.
2). if-else if: Multiple conditions (evaluated top-down).
3). Nested if: Condition inside another condition.
Common Pitfalls to Avoid:
Using = (assignment) instead of == (comparison).
Forgetting braces {} for multi-line blocks.
Comparing strings with == (use .equals() instead).
Re-call the previous statements:
Objects will call Constructors by default.
Child Object behaves as Parent Object is known as Inheritance.
Child Object = Parent Object
If both the statements are correct, My Question is:
Should Child Object call Constructor in Parent Class? Yes / No
Consider I have many constructors in Parent Class. In that case, which constructor will be called by Child Object?
Usually which constructor will be called when we create Objects? [Consider we have multiple constructors]
Constructor which has matching Arguments will be called.
new SuperMarket(10, "Rice", 5);
-------------------------- End of Blog ------------------------------