Java Selection Statements 1: If Statement

Selection statements are statements within programing languages that offer a choice. For example, If statements offer a single selection; If Else offers two selections and the Switch statement offers multiple selections. I will show how to create a simple If statement in the Java language.

If you are going to use input from the user’s keyboard, you must first import Scanner from java utility package.

import java.util.Scanner;

And of course the public class which is basically the name of the package which in this case is “SelectIf” . then you must have an open curly brace.

public class SelectIf {

The next line is tough to explain without getting too technical. It is called a method statement. I’ll just say it allows the program to read and process the strings (characters or words) and arguments (statements, commands or comparisons) within the following curly braces. And yes, you have to have an opening curly brace following the statement.

public static void main(String[] args){

Next you will need to declare the variables. In this case, since it is such a short program, it will be just one variable and I’ll call it x and declare it as an integer or whole number positive or negative. Notice most everything written ends with a “;” (semicolon) which indicates the end of an expression.

int x;

Now we have to declare the Scanner we imported. I will simply call it “scan” and tell that it is a new input from the scanner, or keyboard.

Scanner scan = new Scanner(;

In this example I want to give the user information and direction. So, I need to have the program print my information and directions on the screen for the user. This program just tells the user at what temperature in Fahrenheit that water boils and asks that the user enter a temperature. Notice each line is a separate expression. I could have combined the two into one expression with formatting and the user would see no difference.

System.out.println(“Water boils at 212 degrees Fahrenheit”);

System.out.println(“Enter a temperature in Fahrenheit”);

Now I will need to make the program understand the input from the keyboard. The program already knows that x is supposed to be an integer and scan is the keyboard. So I need to show that the input from the keyboard (scan) will be used as the integer (x).


Now after all of that we still haven’t created the “If” statement yet. I will do that now. This just preforms a simple comparison to see if x is greater than or equal to 212 degrees (the boiling point for water). Notice the If() there is another curly brace. Everything to do with the If statement will be in those braces.

if (x >= 212){

If x is in fact greater than or equal to 212 then I want it to let the user know that the water is boiling.

System.out.println(x + “ Your water is boiling ”);

Okay now I will make sure all open curly braces that are open have closing curly braces to match. I have the If statement, the public static void method statement and the public class statement with open curly braces.




That is about all there is to the simple If statement.

I hope I have made some of these steps easier to understand for the novice Java programmer.


You can view my other articles at sharkness

Category: Programming

QR Code
QR Code java_selection_statements_part_1_if_statement (generated for current page)