Add Your Heading Text Here

Switch statement is a type of branching control flow statement.

Switch statement takes an expression (which results the constant value) and many block of codes are written for different constant values.

The constant expression taken by the switch statement compares the different constant values and the constant value which equals the value of the expression is selected and the code associated with that constant value gets executed.

The different constant values for which different code blocks are written are known as cases.

The structure of the switch case is neat and clean.

Syntax :

switch(constant expression){
            case value 1 :
                        // the block of code gets executed if constant expression equals value11).                                break;
            case value 2 :
                        // the block of code gets executed if constant expression equals value2.
                        break;
                                    …..
                                    …..
            default :
                        // the block of code gets executed if the constant expression doesn’t match any case.
}

In switch statement, there is another keyword called default. default keyword is used as the case whose block of code gets executed when the value of the constant expression of the switch statement doesn’t match to any case value.

In dart, the block of code written for any switch case must end with a break statement. For default case it’s not necessary to write break statement at the end.

For ex :

For ex: 
switch("sunday"){
    case "sunday":
      	print("sunday");
     	break;
     case "monday":
      	print("monday");
      	break;
      case "tuesday":
      	print("tuesday");
      	break;
    default : print("wednesday");
  }

Output :

sunday

default statement is optional. If you don’t write default case and the switch case expression doesn’t match any cases it will run the program without any error.

In switch statement, there is no place for duplicate switch cases.

So switch statement is another type of branching flow statement. It is used to control the flow of execution based on constant expression of switch statement.

assert :

assert statement is used to terminate the normal execution of the program when the expression evaluates to false.

Syntax :

assert(expression);

For ex :

void main(){
	int i=0;
	assert(i>5);
	print("Hi");
	print("Hello");
}

Output :

Uncaught exception: Assertion failed.

Assertion stands for “Confirmation”. Also, in above example, assert confirms that if the value of the i is greater than 5(i.e., assert expression evaluates to true), only then further execution of the program will take place. Otherwise(i.e., assert expression evaluates to false), an assertion exception will be thrown with the program termination.

So, these control flow statements are directly or indirectly helpful for us to control the flow of the program execution.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons