This wiki is out of date, use the continuation of this wiki instead

Switch

From FenixWiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 14:24, 20 February 2008 (edit)
Sandman (Talk | contribs)
m
← Previous diff
Current revision (12:58, 16 July 2008) (edit) (undo)
Sandman (Talk | contribs)
m (Example)
 
(4 intermediate revisions not shown.)
Line 10: Line 10:
'''Switch''' ( <value> )<br> '''Switch''' ( <value> )<br>
-:['''Case''' <value>:<br>+:['''Case''' <value>''':'''<br>
:<nowiki><code></nowiki><br> :<nowiki><code></nowiki><br>
:'''End''']<br> :'''End''']<br>
-:['''Default''':<br>+:['''Default:'''<br>
:<nowiki><code></nowiki><br> :<nowiki><code></nowiki><br>
-:'''End'''<br>+:'''End''']<br>
'''End''' '''End'''
Line 34: Line 34:
</pre> </pre>
-When the '''switch''' is reached it will compare ''value'' with all the values in the '''case'''s. When one compares, that code is executed and the '''switch''' is exited. This is different than the '''switch''' in C and probably more languages, because there the '''switch''' is only exited when a '''break''' is reached or when the '''switch''' ends.+When the '''switch''' is reached it will compare ''value'' with the values in the '''case'''s, going from top to bottom. When a case matches, that code is executed and the '''switch''' is exited. This is different than the '''switch''' in C and probably more languages, because there the '''switch''' is only exited when a [[break]] is reached or when the '''switch''' ends. In Fenix there is no break; for the switch, though.
A value in a '''case''' can also be a range: <code><lowervalue>..<uppervalue></code>. Both the ''lowervalue'' and the ''uppervalue'' are part of the range. A value in a '''case''' can also be a range: <code><lowervalue>..<uppervalue></code>. Both the ''lowervalue'' and the ''uppervalue'' are part of the range.
Line 42: Line 42:
== Example == == Example ==
-A scoretext function. Notice the '''default''': when the points can be 0..100, that code should never be executed. However, an error can occur and blurting an error to the user is not that fancy, so this is a way of showing to the programmer there is an error, but still the user gets some message. In such cases, '''default''' can be handy. Of course that code could just as easily have been put under the '''switch''' with the same result.+A scoretext function. Notice the '''default''': when the points can be 0..100, that code should never be executed. However, an error can occur and blurting an error to the user is not that fancy, so this is a way of showing to the programmer there is an error, but still the user gets some message. In such cases, '''default''' can be handy. Of course that code could just as easily have been put under the '''switch''' with the same result, in this case, because every case does a [[return]].
<pre> <pre>
Line 59: Line 59:
End End
Case 60..80: Case 60..80:
- return "Not to shabby, mate.";+ return "Not too shabby, mate.";
End End
Case 50..60: Case 50..60:

Current revision

Up to Control Flow Statements



[edit] Definition

Switch ( <value> )

[Case <value>:
<code>
End]
[Default:
<code>
End]

End

A Switch is used to control the flow of a program by means of comparing a value to other values and executing the code associated with the correct value.

switch ( <value> )
    case <value1>:
        // code1
    end
    case <value2>:
        // code2
    end
    default:
        // code3
    end
end

When the switch is reached it will compare value with the values in the cases, going from top to bottom. When a case matches, that code is executed and the switch is exited. This is different than the switch in C and probably more languages, because there the switch is only exited when a break is reached or when the switch ends. In Fenix there is no break; for the switch, though.

A value in a case can also be a range: <lowervalue>..<uppervalue>. Both the lowervalue and the uppervalue are part of the range.

You can also specify multiple values in a case, separated by a comma: <value1>,<value2>,...<code>. These values can also be ranges.

[edit] Example

A scoretext function. Notice the default: when the points can be 0..100, that code should never be executed. However, an error can occur and blurting an error to the user is not that fancy, so this is a way of showing to the programmer there is an error, but still the user gets some message. In such cases, default can be handy. Of course that code could just as easily have been put under the switch with the same result, in this case, because every case does a return.

Function String scoretext( int points )
Begin

    Switch( points )
        Case 100:
            return "1337 |-|4><0|2!!1";
        End
        Case 90..100:
            return "Awesomely cool, dude!";
        End
        Case 80..90:
            return "You're getting the hang of it!";
        End
        Case 60..80:
            return "Not too shabby, mate.";
        End
        Case 50..60:
            return "Practice some more.";
        End
        Case 30..50:
            return "Dude...weak.";
        End
        Case 1..30:
            return "That's just awful";
        End
        Case 0:
            return "No points? n00b!";
        End
        Default:
            return "I dunno what you did, but...";
        End
    End

End
Personal tools