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

Saturday, 30 April 2016

break Statement as a exit for a loop

How to use break statement as an exit from a loop?


               break statement can be used as an exit from a loop bypassing the conditional expression and the remaining code inside the loop. When break statement comes inside a loop, the loop terminates immediately and program control resumes at the next statement following the loop. 

               Following are few points to remember when use the break as a terminator from the loops. 


  • When we use the break statement inside a set of nested loops, it only break out of the innermost loop. 
  • More than one break statement can be used inside the loop. 
  • The break statement that terminates a switch statement affects only the switch statement and not any enclosing loop. 
  • break statements are not designed to provide a normal means of exit from a loop. break statement should be used to cancel a loop only when some sort of special situation occurs. 


Here is a simple program where the break statement is used as an exit from the loop. 

Program


// Using break to exit from a loop.
class LoopBreak{
public static void main(String args[]) 
{
    for(int i=0; i<100 font="" i="" nbsp="">
    {
        if(i == 11) break; // terminate loop if i is 11
        System.out.println("i: " + i);
    }
    System.out.println("After Loop.");
}
}

Output:


i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
i: 10
After Loop.

Saturday, 9 April 2016

Jump Statements in Java

Java Jump Statements : break, continue and return


               Java supports three jump statements. They are

                          1. break
                         2. continue
                         3. return

1. break:


               break statement in Java can be used in three ways. Other-words, the break statement has three used in the application. They are as follows

  • It terminates a statement sequence in a switch statement.
  • break statement is used to exit from a loop.
  • It can be used for a goto form of operation as Java doesn't have a goto keyword/statement as its own.


2. continue:


               continue statement is used to force an early iteration in a loop. i.e, if you wish to discard the remaining code part inside a loop and go back to the iteration part, the continue statement will do the job for you. In while and do-while loop, the continue statement transfer control directly to the condition expression of the loop

               In the case of for loop, control goes first to the iteration part and then transfer to the condition expression. 

Example program for continue statement in Java

3. return:


               The return statement is used to explicitly return from a method. The return statement can be used to terminate from a method and return control to its caller position. 

Example program for return statement in Java

Thursday, 9 January 2014

JUMP STATEMENTS OR BRANCHING STATEMENTS:break

 Jump statements:break,continue,and return:These statements transfer control to another part of your program.

Using break statement


break:


Break statement is one of the several control statements Java provide to control the flow of the program.Break Statement is generally used to break the loop of switch statement.In java,break statement has three uses.First ,as you  have seen,it terminates a statement sequence in a switch statement.Second,it can be used to exit a loop.Third,it can be used as a : "civilized" form of goto.

Let’s look at syntax of break statement:

break;



The break statement is written as break; without any expression.

Example for simple break  statement

class BreakDemo
{
  public static void main(String[] args)
  {
  for (int i = 0; i < 5; i++)
  {
  System.out.println(i);
  if (i==3)
  {
  break ;
  }
  }
  }
}

This would produce the following results

0
1
2
3




Saturday, 2 February 2013

Introduction to Control Statements in Java


Control statements decide flow of a program

JAVA CONTROL STATEMENTS

if, if-else, switch, nested if, switch, for, while, do-while, break, continue and return control statements


Control statements are used in programming languages to cause the flow of control to advance and branch based on changes to the state of a program. The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.
In Java, control statements can be divided under the following three categories: