Showing posts with label break jumping statement. Show all posts
Showing posts with label break jumping statement. 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

Use of break statement with switch

How break is useful with switch statement in Java?


               The break statement is used to terminate the statement sequence in a switch statement. If we do not use break statement, the control of flow of the switch continue as usual and just work like an else-if ladder. 

               The below program print the month name on the screen according to the provided number. 

Program:


import java.util.Scanner;
public class SwitchBreakDemo {
    public static void main(String[] args) {

        int month;
        System.out.println("Enter a number to display its corresponding Month Name");
        Scanner in = new Scanner(System.in);  
        month=in.nextInt();
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }
        System.out.println(monthString);
    }
}

Output:


Enter a number to display its corresponding Month Name
5
May