DEVTOME.COM HOSTING COSTS HAVE BEGUN TO EXCEED 115$ MONTHLY. THE ADMINISTRATION IS NO LONGER ABLE TO HANDLE THE COST WITHOUT ASSISTANCE DUE TO THE RISING COST. THIS HAS BEEN OCCURRING FOR ALMOST A YEAR, BUT WE HAVE BEEN HANDLING IT FROM OUR OWN POCKETS. HOWEVER, WITH LITERALLY NO DONATIONS FOR THE PAST 2+ YEARS IT HAS DEPLETED THE BUDGET IN SHORT ORDER WITH THE INCREASE IN ACTIVITY ON THE SITE IN THE PAST 6 MONTHS. OUR CPU USAGE HAS BECOME TOO HIGH TO REMAIN ON A REASONABLE COSTING PLAN THAT WE COULD MAINTAIN. IF YOU WOULD LIKE TO SUPPORT THE DEVTOME PROJECT AND KEEP THE SITE UP/ALIVE PLEASE DONATE (EVEN IF ITS A SATOSHI) TO OUR DEVCOIN 1M4PCuMXvpWX6LHPkBEf3LJ2z1boZv4EQa OR OUR BTC WALLET 16eqEcqfw4zHUh2znvMcmRzGVwCn7CJLxR TO ALLOW US TO AFFORD THE HOSTING.

THE DEVCOIN AND DEVTOME PROJECTS ARE BOTH VERY IMPORTANT TO THE COMMUNITY. PLEASE CONTRIBUTE TO ITS FURTHER SUCCESS FOR ANOTHER 5 OR MORE YEARS!

Java Selection Statements Part 2 If Else Statement

In case you didn’t catch part 1 (If Statement); selection statements are statements within programing languages that offer a choice. For example, If statements create a single selection; If Else creates two selections and the Switch statement creates multiple selections. Here, I will show how to create a simple If Else statement in the Java language.

As in part 1, 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 “SelectIfElse” . then you must have an open curly brace.

public class SelectIfElse {

The next line is called a method statement. The method statement allows the program to read and process the strings (characters or words) and arguments (statements, commands or comparisons) within the following curly braces. You need to place an opening curly brace following the statement.

public static void main(String[] args){

Next you will need to declare the variables. In this case I will be comparing two integers input by the user. So, I will declare x and y as integers or whole numbers positive or negative. Do not forget to add the semicolon at the end of your statements.

int x,y;

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

Scanner scan = new Scanner(System.in);

This is where you would give any information or instructions to the user. So, I need to have the program print my information and directions on the screen for the user. This program takes the input of two numbers from the user and shows which number is largest.

System.out.println(“Enter a number”);

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).

x=scan.nextInt();

We need more input from the user; we need another number to compare to.

System.out.println(“enter another number”);

This time the input from the keyboard (scan) will be used as the integer (y).

y=scan.nextInt();

Next we will use a simple If statement to compare the first number with the second number to see if x is greater than y. Don’t forget the open curly brace.

if (x > y){

So, if x is greater than y then print the input for x is larger than the input for y.

System.out.println(x + ” is larger than ” + y);

You need to close your curly brace for the If statement.

}

Now the else statement shows what you want to do if the “If” statement is false. Don’t forget the open curly brace.

Else{

If x is not greater than y then print that the input for y is larger than the input for x.

System.out.println(y + ” is larger than ” + x);

Once again, you need to close your curly brace for the If statement.

}

Now I will make sure all open curly braces have closing curly braces to match. I have the If statement, the Else statement, (already closed), public static void method statement and the public class statement with open curly braces. So I need to close two.

}

}

I hope this tutorial has been helpful. Good luck.

ifelse.jpg

You can view my other articles at sharkness

Category: Programming


QR Code
QR Code java_selection_statements_part_2_if_statement (generated for current page)
 

Advertise with Anonymous Ads