Home Java Learn-java-in-a-day Java Control Statements
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java Control Statements

Advertisement
In this section, we are going to discuss the control statements. Different types of control statements: the decision making statements (if-then, if-then-else and switch), looping statements (while, do-while and for) and branching statements (break, contin

Java Control Statements

     

In this section, we are going to discuss the control statements. Different types of control statements: the decision making statements (if-then, if-then-else and switch), looping statements (while, do-while and for) and branching statements (break, continue and return).

Control Statements  
The control statement are used to controll the flow of execution of the program . This execution order depends on the supplied data values and the conditional logic. Java contains the following types of control statements:

1- Selection Statements
2- Repetition Statements
3- Branching Statements 

Selection statements:

  1. If Statement:
    This is a control statement to execute a single statement or a block of code, when the given condition is true and if it is false then it skips if block and rest code of program is executed .

      Syntax:
     
    if(conditional_expression){
      <statements>;
      ...;
      ...;
    }

    Example:
    If n%2 evaluates to 0 then the "if" block is executed. Here it evaluates to 0 so if block is executed. Hence "This is even number" is printed on the screen.
    int n = 10;

    if(n%2 = = 0){

       System.out.println("This is even number");

    }


  2. If-else Statement:
    The "if-else" statement is an extension of if statement that provides another option when 'if' statement evaluates  to "false" i.e. else block is executed if "if" statement is false.  

      Syntax:
      
    if(conditional_expression){
      <statements>;
      ...;
      ...;
      }
       else{
      <statements>;
      ....;
      ....;
       } 

    Example:
    If n%2 doesn't evaluate to 0 then else block is executed. Here n%2 evaluates to 1 that is not equal to 0 so else block is executed. So "This is not even number" is printed on the screen.

    int n = 11;

    if(n%2 = = 0){

       System.out.println("This is even number");

    }

    else{

       System.out.println("This is not even number");

    }


  3. Switch Statement:
    This is an easier implementation to the if-else statements. The keyword "switch" is  followed by an expression that should evaluates to byte, short, char or int primitive data types ,only. In a switch block there can be one or more labeled cases. The expression that creates labels for the case must be unique. The switch expression is matched with each case label. Only the matched case is executed ,if no case matches then the default statement (if present) is executed.

    Syntax:
     
    switch(control_expression){
      case expression 1:
      <statement>;
      case expression 2:
      <statement>;
       ...
       ...
      case expression n:
      <statement>;
      default:
      <statement>;
      }//end switch

    Example: Here expression "day" in switch statement evaluates to 5 which matches with a case labeled "5" so code in case 5 is executed that results to output "Friday" on the screen.

    int day = 5;

    switch (day) {
       case 1:
      System.out.println("Monday");
      break;
       case 2:
       System.out.println("Tuesday");
       break;
      case 3:
      System.out.println("Wednesday");
      break;
      case 4:
      System.out.println("Thrusday");
      break;
      case 5:
       System.out.println("Friday");
       break;
      case 6:
       System.out.println("Saturday");
       break;
      case 7:
      System.out.println("Sunday");
      break;
      default:
       System.out.println("Invalid entry");
       break;
    }

Repetition Statements:

  1. while loop statements:
    This is a looping or repeating statement. It executes a block of code or statements till the given condition is true. The expression must be evaluated to a boolean value. It continues testing the condition and executes the block of code. When the expression results to false control comes out of loop.

    Syntax:
      
    while(expression){
      <statement>;
      ...;
      ...;
      }

    Example: Here expression i<=10 is the condition which is checked before entering into the loop statements. When i is greater than value 10 control comes out of loop and next statement is executed. So here i contains value "1" which is less than number "10" so control goes inside of the loop and prints current value of i and increments value of i. Now again control comes back to the loop and condition is checked. This procedure continues until i becomes greater than value "10". So this loop prints values 1 to 10 on the screen.

    int i = 1;

    //print 1 to 10

    while (i <= 10){

      System.out.println("Num " + i);

      i++;

    }


  2. do-while loop statements:
    This is another looping statement that tests the given condition past so you can say that the do-while looping statement is a past-test loop statement. First the do block statements are executed then the condition given in while statement is checked. So in this case, even the condition is false in the first attempt, do block of code is executed at least once.

    Syntax:
     
    do{
      <statement>;
      ...;
      ...;
      }while (expression);

    Example: Here first do block of code is executed and current value "1" is printed then the condition i<=10 is checked. Here "1" is less than number "10" so the control comes back to do block. This process continues till value of i becomes greater than 10.

    int i = 1;

    do{

      System.out.println("Num: " + i);

       i++;

    }while(i <= 10);

     

  3. for loop statements:
    This is also a loop statement that provides a compact way to iterate over a range of values. From a  user point of view, this is reliable because it executes the statements within this block repeatedly till the specified conditions is true .

    Syntax:
     
       for (initialization; condition; increment or decrement){
      <statement>;
      ...;
      ...;
      }
    initialization: The loop is started  with the value specified.
    condition: It evaluates to either 'true' or 'false'. If it is false then the loop is terminated. 
    increment or decrement: After each iteration, value increments or decrements. 

    Example: Here num is initialized to value "1", condition is checked whether num<=10. If it is so then control goes into the loop and current value of num is printed. Now num is incremented and checked again whether num<=10.If it is so then again it enters into the loop. This process continues till num>10. It prints values 1 to10 on the screen.
    for (int num = 1; num <= 10; num++){

      System.out.println("Num: " + num);

    }

Branching Statements:

  1. Break statements: 
    The break statement is a branching statement that contains two forms: labeled and unlabeled. The break statement is used for breaking the execution of a loop (while, do-while and for) . It also terminates the switch statements.

    Syntax:
     
    break;  // breaks the innermost loop or switch statement.
      break label;   // breaks the outermost loop in a series of nested loops.

    Example: When if statement evaluates to true it prints "data is found" and  comes out of the loop and executes the statements just following the loop.


  2. Continue statements:
    This is a branching statement that are used in the looping statements (while, do-while and for) to skip the  current iteration of the loop and  resume the next iteration .

    Syntax:
     
    continue;

    Example:


  3. Return statements:
    It is a special branching statement that  transfers the control to the caller of the method. This statement is used to return a value to the caller method and terminates execution of method. This has two forms: one that returns a value and the other that can not return. the returned value type must match the return type of  method.

    Syntax:
     
    return;
      return values;

    return;   //This returns nothing. So this can be used when method is declared with void return type.
    return expression;   //It returns the value evaluated from the expression.

    Example: Here Welcome() function is called within println() function which returns a String value "Welcome to roseIndia.net". This is printed to the screen.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 25, 2008

Ask Questions?    Discuss: Java Control Statements   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Raj
April 21, 2011
java

what purpose java
raju
April 23, 2011
java

java is a pure object oriented language.
Rishi Sharma
June 11, 2011
Awesome Notes i found here

Hi!!! FIrst, would like to thanks for the publisher to posting this great notes of (Java Control Statement). Actually, I was preparing for my B.SC (IT)_5th sem and your these notes assist me a lot..... Well, thanks once again for publishing this... regards, Rishi
Elango
November 17, 2011
java

good.....
Sandhya
January 31, 2012
tanks

Java Control Statements with given Examples tanks for that
Mahesh
February 8, 2012
Java

Very good meteraial for d begginers..
praveen
January 19, 2013
i wana know is java easy learning language

i dont know much about java i had been to basic java course now i can make out wat the program is now a days but still i feel wat is java n wat kind of job will i get on java programming lang is it possible for me to fit into this java programing field from where do i start to learn java n how do i start my self can any one give me few suggestion on my email id ie praveen.gkh@gmail.com
priya
July 16, 2012
java

its very easy.....
ANKIT CHAUHAN
August 16, 2012
java control statement

It is a very good web service provided to all the students who are learning java!
ankit chauhan
August 16, 2012
java control statement

paste some more examples with proper indication of statements and their results.....
riyaz
August 30, 2012
@

it's ok not bed adding the advance example means high level........
kamlakar singh
October 14, 2012
about controls in java

my dream is softwareengineer
Jaffa
August 13, 2013
Java

It is not good ....Imprecise......
rama suresh
November 22, 2012
java

First of all thanks.actually i am studying 2nd year b.s.c computers.so i am prepairing for my final exams.so i was known lot of matter about java from the internet.so once again thanks to you.
DMCA.com