Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry Facebook connect
  • Androidberry twitter connect
  • Androidberry Google Plus connect

Java Tutorials


Basic Introduction

What is Java?
Java Features
Java vs Other Languages
Java on Internet
Java Components
Java First Program
Java Commandline argument
Java Constants
Java Variables
Java Types of Variables
Java Datatypes
Java Getting Input from User
Java Typecasting
Java Operators
Java Math Functions

Decision Making

Java if statement
Java if-else statement
Java nested if-else
Java else-if ladder
Java Switch case statement
Java Nested Switch Case
Java Ternary/Conditional operator

Looping

Java while loop
Java do-while loop
Java for loop
Java Enhanced loop/for each loop
Java Labelled for loop
Java Nesting of loops
Java Break and Continue

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Interfaces

Packages

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Decision Making - if Statement


Decision Making If statement

Decision making

Normally, the program is executed sequentially. In some situation in the programming the flow is not applicable to execute sequentially. In that case we need repeat some statements until certain condition is encountered.

Branching

When a program does not follow the sequential flow or the program breaks the sequential flow and jumps to another part of code, then it is called as branching.

Types of branching

1. Conditional :

if the branching proceeds as per the occurrence of condition or decision then it is known as conditional branching

2. Unconditional :

when branching is not based on a particular condition or decision then it is known as conditional branching.

Types of Unconditional branching

java if statement unconditional branching

If statement

If statement is a simplest decision making statement. It is used to control the flow of execution of statements.

Syntax

if ( test - expression ) 
{ 
    // if block statements are executed when test-expression becomes true
    // write whatever you want
}

Flowchart of if statement

If Statement Flowchart

How if statement works?

  • he compiler first evaluates the test expression then; control is transferred to the particular statement as the result of test expression.
  • If test expression result is true, then executes the body of if statement otherwise it skips the body and goes to next statement.

Example : ATM login system for displaying balance of user

In below example we are taking input from user throw commandline argument as ATM PIN and checking the available balance

class IfStatement
{
    public static void main(String args[])
    {
      int PIN;
      int balance = 15000;
      PIN = Integer.parseInt(arg[0]);
		
      if (PIN == 1234)
      {
	System.out.println("Welcome user");
	System.out.println("Your balance is "+ balance);
      }
      System.out.println("\nThank you for login");
    }
}
Input : Assume we are passing 1234

Output

Welcome user
Your balance is 15000

Thank you for login

In above example we have taken input from user and condition is written as PIN == 1234.
The relational operator = = is used to compare two different values. If both values are equal then if block statement is executed.

But the statements outside the if block are by default executed.Thats why 'thank you for login is get printed'.


Types of If statement

There are 3 different types of if statement based on their need.

  1. if-else statement
  2. Nested if-else statement
  3. else-if ladder

For Practice
  • You can write any number of if statements for checking different conditions.
  • You can also check boolean value instead of writting condition.
    Example - if (true)
Best Examples for Practice
  • Check Number is Even or Odd program
  • Check number is Positive or Negative program
  • Find largest number from two/three numbers
<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo