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

Sunday, 9 December 2018

JavaScript: Do I require break statement in default case of switch statement

If you added default case at the end of all the switch cases, then you do not require a break statement.

HelloWorld.js
function print_day_of_week(day){
  switch(day){
    case 1:
      console.log("Sunday");
      break;
    case 2:
      console.log("Monday");
      break;
    case 3:
      console.log("Tuesday");
      break;
    case 4:
      console.log("Wednesday");
      break;
    case 5:
      console.log("Thursday");
      break;
    case 6:
      console.log("Friday");
      break;
    case 7:
      console.log("Saturday");
      break;
    default:
      console.log("Not a valid day");
  }
}

print_day_of_week(10);

Output
Not a valid day

But if you add default case anywhere not at the end, then you should add break statement.

HelloWorld.js
function print_day_of_week(day){
  switch(day){
    case 1:
      console.log("Sunday");
      break;
    case 2:
      console.log("Monday");
      break;
    default:
      console.log("Not a valid day");
    case 3:
      console.log("Tuesday");
      break;
    case 4:
      console.log("Wednesday");
      break;
    case 5:
      console.log("Thursday");
      break;
    case 6:
      console.log("Friday");
      break;
    case 7:
      console.log("Saturday");
      break;
  }
}

print_day_of_week(10);

As you see above snippet, I added default case after case 2 and do not provided break statement inside default case.

When I ran above application, I seen below two messages.

Not a valid day
Tuesday

It is because, break is omitted, the program continues execution at the next statement in the switch statement.


Previous                                                 Next                                                 Home

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