Showing posts with label break. Show all posts
Showing posts with label break. Show all posts

Friday, 30 May 2014

Break Statement in Java

Break Statement

In java , the break statement has three uses.
1. It terminates a statements sequence in switch statement.
2. It can be used to exit a loop.
3. It can be used as a "civilized" form of goto.

And last two uses are explained here.

2. Using the break to exit loop:

By break  , you can force to terminates the loop.When a break statement is encountered inside a loop , 
the loop is terminated and program control reeches to the next statement to the loop.The break statement can be used with any of the loop.

Simple program of Break statement:

package org.modi;
public class Test
{
  public static void main(String[] args)
  {
 int i=1;
 while(i<100)
 {
  if(i==5)
  {
   break; //terminates the loop while i become 5
  }
  System.out.println("i : "+i);
  i++;
 }
 System.out.println("After while loop");
 }
}
OUTPUT
i : 1
i : 2
i : 3
i : 4
After while loop
You can see in above program the while loop designed  to iterate from 1 to 99 ,
But the break statement terminate the loop while i reach to 5
When break statement  used inside a set of nested loops , it will only break out the innermost loop.

3. Using break as a goto form:

Java does not have a goto statement,so where goto is required java defines the expanded form of the break statement.
By using this form of the break statement you can break out one or more blocks of code.
This form of the break works with a label.

The general form of the labeled break statement is :

break label;
Here,label is the name of a label that identifies the block of the code.When this form of the break executes , control transferred 
to the named block.
To name a block , put label at the start of it.A label is valid java identifier followed by a colon (:) .

Simple program of break statement in from of goto:

package org.modi;
public class Test
{
   public static void main(String[] args)
   {
       one:
       {
          two:
         {
             three:
            {
              System.out.println("Block Three begin");
              if(true)
                  break two;  //control goes out of block two
              System.out.println("Block Three End");
           }
           System.out.println("Block Two");
        }
        System.out.println("Block one");
      }
   }
 
}
OUTPUT
Block Three begin
Block one

Thursday, 29 May 2014

Break And Continue in C

Break:
The break statement allows you to exit a loop from any point within its body, bypassing its normal termination expression. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop. The break statement can be used with all three of C's loops for, while and do...while loops.
Syntax:
break;

The figure below explains the working of break statement in all three type of loops.


Here's an example of a use for the break statement:
#include
int main()
{
 int t ;
 for ( ; ; ) 
 {
  scanf("Enter any no.\n%d" , &t) ;
  if ( t==10 ) 
                break ;
 }
 printf("End of an infinite loop...\n");
 return 0;
 }
Output:
Enter any no.
2
3
10
End of an infinite loop...
In above example, it takes no. until user enter 10 because t=10 then it will break.

Continue:
The continue statement is somewhat the opposite of the break statement. It forces the next iteration of the loop to take place, skipping any code in between itself and the test condition of the loop. In while and do-while loops, a continue statement will cause control to go directly to the test condition and then continue the looping process. In the case of the for loop, the increment part of the loop continues. One good use of continue is to restart a statement sequence when an error occurs.

Syntax:
continue; 

Here's an example of a use for the break statement:

#include
int main()
{
 int x ;
 for ( x=0 ; x<=100 ; x++) 
 {
  if (x%2) continue;
  printf("%d\n" , x);
 }
}

Decision Making In C

Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C language handles decision-making by supporting the following statements,

This also we known as control statement.It is also divided into following category:
1. Selection statements.(if..else and switch-case statement)
2. Iteration or looping statements.(for loop and do-while loop)
3. Jump statements.(break, continue and goto statement)

Friday, 23 May 2014

Java Control Statements

In this post we are going to discuss the control statements.
The control statements are used to control the flow of execution of the program.
Different type of control statements are:
1. Selection statements.
2. Iteration or looping statements.
3. Jump statements.

1.Selection statements:
 Java supports two selection statements if and switch. These  statements allows you to control the flow of your program's execution based upon conditions known only during run time.
For details information on control statements click on below links.

1. If
2. Switch

2. Iteration or Looping statements:
Java supports three Iteration or Looping statements for , while and do while.It executes a block of code or statements till the given condition is true. 
For details information on Iteration or Looping statements click on below links.

1. For loop
2. While loop
3. Do-While loop

3. Jump statements:
Java supports three Jump statements break , continue and return .These statements transfer control to another part of your program.
For details information on Jump statements click on below links.

1. break 
2. continue
3. return