+ 4
Hello! Umm... Can't we use constant expression in switch case???
11 ответов
+ 4
Important Points about Switch Case Statements:
1.The expression provided in the switch should result in a constant value otherwise it would not be valid.
Valid expressions for switch:
// Constant expressions allowed
switch(1+2+23)
// Variable expression are allowed provided they are assigned with fixed values
switch(a*b+c*d)
switch(a+b+c)
2.Duplicate case values are not allowed.
3.The default statement is optional.Even if the switch case statement do not have a default statement,
it would run without any problem.
4.The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
5.The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached.
6.Nesting of switch statements are allowed, which means you can have switch statements inside another.
+ 3
Hey Suryakanta Jena please specify your question in detail.
If you are asking about switch's conditional expression , then you can you either any constant_value or constant_expression.
e.g.// Constant expressions allowed
switch(1+2+23)
+ 3
Thirt13n no no... I'm asking.. Can we use... Case(3+2) or case(3*2+1)... Like this??
+ 3
Suryakanta Jena
Ofcourse you can
+ 3
Of my teacher confused me.. Said only integer or character we can use..
+ 3
Suryakanta Jena
Switch Statement in C/C++:
Switch case statements are a substitute for long if statements that compare a variable to several integral values.
The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
Switch is a control statement that allows a value to change control of execution.
Syntax:
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
+ 3
switch is a conditional control structure but it revolves around a single variable. this has well been illustrated by Thirt13n.This means that the value of the variables can change. This conflicts with what a constant is. constant values cannot change so the switch statement cannot be used with a constant
+ 2
Suryakanta Jena you can use enumeration also
+ 2
Can you explain a bit more? About switch.. And enumeration Thirt13n
+ 2
Facts:
1) The expression used in switch must be integral type ( int, char and enum). Any other type of expression is not allowed.
2) All the statements following a matching case execute until a break statement is reached.
3) The default block can be placed anywhere. The position of default doesn’t matter, it is still executed if no match found.
4) The integral expressions used in labels must be a constant expressions.
5) The statements written above cases are never executed After the switch statement, the control transfers to the matching case, the statements written before case are not executed.
6) Two case labels cannot have same value.
+ 2
Okay.. This is helpful.. Thanks