Showing posts with label do-while loop. Show all posts
Showing posts with label do-while loop. Show all posts

Wednesday, 5 February 2014

Branching statement: continue

continue statement skips the current iteration of a for, while, do-while loops. Just like break, continue also has two forms.
    1. unlabelled continue
    2. labelled continue

Unlabelled continue
    Syntax
        continue;

Example: Count the number of odd values from 0 to 10
class ContinueEx{
 public static void main(String args[]){
  int counter = 0;
  
  for(int i=0; i < 11; i++){
   if(i % 2 == 0)
    continue;
   System.out.println(i);
   counter++;
  }
  
  System.out.println("Total number of odd numbers from 0 to 10 is " + counter);
 }
}
       
Output
1
3 
5
7
9  
Total number of odd numbers from 0 to 10 is 5 
       
Labelled continue
     Syntax
     continue labelName;

Example
class ContinueEx{
 public static void main(String args[]){
  int counter = 0;
  
  myLabel:
   for(int i=0; i < 11; i++){
    if( i < 10)
     continue myLabel;
    System.out.println(i);
   }
   
  System.out.println("I am out of the label");
 }
}

Output
10
I am out of the label

break statement                                                 arrays                                                 Home

Branching statement: break

The break Statement
We already seen the use of break in switch case. Similarly, break is used in while, do-while and for loops. Break has two forms in java.
   1. unlabelled break
   2. labelled break

unlabelled break
   Example
class BreakEx{
 public static void main(String args[]){
  for(int i=0; i < 10; i++){
   if(i==5) break;
   System.out.println(i);
  }
  System.out.println("I am out of the loop");
 }
}

Output
0
1
2
3
4
I am out of the loop
 
As you in the above program, when variable “i” reached to 5, break is called, and execution comes out of the loop

labelled break
   label is a group of statements, identified by using labelname.

Syntax
    labelName:{
      statement 1
      statement 2
      …....
      statement N
    }

Example
class BreakEx{
 public static void main(String args[]){
  myLoop: {
   for(int i=0; i < 10; i++){
    if(i==5) break myLoop;
    System.out.println(i);
   }
   System.out.println("I am also in label");
  }
  System.out.println("I am out of the loop"); 
 }
}
        
Output
0
1 
2
3
4 
I am out of the loop
 
As you see in the above program label name is myLoop. When variable 'i' value reaches to 5,  execution comes out of the label.




loops                                                 continue statement                                                 Home

Control flow statements : looping : while, do-while

while
while statement executes a block of statements until a particular condition is true

Syntax
    while (expression) {
      //statements
}

The expression in the while statement must evaluates to a boolean value, other wise compiler will throw error. While statement first evaluates the expression, until the expression evaluates to true, it  executes the code inside the while block, other wise it comes out of the while loop.

Example : Print numbers from 0 to 9 using while loop.
class WhileEx{
 public static void main(String args[]){
  int var1 = 0;
  while(var1 < 10){
   System.out.println(var1);
   var1++;
  }
 }
}


Output
0
1
2
3
4
5
6
7
8
9

do-while
   do-while is also a loop construct like while, but evaluates its expression at the bottom of the loop instead of the top.

    Syntax
    do {
        //statement(s)
    } while (expression);

Example
class DoWhileEx{
 public static void main(String args[]){
  int var1 = 0;
  do{
   System.out.println(var1);
   var1++;
  }while(var1<10);
 }
}
    
Output   
0
1
2
3
4
5
6
7
8
9

Some points to Remember
1. What is the difference between while and do-while ?
 Statements within the do block are always execute at least once even if the expression evaluates to false.

Example
class CompareLoop{
 public static void main(String args[]){
  boolean flag = false;
  
  while(flag)
   System.out.println("I am in while loop");
  do
   System.out.println("I am in do-while loop");
  while(flag);
 }
}
 
Output
I am in do-while loop
         
Observation
Note that do-while executed, even if the expression “flag” is false. And if you have only one statement, then no need to put the line in between blocks{ }

Control flow statements                                                 loops                                                 Home

Control flow statements : Decision Making: if-then, if-else, if-else if-else

Java provides control flow statements
     for decision making (if, if-else, switch) and
     for looping (for, while, do-while)

Block of statements
In java the statements between { } are called block of statements.
     {
          //block of code
     }

Decision making statements
if statement
if ” tell the program execute the section of code when the condition evaluates to true.
     Syntax
          if(condition){
               // Executes if the condition true
          }

Example
class Grades{
 public static void main(String args[]){ 
  int marks = 20;
  if(marks < 35 ){
   System.out.println("You failed in the Exam. Better luck next time");
  }
 }
}
Output
You failed in the Exam. Better luck next time

Some points to remember
1. In java the condition in the if must evaluates to boolean value, other wise compiler will throw error

Example
class Grades{
 public static void main(String args[]){
  int var1;
  if(var1 = 10){
  
  }
 }
}

When you try to compile the above program, you will get the below error
Grades.java:5: error: incompatible types
 if(var1 = 10){
 ^
 required: boolean
 found: int
1 error
    if-else statement
        Syntax:
        if(condition){

        }
        else{

        }

    If the condition true, then if block code executed. other wise else block code executed.

    Example
    class Grades{
     public static void main(String args[]){ 
      int marks = 36;
      
      if(marks < 35)
       System.out.println("You are failed");
      else
       System.out.println("You are passed");
     }
    }
    

    Output
    You are passed
    

    Observation
    In the above program we haven't written the if, else block codes in between { }, since if there is only one statement in between the blocks, we can omit { }.

    if-else if-else
    By using if-else if-else construct, you can choose number of alternatives.

    An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

    class Grades{
     public static void main(String args[]){
      int marks = 69;
      
      if(marks < 35)
       System.out.println("You are failed");
      else if( marks < 50)
       System.out.println("You are passed and got third class");
      else if( marks < 60)
       System.out.println("You are passed and got second class");
      else if( marks < 70)
       System.out.println("You are passed and got first class");
      else
       System.out.println("You are passed and got distinction");
     }
    }
    

    Output
    You are passed and got first class
    


    other Operators                                                 Control flow statements                                                 Home