Conditional structures: Difference between revisions
Content added Content deleted
(Added C section. Surgery complete. :)) |
(Added C++) |
||
Line 47: | Line 47: | ||
case 2: |
case 2: |
||
// Do something, because input = 2 |
// Do something, because input = 2 |
||
default: |
|||
// Do something else. |
|||
break; // Optional |
|||
} |
|||
} |
|||
==[[C plus plus|C++]]== |
|||
[[Category:C plus plus]] |
|||
=== Run-Time Control Structures === |
|||
====if-then-else==== |
|||
'''Compiler:''' [[GCC]] 4.1.2 |
|||
int main (void) { |
|||
int input = 2; |
|||
if ( 3 == input ) { |
|||
// Do something |
|||
} |
|||
if ( 3 == input ) { |
|||
// Do something |
|||
} else { |
|||
// Do something else |
|||
} |
|||
} |
|||
====switch==== |
|||
'''Compiler:''' [[GCC]] 4.1.2 |
|||
int main (void) { |
|||
int input = 42; |
|||
switch (input) { |
|||
case 0: |
|||
// Do something, because input = 0 |
|||
break; |
|||
case 1: |
|||
// Do something, because input = 1 |
|||
break; |
|||
case 2: |
|||
// Do something, because input = 2 |
|||
// Because there is no 'break', we also [[Fall-Through|fall through]] |
|||
// into the default case, executing it right after case 2: |
|||
default: |
default: |
||
// Do something else. |
// Do something else. |
Revision as of 16:05, 25 January 2007
AppleScript
if-then-else
if myVar is "ok" then return true
set i to 0 if i is 0 then return "zero" else if i mod 2 is 0 then return "even" else return "odd" end if
C
if-then-else
Compiler: GCC 4.1.2
int main (int argc, char ** argv) { int input = 2; if ( 3 == input ) { // Do something } if ( 3 == input ) { // Do something } else { // Do something else } }
switch
Compiler: GCC 4.1.2
int main (int argc, char ** argv) { int input = 42; switch (input) { case 0: // Do something, because input = 0 break; case 1: // Do something, because input = 1 break; case 2: // Do something, because input = 2 default: // Do something else. break; // Optional } }
C++
Run-Time Control Structures
if-then-else
Compiler: GCC 4.1.2
int main (void) { int input = 2; if ( 3 == input ) { // Do something }
if ( 3 == input ) { // Do something } else { // Do something else } }
switch
Compiler: GCC 4.1.2
int main (void) { int input = 42; switch (input) { case 0: // Do something, because input = 0 break; case 1: // Do something, because input = 1 break; case 2: // Do something, because input = 2 // Because there is no 'break', we also fall through // into the default case, executing it right after case 2: default: // Do something else. break; // Optional } }