Scroll to top

Java Statements tutorial for Beginners

Here we will learn about statements in Java.

Types of Java Statement

  • Expression Statement
  • Control Statement
  • Assignment Statement

 In this section, we will discuss control statements. 


Control Statements

  • Conditional execution
  • Looping
  • Flow Control Statement

Types of Conditional Execution

  • If Statement
  • If – Else statement
  • If- Else-if statement
  • Switch statement

if Statement

The “if” statement in Java encloses a portion of code which is executed only if the applied condition is true. if statement only accept the boolean expression as a condition.

Unlike other languages Java does not accept numbers as conditional operators. It only considers boolean expressions as conditions that return TRUE or FALSE.

The syntax of If Statement

if (true)
   System.out.println("Hello! This will always get printed");
if (Boolean Expression) {
    Code block to get executed
}

Example code of If Statement

public class if_condition {
    public static void main(String[] args) {
        String string = "Hello";
        if ("Hello".equals(string)) {
            System.out.println("String has the Value Hello");
        }

        if ("Hi".equalsIgnoreCase(string)) {
            System.out.println("String has the value Hi");
        }
    }
}

If you run above code it will print “String has the Value Hello”.


If Else Statement

The if Else statement consists of one if condition and one else statement. It encloses a portion of code which is executed only if the if a condition is true if it is false then the else part of the code will be executed. If else statements take only boolean expression as valid conditions.

Unlike other languages, java does not accept numbers as conditional operators. It only considers boolean expressions as conditions that return TRUE or FALSE.
E.g if (1==1) is accepted as it is a boolean expression and will return true. if(x=1) statement is not allowed in Java.

Syntax of If  Else Statement

if (1==2)
	System.out.println("Hello! This will not get printed");
else
	System.out.println("Hello! This will get printed");
if (Boolean Expression) {
    Code block to get executed
}
else{
code block to get executed when above condition is false
}

Example code of If Else Statement

public class if_condition {
    public static void main(String[] args) {
        String string = "Hello";
        if ("Hello".equals(string)) {
            System.out.println("String has the Value Hello");
        } else {
            System.out.println("String is not Hi");
        }

        if ("HI".equals(string)) {
            System.out.println("String has the Value Hi");
        } else {
            System.out.println("String is not Hi");
        }
    }
}

When you run above code, it will print..

String has the Value Hello
String is not Hi

If Else If Statement

The if else if statement consists of multiple if conditions and an else statement. If the if condition is true then the enclosed code will be executed. If the if condition is false then it will check the next if condition. If the next if condition is true then the enclosed code will be executed otherwise the else part of the code will be executed. If Else If statements take only boolean expressions as a valid condition.

Unlike other languages java doesn’t accept numbers as conditional operators. It only considers boolean expressions as conditions that return TRUE or FALSE.
E.g If(x=1) is not allowed while if(1==1) is accepted as it is a boolean expression and will return true.

Syntax of If  Else If Statement

if (1==2)
	System.out.println("Hello! This will not get printed");
else if(1==1)
	System.out.println("Hello! This will get printed");
else
	System.out.println("This will get executed when Above conditio is FALSE");
if (Boolean Expression) {
    Code block to get executed
}
else{
code block to get executed when above condition is false
}

Example Code of If Else If Statement in java

public class if_condition {
    public static void main(String[] args) {
        String string = "Hello";

        if ("HI".equals(string)) {
            System.out.println("String has the Value Hi");
        } else if ("Hello".equals(string)) {
            System.out.println("String is not Hi");
        }

        string = "NotHello";
        if ("Hello".equals(string)) {
            System.out.println("String has the Value Hello");
        } else if ("Hi".equalsIgnoreCase(string)) {
            System.out.println("String is not Hi");
        } else {
            System.out.println("String is neither Hello nor Hi");
        }
    }
}

When you run above code, it will print..

String is not Hi
String is neither Hello nor Hi

Types of Looping Statement

  • For Loop
  • While Loop
  • Do  – While Loop

Types of Flow Control Statement

  • Return Statement
  • Continue Statement
  • Break Statement
Author avatar
JBT
https://javabeginnerstutorial.com/

18 comments

  1. venja

    Where is the explanation for Conditional Execution-Switch Case and others..

  2. anusha

    hello sir…………..i need small examples for statements.

  3. plz give the explanation for the fallowing last concepts

    ya its gud gor learners

  4. swetha

    Hi sir,

    please give me explation on Looping statements

  5. shubham

    Please tell me something about assignment statement.. bcuz there is no any link ……….
    plzzzzz

  6. aditya

    please give the explanation about break , continue and return as these are so important ………..

    • Ramneet Singh Dua

      break: is used to stop execution of a loop
      continue: is used when we want the code execution irrespective of the condition
      return: is used to explicitly return from a method

  7. Vinny Oisebe

    Thz is a nice readig zone for those who need to be a an expert in programming n thx

  8. Thanks for developing this site from which I can able to learn the concepts easily.

    Could you please provide the explanation for flow control statement and assignment statement with some examples for better understanding?

  9. Upendra joshi

    Awesome sir
    Easy ways given for learning core java

  10. Ramesh

    hi sir
    please give mi explanation on jvm concepts.

  11. gurjeet

    plz tell us about set visible command

  12. sai pavan

    sir i think you have missed assignment statements..

  13. Vittal

    Plz provide with examples…that will be more helpful………….Tx..

  14. mani

    very nice explaintion

  15. vikramadity........

    very nice explaintion……………….but i am not understand…..bsuz no problem……i am indian airforce….not depended…understood…yaaa qki mere dosto ko smj me aa gaya kafi hain…………..

  16. Papa

    Good,but we need more examples

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.