Switch statement example in Malab and C programming language | applied electronics engineering

Latest

Switch statement example in Malab and C programming language

By Applied Electronics - Friday, March 17, 2017 No Comments
Switch statement is valuable decision making elements in programming languages like Matlab and C. Matlab programming language switch statement is same in functionality as in C and using both the inbuilt keywords switch and case. The difference is only minor in the way the block of the switch case is written.

Switch statement is similar to to if else statement but if the number of condition is large than switch statements should be used. See also How to make decision with Matlab programming language.

In C programming language the switch statement block is written inside curly braces. The general syntax of switch is below,

switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}

So the input argument of switch in C expects an integer expression. Following is an example.

int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in case 1 \n" ) ;
case 2 :
printf ( "I am in case 2 \n" ) ;
case 3 :
printf ( "I am in case 3 \n" ) ;
default :
printf ( "I am in default \n" ) ;
}

The output in this case is,

I am in case 2
I am in case 3
I am in default

You may have noticed and asked why all the other cases after the second case are also printed out or displayed. The reason is in C after the match has been found the program code will be executed normally, that is all other C statements are executed.

But you don't want that so how to do? This is where break keyword comes in. You may have heard of break keyword in switch case or have forgotten. So here is the above example with break.

int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in case 1 \n" ) ;
break ;
case 2 :
printf ( "I am in case 2 \n" ) ;
break ;
case 3 :
printf ( "I am in case 3 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}

One thing is important here, there is no need for break in the default break statement. One more last thing about switch statement in C.  You can use char also for the switch statement. An example is below.

char c = 'x' ;
switch ( c )
{
case 'v' :
printf ( "I am in case v \n" ) ;
break ;
case 'a' :
printf ( "I am in case a \n" ) ;
break ;
case 'x' :
printf ( "I am in case x \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}

Moving on to Matlab programming language, the general switch statement syntax is as follows.

switch (switch_expr)
case {case_expr_1, case_expr_2, case_expr_3}
Statement 1
Statement 2 Block 1
...
otherwise
Statement 1
Statement 2 Block n
...
end

An example of a switch statement is below.

switch units
case ’length’
disp(’meters’)
case ’volume’
disp(’liters’)
case ’time’
disp(’seconds’)
otherwise
disp(’I give up’)
end

Now we can compare switch in Matlab and C. The differences between switch statement in C and Matlab programming language are:
  • switch in C requires condition within parenthesis
  • there is no need for break in Matlab whereas in C this is required
 See also While statement in C, python and Matlab programming language.

Tags:

No Comment to " Switch statement example in Malab and C programming language "