Hamilton LaboratoriesHamilton C shell 2012User guideTutorials

if and switch

Oregon Coast

if and switch
Previous | Next

Topics

if statement
switch statement
See also

The if and switch statements provide condition testing.

if statement

The if statement comes in two forms. The short form is convenient if the choice is only between executing and not executing a single statement, which appears on the same line.

390 D% if (5 == 2 + 3) echo yes yes 391 D% if (5 == 10) echo really 392 D% _

The longer form provides the more traditional if-then-else structure. Indentation is a matter of choice, it’s used in these examples merely to improve readability.

392 D% if (5 == 10) then 393 D? echo 5 == 10 394 D? else 395 D? echo 5 is not 10 396 D? end 5 is not 10 397 D% _

switch statement

The switch statement works by attempting to pattern match the switch value against a series of alternative cases. The switch and case values can all be arbitrary expressions. If any pattern match succeeds, execution begins with the next statement following and continues, skipping over any interspersed case clauses until either the end of the switch block or a break statement is reached.

397 D% switch ("hello world") 398 D? case 5: 399 D? echo hit 5 400 D? case "h*": 401 D? echo hit "h*" 402 D? case "x*": 403 D? echo hit "x*" 404 D? break 405 D? case 43.2: 406 D? echo hit 43.2 407 D? default: 408 D? echo did not hit 409 D? end hit h* hit x*

The break statement used here causes execution to “break out of” the innermost control structure. If you’re nested several layers deep into control structures and want to break out of a higher level structure you can label the higher level structure and specify that name on the break statement.

See also

Condition testing
Miscellaneous statements
Expression operators
Wildcard characters
Tutorial: Expressions
Tutorial: Wildcarding

Previous | Next

Getting started with Hamilton C shell

Hamilton C shell, as it first wakes up.

Getting started with Hamilton C shell

A first few commands.

You can set the screen colors to your taste.

You can set the screen colors to your taste.