+91 81900 98289

Java If...Else

if runs the next block only when the condition is true. else runs when that condition is false. else if checks another condition after an earlier one failed.

The condition must be a boolean. You cannot write if (marks) when marks is an int. Write if (marks >= 50). Use braces even for a one-line body so a later edit cannot attach the wrong statement.

Example

72 is greater than or equal to 50, so the first branch prints Pass. The else block is skipped.

public class Result { public static void main(String[] args) { int marks = 72; if (marks >= 50) { System.out.println("Pass"); } else { System.out.println("Retry"); } } }
A common bug is if (marks = 50), which assigns 50 and does not compile for an int condition. Comparison is ==. Assignment is =.