Java Statements tutorial for Beginners

Here we will learn about statements in Java.

Types of Java Statement

  • Control Statement
  • Assignment Statement

 In this section we will discuss about control statements. Click here for additional information on assignment statement.


 

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 statements only accept boolean expression as condition.

Note*:  Unlike other languages Java does not accept numbers as conditional operators. It only considers boolean expressions as conditions which returns TRUE or FALSE.

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


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

Note*:  Unlike other languages, Java does not accept numbers as conditional operators. It only considers boolean expressions as conditions which returns 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


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 takes only boolean expressions as  valid condition .

Note*:  Unlike other languages Java doesn’t accept numbers as conditional Operators. Only boolean expression which returns TRUE or FALSE.
E.g If(x=1) is not allowed while if(1==1) is accepted as it is 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


 

Types of Looping Statement


 

Types of Flow Control Statement

  • Return Statement
  • Continue Statement
  • Break Statement
Series Navigation<< Operators in java programming languageDifferent ways to create an object in Java >>
By | 2017-08-14T11:00:44+00:00 October 4th, 2012|Core Java|13 Comments

About the Author:

13 Comments

  1. venja November 25, 2013 at 4:39 pm - Reply

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

  2. anusha April 10, 2014 at 5:29 pm - Reply

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

  3. plz give the explanation for the fallowing last concepts June 16, 2014 at 10:44 am - Reply

    ya its gud gor learners

  4. swetha July 16, 2014 at 12:26 pm - Reply

    Hi sir,

    please give me explation on Looping statements

  5. shubham July 23, 2014 at 1:14 pm - Reply

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

  6. aditya August 19, 2014 at 6:46 am - Reply

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

    • Ramneet Singh Dua May 18, 2016 at 6:56 pm - Reply

      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 September 29, 2014 at 7:43 am - Reply

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

  8. surya February 2, 2015 at 12:29 pm - Reply

    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 July 11, 2015 at 2:31 am - Reply

    Awesome sir
    Easy ways given for learning core java

  10. Ramesh August 12, 2015 at 3:54 am - Reply

    hi sir
    please give mi explanation on jvm concepts.

  11. gurjeet November 19, 2016 at 1:29 pm - Reply

    plz tell us about set visible command

  12. Vittal March 7, 2017 at 4:48 pm - Reply

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

Leave A Comment