switch, check
switch/check ( expression ) { case ( comparison expression ) statement; case ... [ else : statement; ]
switch/check ( expression ) { case ( comparison expression ) { statements }; case ... [ else { statements } ]
The switch function allows to execute exactly one code block among alternatives where the condition is met.
The check function allows to execute multiple code blocks wherever the conditions are met.
The subsequent block may consist of a combination of case statements as well as else. The statement(s) after
else will be executed if none of the cases were executed.
Following the else, additional case statements are allowed to do another comparison round.
Indirect parameter passing is disabled
1
No. | Type | Description |
---|---|---|
1 input |
all types | Expression The value provided here will be calculated once and then compared in the subsequent case procedures. |
echo("Try 'switch:'");
for (a[] = 1, a[] <= 6, a[]++)
{
print ( " ",a[], ": " );
switch( a [] )
{
case (>1) print("Greater than 1; ");
case (<>3) print("Not equal to 3; ");
case (4,6) print("Equals to 4 and 6; ");
else: print ("Not matching; "); // else will also enable another case round
case (>2) print ("Greater than 2; ");
else: print ("Else: at most 2; ");
else: echo ("Done");
}
}
echo("Try 'check':");
for (a[] = 1, a[] <= 6, a[]++)
{
print ( " ",a[], ": " );
check( a [] )
{
case (>1) print("Greater than 1; ");
case (<>3) print("Not equal to 3; ");
case (4,6) print("Equals to 4 and 6; ");
else: print ("Not matching; "); // else will also enable another case round
case (>2) print ("Greater than 2; ");
else: print ("Else: at most 2; ");
else: echo ("Done");
}
}
Try 'switch:'
1: Not equal to 3; Else: at most 2; Done
2: Greater than 1; Else: at most 2; Done
3: Greater than 1; Greater than 2; Done
4: Greater than 1; Greater than 2; Done
5: Greater than 1; Greater than 2; Done
6: Greater than 1; Greater than 2; Done
Try 'check':
1: Not equal to 3; Else: at most 2; Done
2: Greater than 1; Not equal to 3; Else: at most 2; Done
3: Greater than 1; Greater than 2; Done
4: Greater than 1; Not equal to 3; Equals to 4 and 6; Greater than 2; Done
5: Greater than 1; Not equal to 3; Greater than 2; Done
6: Greater than 1; Not equal to 3; Equals to 4 and 6; Greater than 2; Done