Monday, March 18, 2013

Java Control Structure & Looping Structure with Example

Java Control Structure & Looping structure explained with examples. 

* java if... else statement
* java switch case
* java while loop
* java for loop
* java do....while loop
* java break statement
* java continue statement.
 

Control flow Statement

All application development environment provide decision making process called control flow statements that directs the execution of application. It facilitates the programmer to examine the existing condition and decide suitable flow of action.

It controls the flow statements  that, repetition of loop upto condition is true and when condition is false  it jumps to next statement.

If-else statement

The if-else statement tests the result of a condition, and perform  appropriate action based on result.
The syntax of if-else statement is:
if(condition 1){
 action 1;
}else if(condition 2){
 action 2;
}else{
 action 3;
}

where condition is boolean expression, it returns true or false value;

Example :

class CheckNumber{

      public static void main(String args[]){

            int num = 10;

            if (num % 2 == 0){
                  System.out.println(num + “ is even number );
            } else {
                  System.out.println(num + “ is odd number );
            }
      }
}


switch-case statement

switch-case can be used as an alternative for if-else condition. If the programmer has to make number of decisions, and all the decisions depend on the value of variable, then switch-case is used instead of if-else condition.

Syntanx of switch-case:

switch(expression){
      case 1 :
      action1 statement;
      break;
      case 2:
      action2 statement;
      break;
      .    
      .    
      .
      case N:
      actionN statement;
      break;
      default:
      default statement;
}
where expression: is variable containing the value to be evaluated. It must be of type byte, short, int, char.
      default:  is an optional keyword used to specify the statements to be executed only when all the case statements evaluate to false.


Example :

int month = 1;
String name;
switch (month) {
      case 1:
            name = “January”;
            break;
      case 2:
            name=”February”;
            break;
      .
      .
      .
      default:
            name=”Invalid Month”;
}


While Loop

The while loop executes till condition is specified. It executes the steps mentioned in the loop only if the condition is true. This is useful where programmer doesn't know in advance that how many times the loop will be executed.

Syntanx of while loop:

while (condition) {
      action statements:
.     .
      .
      .
}
Where condition: is any boolean expression that returns a true or false value. The loop continues upto condition returns true value.

Example :

int i = 1;

while(i<=5){
      i++;
      System.out.println(“value of i is : “+i);
}

do-while loop

The do-while loops execute certain statements till the specified condition is true. This loop ensures that the loop body is executed atleast once.

Syntax of do-while loop:
do{
      action statements;
}while(condition){
      action statements;
      .
      .
}

Example :

int j = 8;
do{
      j = j+2;
      System.out.println(“value of j is : “ +j);               
}while(j>=10 && j<=50){
      j = j+5;
      System.out.println(“value of j is : “ +j);
}


for loop

All loops have some common feature: a counter variable that is initialized before the loop begins. The for loop provides the feature that, initialized the variable before loop begins, a condition for counter variable and counter upto which loop lasts.

Syntax of for loop:

for(initialization statements; condition; increment/decrement statements){
      action statements;
      .
      .
}

where initialization statements : sets or initialize the value for counter variable.
condition : A boolean expression that returns true or false value. The loop terminates if false value is returned.
Increment/decrement statements : Modifies the counter variable. This statements are always executed after the action statements, and before the subsequent condition check.

 Example :

for(int i=1;i<=10;i++){
      System.out.println(“Value of i is “ +i);
}


break statement

The break statements are used to,

-        Terminate a statement sequence in a switch statement
-        Exit a loop

 Example :

class BreakExample{

      public static void main(String args[]){
           
            for(int count=1;count<=100;count++;{
                  if(count==10){
                        break;
                  }
                  System.out.println(“The value of num is : “ +count);
            }
      System.out.println(“Outside loop“);
      }
}

continue statement

Sometimes the programmer might want to continue a loop, but stop processing the remainder of the code in its body for a particular iteration. The continue statement can be used for such a scenario. In while and do-while loops, a continue statement can be used, as shown in below example.

 Example :

class ContinueDemo{
     
      public static void main(String args[]){
           
            for(int count = 1; count <= 10; count++){
                  System.out.println(“Value of count before :  “+count);   
                  if(count%2==0){
                        continue;
                  }
                  System.out.println(“Value of count after :  “+count);
            }
      }
}

In above example, whenever even number is iterated in loop, second print statement will not execute.

3 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
  2. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
  3. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
    Java Training in Chennai

    ReplyDelete