if / else statements

The two programs below show the syntax forthe if else statement. If there is just one statment to execute, you can omit the curly braces. However, many programmers always put in the curly braces, even if there is only one statement to execute. The if block is optional

1
2
3
4
5
6
7
8
9
10
11
package grades;
import java.util.*;
public class PassFail {
	public static void main(String[] args) {
		Scanner keyboard=new Scanner(System.in); //do just once
		System.out.print("Enter a grade:");
		int grade=keyboard.nextInt();
		if(grade>=65) System.out.println("That is passing!");
		else System.out.println("That is failing");
	}//main
}//class
CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package grades;
import java.util.*;
public class PassFail {
	public static void main(String[] args) {
		Scanner keyboard=new Scanner(System.in); //do just once
		System.out.print("Enter a grade:");
		int grade=keyboard.nextInt();
		if(grade>=65) {
			System.out.println("That is passing!");
		}//if
		else {
			System.out.println("That is failing");
		}//else
	}//main
}//class
CODE

Sample output:

Enter a grade:64
That is failing

Enter a grade:80
That is passing!

NEXT: