Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Control Statements in java IF, ELSE and ELSE IF Tutorial

Control Statements in java

Study and learn Interview MCQ Questions and Answers on IF , ELSE , ELSE IF Control Statements in java . Attend job interviews easily with these Multiple Choice Questions

Control Statements in java

All control statements take the boolean value as an INPUT and branches control accordingly. Usually, the true condition takes control to one branch of code and the false condition takes control to another branch. The only exception to this is a SWITCH statement which can accept integers like byte, short, int and enum, character type data like char and String.

Control Statements

Type of Control Statements in java

Java language has three types of Control Statements.

  1. Selection Statements
  2. Loop or Iteration Statements
  3. Jump Statements

1. Selection Control Statements in java

A Selection control statement takes a condition (boolean value or relational expression) as input and branches to one of the two possible branching point statements.

If the condition is true, One set of instructions or statements are executed and if the condition is false, another set of instructions are executed.

There are two types of Selection Control Statements.

  1. IF statement (IF ELSE statements)
  2. SWITCH statement

2. Loop Control Statements in java

Loop control statements execute a particular set of instructions repeatedly as long as the Loop Condition is satisfied i.e true condition.

There are four types of Loop Control Statements

  1. WHILE
  2. DO WHILE
  3. FOR
  4. FOR EACH or Advanced FOR or Enhanced FOR

3. Jump Control Statements in java

Jump control statements take the execution control by skipping a number of statements in between. Unlike other control statements, which go in a predictable linear way of execution, Jump control statements work in a non-linear nested way by skipping and jumping a number of statements.

There are three Jump Control statements.

  1. BREAK
  2. CONTINUE
  3. RETURN

Java IF, ELSE and ELSE IF control statements

Java language continues the same features of IF ELSE statements introduced by the C Language. Programmers heavily depend of IF ELSE statements to create good java products with efficiency. Remember that you can put any number of white spaces like TAB, New Line and Space between two tokens or keywords of a Java program. You can write all IF statements of a program in just one line though it is not reading friendly.

1. IF Control Statement

IF statement takes a condition as an input. This condition should evaluate to a boolean type constant like true or false. If you do not use Braces, only one statement or Single statement can be specified inside an if statement. With the use of Braces, you can add any number of statements inside a Single IF statement.

If the condition evaluates to true, statements below IF will be executed. Otherwise, program control goes to the next statement after the IF block.

Syntax:

if( CONDITION )
   //STATEMENT;

(or)

if( CONDITION )
{
   Statement1;
   Statement2;
   ...
}

Example: IF statement

The second statement is not part of IF(a<10). Use braces to add if required.

class IFStatement
{
  public static void main(String args[])
  {
    int a=11;
    if(a<10)
	  System.out.println(a + "<10");
	  System.out.println("AFRICA");
	
    int b=8;
    if(b<10)
    {	
	  System.out.println(b + "<10");
	  System.out.println("POLAND");
    }
  }
}
//OUTPUT
//AFRICA
//8<10
//POLAND

2. IF ELSE Control Statement

ELSE statement is a companion to the IF statement. You can not use ELSE without using the IF statement. ELSE block is executed only if IF condition fails or it is false.

Syntax:

if( CONDITION )
   //STATEMENT1;
else
   //STATEMENT2
(or)

if( CONDITION )
{
   Statement1;
   Statement2;
   ...
}
else
{
   Statement8;
   Statement9;
   ...
}

Example: IF ELSE statement with String comparison

“str” is compared with “str2”. Both the String variables hold the same value “ELEPHANT”. So, IF condition evaluates to true and the corresponding statements are executed.

class IF_ELSE_Statement
{
  public static void main(String args[])
  {
    String str="ELEPHANT";
    String str2= "ELEPHANT";
    if(str == str2)
    {
	  System.out.println("IF Condition");
    }
    else
    {
	  System.out.println("ELSE Condition");
    }

    if(str != str2)
    { System.out.println("IF Condition"); }
    else
    { System.out.println("ELSE Condition"); }
  }
}
//OUTPUT
//IF Condition
//ELSE Condition

3. IF ELSE IF Control Statement

IF ELSE-IF ladder is useful to execute a different set of statements for different conditions. We can use as many ELSE-IF statements as we want. Do note that the ELSE statement or block at the end of IF-ELSE-IF is not mandatory or it is optional.

If any of IF or ELSE IF condition matches control goes inside that IF or ELSE IF and then exits without going inside ELSE block at the last.

“float” is promoted to type “double” in this example.

Example:

public class IF_ELSE_IF
{	
  public static void main(String[] args)
  {
    float a = 1.5f;
    float b = 2.5f;
    if(a > 2)
    {
      System.out.println("A>2");	
    }
    else if(b>2)
    {
	  System.out.println("B>2");
    }
    else if(a>1)
    {
 	  System.out.println("A>1");
    }
    else
    {
	  System.out.println("UNKNOWN");
    }
  }
}
//OUTPUT
//B>2

4. NESTED IF, ELSE and ELSE-IF Control Statements

You can nest an IF inside another IF. and You can nest an IF ELSE inside another IF. You can nest IF ELSE inside another ELSE or ELSE-IF and so on. Carefully use braces to demarcate one block from another block. You can add as many conditions as you want. Some programmers write multiple conditions on a single line with the help of relational operators and a single IF.

Example:

public class NESTED_IF
{	
  public static void main(String[] args)
  {
    float a = 1.5f;
    float b = 2.5f;
    if(a > 1)
    {
      if(b>=2)
      {
        System.out.println("A>1 AND B>2");	
      }
      else
      {
        System.out.println("A>1 AND B<2");
      }
    }
    else if(a>2)
    {
	  System.out.println("A>2");
    }
    else
    {
	  System.out.println("UNKNOWN");
    }
  }
}
//OUTPUT
//A>1 AND B>2


Control Statements in java online test

[WpProQuiz 152]

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming Tests
  • C programming Tests
  • C++ programming Tests
  • Aptitude Tests

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2022
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2022
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.