Friday 23 June 2017


Decision making statement:

Decision making statement means a condition block need to be executed or not which is decided by condition. If the condition is "true" statement block will be executed, if condition is "false" then statement block will not be executed. In java there are three types of decision making statement.
  • if
  • if-else
  • switch
if-then is most basic statement of Decision making statement. It tells to program to execute a certain part of code only if particular condition is true.

Syntax:
if (condition) {
    Statement(s)
}

Example if statement,

public class Hello {
    int a = 10;
    public static void main(String[] args) {
        if (a < 15) {
            System.out.println("a is less than 15");
        }
    }
}

Output:
a is less than 15

if-else statement:

In general it can be used to execute one block of statement among two blocks, in java language if and else are the keyword in java.
Syntax:

if (condition) {
    Statement(s)
} else {
    Statement(s)
}........

In the above syntax whenever condition is true all the if block statement are executed, remaining statement of the program by neglecting. If the condition is false else block statement executed and neglecting if block statements.
Example if else,


import java.util.Scanner;
public class GreaterOrLess {
    public static void main(String[] args) {
        int no = 10;
        
        if (no > 15) {
            System.out.println("Number is greatger than 15");
        } else {
            System.out.println("Number is less than 15");
        }
    }
}

Output:

Number is less than 15

Switch Statement:

The switch statement in java language is used to execute the code from multiple conditions or case. It is same like if else-if ladder statement.

A switch statement work with byte, short, char and int primitive data type, it also works with enumerated types and string.

Syntax:

switch(expression/variable)
{
 case  value:
 //statements
 // any number of case statements
 break;  //optional
 default: //optional
 //statements
}

Rules for apply switch statement:

With switch statement use only byte, short, int, char data type (float data type is not allowed). You can use any number of case statements within a switch. Value for a case must be same as the variable in switch.

Limitations of switch statement:

Logical operators cannot be used with switch statement. For instance, k>=20: // not allowed

Example of switch case,


import java.util.*;
class switchCase {
    public static void main(String arg[]) {
        int ch;
        System.out.println("Enter any number (1 to 7) :");
        Scanner s = new Scanner(System.in);
        ch = s.nextInt();
        switch (ch) {
            case 1:
                System.out.println("Today is Monday");
                break;
            case 2:
                System.out.println("Today is Tuesday");
                break;
            case 3:
                System.out.println("Today is Wednesday");
                break;
            case 4:
                System.out.println("Today is Thursday");
                break;
            case 5:
                System.out.println("Today is Friday");
                break;
            case 6:
                System.out.println("Today is Saturday");
                break;
            case 7:
                System.out.println("Today is Sunday");
            default:
                System.out.println("Only enter value 1 to 7");
        }
    }
}

Output

Enter any number(1 to 7): 5 Today is Friday

Loop Control statements: 

Loops enable your program to conditionally execute particular blocks of code multiple times.

Looping statements are the statements execute one or more statements repeatedly several number of times. In java programming language there are three types of loops; while, for and do-while.


Why use loop?
When you need to execute a block of code several number of times then you need to use looping concept. 

In java programming language there are three types of loops; 
  • for 
  • while 
  • do-while
for loop:

for loop is a statement which allows code to be repeatedly executed. For loop contains 3 parts Initialization, Condition and Increment or Decrements

Syntax:

for (initialization; condition; increment) {
    statement(s);
}

Initialization: This step is executed first and executed only once when we are entering into the loop first time. This step will allow to declare and initialize any loop control variables.

Condition: This is next step after initialization step, if it is true, the body of the loop is executed, if it is false then the body of the loop does not execute and flow of control goes outside of the for loop.

Increment or Decrements: After completion of Initialization and Condition steps loop body code is executed and then Increment or Decrements steps is execute. This statement allows to update any loop control variables.
  • First initialize the variable
  • In second step check condition
  • In third step control goes inside loop body and execute.
  • At last increase the value of variable
  • Same process is repeat until condition not false.

Example of for loop:

Display any message exactly 5 times.
class Hello {
    public static void main(String args[]) {
        int i;
        for (i = 0: i < 5; i++) {
            System.out.println("Hello Friends !");
        }
    }
}

Output:
Hello Friends!Hello Friends!Hello Friends!Hello Friends!Hello Friends!


For-each loop (Advanced or Enhanced For loop):

The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.

Syntax of for-each loop:


for(data_type variable : array | collection){}

Simple Example of for-each loop for traversing the array elements:

public class ForEachArraysExample{  
  public static void main(String args[]){  
   int arr[]={1, 2, 3, 4, 5};  
  
   for(int i:arr){  
     System.out.println(i);  
   }  
  
 }   
}
Output:
1
2
3
4
5

Simple Example of for-each loop for traversing the ArrayList elements:

import java.util.*;  
public class ForEachListExample{  
  public static void main(String args[]){  
   List<String> list=new ArrayList<>();  
   list.add("foo");  
   list.add("blah");  
   list.add("wow");  
  
   for(String s:list){  
     System.out.println(s);  
   }  
  }   
} 

Output:
foo
blah
wow 


While loop:

In while loop first check the condition if condition is true then control goes inside the loop body otherwise goes outside of the body. while loop will be repeats in clock wise direction.

Syntax:

while (condition) {
    Statement(s) Increment / decrements(++or--);
}

Example while loop,

class whileDemo {
    public static void main(String args[]) {
        int i = 0;
         while (i < 5) {
            System.out.println(+i);
            i++;
         }
    }
}

Output:
1 2 3 4 5

do-while:

A do-while loop is similar to a while loop, except that a do-while loop is execute at least one time.

A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given condition at the end of the block (in while).

When use do..while loop?
when we need to repeat the statement block at least one time then use do-while loop. In do-while loop post-checking process will be occur, that is after execution of the statement block condition part will be executed.

Syntax:

do {
    Statement(s) increment / decrement(++or--)
} while ();

In below example you can see in this program i=20 and we check condition i is less than 10, that means condition is false but do..while loop execute onec and print Hello world ! at one time.

Example do..while loop,

class dowhileDemo {
    public static void main(String args[]) {
        int i = 20;
        do {
            System.out.println("Hello world !");
            i++;
        } while (i < 10);
    }
}

Output: 
Hello world !


Another example of do..while loop where condition is satisfied,


class dowhileDemo {
    public static void main(String args[]) {
        int i = 0;
        do {
            System.out.println(+i);
            i++;
        } while (i < 5);
    }
}

Output:
    1 2 3 4 5



No comments:

Post a Comment