Kamis, 28 April 2016

Control statements in java with examples

  • Programs in java will be executed in sequential manner means line by line execution of statements.
  • By using control statements we can control the flow of execution.
  • We have three types of control statements in java.
  1. Decision making Statements.
  2. Iteration statements.
  3. Branching statements. 




1.Decision making statements in java: 

  • In some cases we will have a situation like need to execution set of statements based on a condition
  • In this scenario we need to use decision making statements in java.
  1. if
  2. if-else
  3. if-else-if
  4. switch

Simple if

  • Based on some condition if we want to execute some set of statements then we need to use if condition in java.
  • We need to keep all the required statements inside if block .
  • If the condition is true then control executes the whole block otherwise it skips the if block of statements.

  1. if( condition ){
  2. // code
  3. }

Java example program on if control statement:

  1. package controlstatementsinjava;
  2. import java.util.Scanner;
  3.  
  4. public class ControlStatementsInjava {
  5.  
  6.     /**
  7.      * @website: www.instanceofjava.com
  8.      */
  9. public static void main(String[] args) {
  10.         
  11.   Scanner in= new Scanner(System.in);
  12.  
  13.   System.out.println("Please enter a number");
  14.   int n=in.nextInt();
  15.         
  16. if(n%2==0){
  17.    System.out.println(n+" is even number");
  18.         
  19.  }
  20.   
  21. if(n%2!=0){
  22.      System.out.println(n+" is odd number");
  23. }
  24.  
  25. }
  26. }

Output:

  1. Please enter a number
  2. 5
  3. 5 is odd number

If else condition:

  • If the condition is true then control executes the whole block otherwise it skips the if block of statements
  • if condition is false  if we need to execute another set of statement we can write another if and keep all the statements in that block.
  • instead of checking multiple times we can simply use else block .
  • More about in else statement in java

    1. if( condition ){
    2. // code
    3. }else{
    4. //code
    5. }

     if else control statement with example programs:

    1. package controlstatementsinjava;
    2. import java.util.Scanner;
    3.  
    4. public class ControlStatementsInjava {
    5.  
    6.     /**
    7.      * @website: www.instanceofjava.com
    8.      */
    9. public static void main(String[] args) {
    10.         
    11.   Scanner in= new Scanner(System.in);
    12.  
    13.   System.out.println("Please enter a number");
    14.   int n=in.nextInt();
    15.         
    16. if(n%2==0){
    17.    System.out.println(n+" is even number");
    18.         
    19.  }else{
    20.      System.out.println(n+" is odd number");
    21. }

    22.  
    23. }
    24. }

    Output:

    1. Please enter a number
    2. 5
    3. 5 is odd number


    if else ladder :

    • In some cases we may get requirement with multiple conditions in such cases we need to use if else ladder.(if -else-if-else if -else...)

    1. if( condition ){
    2. // code
    3. }else if{
    4. //code
    5. }else {
    6.  //code
    7. }

     if else if control statement with example programs:


    1. package controlstatementsinjava;
    2. import java.util.Scanner;
    3.  
    4. public class ControlStatementsInjava {
    5.  
    6.     /**
    7.      * @website: www.instanceofjava.com
    8.      */
    9. public static void main(String[] args) {
    10.         
    11.   Scanner in= new Scanner(System.in);
    12.  
    13.   System.out.println("Please enter your age");
    14.   int age=in.nextInt();
    15.         
    16. if(age<=18){
    17.    System.out.println("Your age is between 1-18");
    18.         
    19.  }else if ((age>=18) && (age<=35)){
    20.      System.out.println(" Your age is between 18-35");
    21. }
    22. else if ((age>=35) && (age<=50)){
    23.      System.out.println(" Your age is between 35-50");
    24. }else{
    25.  
    26.   System.out.println(" Your age is above 50");
    27.  
    28. }
    29.  
    30. }
    31. }

    Output:

    1. Please enter your age
    2. 20
    3. Your age is between 18-35

    control statements in java

    Switch statement in java:

    • Instead if writing number of if conditions we can use single switch statement


    1. switch(value)
    2.  case n1: statements;
    3. break;
    4. case n2 : statements;
    5. break;
    6. default: statements;
    7. }

    Java example program on switch statement in java:


    switch statement in java

    Bagikan

    Jangan lewatkan

    Control statements in java with examples
    4/ 5
    Oleh

    Subscribe via email

    Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.