describe the fall through situation on numeric data and explain
Answers
Answer:
A switch statement in Swift 4 completes its execution as soon as the first matching case is completed instead of falling through the bottom of subsequent cases as it happens in C and C++ programming languages.
The generic syntax of a switch statement in C and C++ is as follows −
switch(expression){ case constant-expression : statement(s); break; /* optional */ case constant-expression : statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); }
Here we need to use a break statement to come out of a case statement, otherwise the execution control will fall through the subsequent case statements available below the matching case statement.
Syntax
The generic syntax of a switch statement in Swift 4 is as follows −
switch expression { case expression1 : statement(s) fallthrough /* optional */ case expression2, expression3 : statement(s) fallthrough /* optional */ default : /* Optional */ statement(s); }
If we do not use fallthrough statement, then the program will come out of the switch statement after executing the matching case statement. We will take the following two examples to make its functionality clear.
Example 1
The following example shows how to use a switch statement in Swift 4 programming without fallthrough −
") no-repeat;">Live Demo
var index = 10 switch index { case 100 : print( "Value of index is 100") case 10,15 : print( "Value of index is either 10 or 15") case 5 : print( "Value of index is 5") default : print( "default case") }
When the above code is compiled and executed, it produces the following result −
Value of index is either 10 or 15 Example 2
The following example shows how to use a switch statement in Swift 4 programming with fallthrough −
") no-repeat;">Live Demo
var index = 10 switch index { case 100 : print( "Value of index is 100") fallthrough case 10,15 : print( "Value of index is either 10 or 15") fallthrough case 5 : print( "Value of index is 5") default : print( "default case") }
When the above code is compiled and executed, it produces the following result −
Value of index is either 10 or 15 Value of index is 5
Explanation:
please follow me also
Answer:
The switch statement terminates, whenever it reaches a break statement, and the action jumps to the line after the switch statement. In a switch statement if no break appears, then the control will fall through to subsequent cases till it hit a break statement in the loop.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. ... Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached
okay!!!!!
please mark as brainlist.