Java Program: Using If-Else Statement in Java Programming

Java Program: Using If-Else Statement in Java Programming
New Unlocked Blackberry Storm 2 II 9550 Touchscreen GSM Smart Phone
Current Bid: $93.99

If and Else Statement in Java with Sample Java Source Code

If and else statement in Java programming means if(condition here meets){implement your code here} else{implement this code instead}. Hence the syntax for if and else statement in Java programming is this:

if(condition)

{

Write your code here

}

else

{

Write your code here

}

Usually the if and else statement is used if the program has a unique condition to be done and the other condition is exactly the opposite of that unique condition. The else statement is just another gateway for the program to execute something if it does not meet the unique condition it needs on the if statement.

Java Books

Java How to Program by Harvey M. Deitel and Paul J. Deitel (2002, CD-ROM /…
Current Bid: $10.00
FAST SHIP – DEITEL 9e Java : How to Program B75
Current Bid: $28.76
Java How to Program: Early Objects 9E by Harvey Deitel 9th (Int’l Edition)
Current Bid: $55.90

For example, we will write a program about determining whether the user is a teenager or not. So we will have an algorithm that says, if the user is age 13-19, the program will say that she/he is a teenager. Else if the user is less than or greater than the specified ages, the program will say, you are not a teenager. Just note that if the user will enter he/she is 12 the program will say “you are not a teenager”, the output is similar also if the user enters that he/she is 20. See the Java Free Source code below:

Package name: teenager_identifier

package teenager_identifier;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(“Enter your age: “);
int user_age = input.nextInt();
if(user_age >= 13 && user_age <= 19) { System.out.println("You are a Teenager."); } else { System.out.println("You are not a Teenager."); } } }

Sample Output:

Simple isn’t it? 🙂 Happy Programming!

Related Java Tutorials:

Complete List of Java Tutorial and Source Codes for Absolute Beginners
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming

Cool Touch Screens!

LG Optimus Logic L35G cheap smart phone black silver 3.0 prepaid
Current Bid: $17.50
Cheap New 4″ Resistive Touch screen Unlocked Quad band Dual Sim T mobile Phone
Current Bid: $41.99
4.0″ NEW Cheap Android 4.0 Dual Sim WIFI Capacitive Smart phone AT&T T-Mobile
Current Bid: $63.95

Java Source Codes Samples:

Class in Java: Learn More about Java Classes with Sample Java Codes
Java Program: How to Use If Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming
Java Program: How to Sort Numbers in Ascending Order
Java Program: How to Sort Numbers in an Array in a Descending Order
What is MDAS? Sample Java Source Code for MDAS
Fibonacci Java Program: Complete Program for Fibonacci Series
Java Program: Palindrome Test Java Source Code
Java Program: Reverse String in Java Using For Loop
Fibonacci Java Program: Complete Program for Fibonacci Series
Java Program: Count the Number of String Characters in Java
Count the Number of Vowels in Java String
Java Program: How to Parse a String into Integer in Java Programming
Sample Java Program for String Tokenizer
Java Program: How to Use Switch Statement in Java