Conditional structures: Difference between revisions
(→Compile-Time Control Structures: Preprocessor conditionals expanded) |
|||
Line 120: | Line 120: | ||
==== Preprocessor Techniques ==== |
==== Preprocessor Techniques ==== |
||
Conditional compile based on if a certain macro exists, |
|||
#ifdef, #ifndef, etc.. |
|||
#ifdef FOO |
|||
// compile this only if macro FOO exist |
|||
#endif |
|||
Conditional compile based on if a macro doesn't exist |
|||
#ifndef FOO |
|||
// only compiled if macro FOO does not exist |
|||
#endif |
|||
Conditional compile based on if a certain macro exists, with else clause |
|||
#ifdef FOO |
|||
// compile this only if macro FOO exist |
|||
#else |
|||
// compile this only if macro FOO does not exist |
|||
#endif |
|||
Conditional compile based on expression |
|||
#if defined(FOO) && FOO == 1 |
|||
// only compiled if macro FOO is compiled and expands to a constant expression evaluating to 1 |
|||
#endif |
|||
Chain of conditionals |
|||
#if defined(FOO) |
|||
// only compiled if macro FOO is defined |
|||
#elif defined(BAR) |
|||
// only compiled if macro FOO is not defined, but macro BAR is |
|||
#else |
|||
// only compiled if neither FOO nor BAR is defined |
|||
#endif |
|||
Typical usage: Include guards |
|||
#ifndef FOO_H_ALREADY_INCLUDED |
|||
#define FOO_H_ALREADY_INCLUDED |
|||
// header content |
|||
#endif |
|||
If the header is included the first time, the macro FOO_H_ALREADY_INCLUDED will not be defined, thus the code between #ifndef and #endif will be executed. The first thig this code does is to define that macro, so that the next time the header is included, the code will be ignored. This effectively avoids multiple inclusion. |
|||
==[[ColdFusion]]== |
==[[ColdFusion]]== |
Revision as of 21:05, 26 January 2007
Here, we document the conditional structures offered by different programming languages. Common conditional structures are if-then-else and switch.
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 } }
?:
Compiler: GCC 4.0.2
Conditionals in C++ can also be done with the ?: operator. The arguments are expressions, and a?b:c is an expression as well. However, since many things in C++ are expressions (this especially includes assignments and function calls), ?: can be used for those, too. However, the if/else construct is usually more readable and therefore preferred.
int main() { int input = 2; int output = (input == 2? 42 : 4711); // sets output to 42 int output2 = (input == 3? 42 : 4711); // sets output2 to 4711 void do_something(); void do_something_else(); input == 1? do_something() : do_something_else(); // only calls 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 } }
Compile-Time Control Structures
Preprocessor Techniques
Conditional compile based on if a certain macro exists,
#ifdef FOO // compile this only if macro FOO exist #endif
Conditional compile based on if a macro doesn't exist
#ifndef FOO // only compiled if macro FOO does not exist #endif
Conditional compile based on if a certain macro exists, with else clause
#ifdef FOO // compile this only if macro FOO exist #else // compile this only if macro FOO does not exist #endif
Conditional compile based on expression
#if defined(FOO) && FOO == 1 // only compiled if macro FOO is compiled and expands to a constant expression evaluating to 1 #endif
Chain of conditionals
#if defined(FOO) // only compiled if macro FOO is defined #elif defined(BAR) // only compiled if macro FOO is not defined, but macro BAR is #else // only compiled if neither FOO nor BAR is defined #endif
Typical usage: Include guards
#ifndef FOO_H_ALREADY_INCLUDED #define FOO_H_ALREADY_INCLUDED // header content #endif
If the header is included the first time, the macro FOO_H_ALREADY_INCLUDED will not be defined, thus the code between #ifndef and #endif will be executed. The first thig this code does is to define that macro, so that the next time the header is included, the code will be ignored. This effectively avoids multiple inclusion.
ColdFusion
if-elseif-else
Compiler: ColdFusion any version
<cfif x eq 3> do something <cfelseif x eq 4> do something else <cfelse> do something else </cfif>
switch
Compiler: ColdFusion any version
<cfswitch expression="#x#"> <cfcase value="1"> do something </cfcase> <cfcase value="2"> do something </cfcase> <cfdefaultcase> do something </cfdefaultcase> </cfswitch>
Java
if-then-else
if(s.equals("Hello World")) { foo(); } else if(s.equals("Bye World")) { bar(); } else { deusEx(); }
ternary
s.equals("Hello World") ? foo : bar
switch
switch(c) { case 'a': foo(); break; case 'b': bar(); default: foobar(); }
JavaScript
if-then-else
if(s=="Hello World") { foo(); } else if(s=="Bye World") { bar(); } else { deusEx(); }
OCaml
if-then-else
Compiler: OCaml 3.09
let condition = true if condition then ()//do something else ()//do something else
match-with
match expression with | 0 -> ()//do something | 1 -> ()//do something | n when n mod 2 = 0 -> ()//do something | _ -> ()//do something
Pascal
if-then-else
Compiler: Turbo Pascal 7.0
IF condition1 THEN procedure1 ELSE procedure3; IF condition1 THEN BEGIN procedure1; procedure2; END ELSE procedure3; IF condition 1 THEN BEGIN procedure1; procedure2; END ELSE BEGIN procedure3; procedure4; END;
Perl
if-then-else
Interpreter: Perl 5.8.8
#!/usr/bin/perl -w use strict; my $condition1 = 0; my $condition2 = 1; if ( $condition1 ) { # Do something } # post-conditional if do_something() if $condition1;
#!/usr/bin/perl -w use strict; my $condition1 = 0; my $condition2 = 1; if ( $condition1 ) { # Do something } elsif ( $condition2 ) { # Do somethine else }
#!/usr/bin/perl -w use strict; my $condition1 = 0; my $condition2 = 1; if ( $condition1 ) { # Do something } else { # Do something else }
#!/usr/bin/perl -w use strict; my $condition1 = 0; my $condition2 = 1; if ( $condition1 ) { # Do something } elsif ( $condition2 ) { # Do something else } else { # Do that other thing }
($condition) ? print "Then\n" : print "Else\n"; # or my $var = ($condition) ? "Then" : "Else";
unless
Interpreter: Perl 5.8.8
#!/usr/bin/perl -w use strict; my $condition1 = 0; unless ( $condition1 ) { # Do something }
# post-conditional unless do_something() unless $condition1; unless ( $condition1 ) { # Do something } else { # Do something else }
switch
Interpreter: Perl 5.8.8
#!/usr/bin/perl -w use strict; use Switch; $input = 42; switch ($input) { case 0 { # Do something, because input = 0 } case 1 { # Do something, because input = 1 } case "coffee" { # Do something, because input = coffee } else { # Do something else. } }
goto
Interpreter: Perl 5.8.8
Typically dispised by most Perl programmers
goto LABELB; LABELA: # note labels end with a colon not semi-colon goto END; LABELB: goto LABELA; END: exit(0);
PHP
if
Interpreter: PHP 3.x & 4.x & 5.x
<?php $foo = 3; if ($foo == 2) //do something if ($foo == 3) //do something else //do something else
if ($foo != 0) { //do something } else { //do another thing } ?>
switch
Interpreter: PHP 3.x & 4.x & 5.x
<?php switch ($i) { case "apple": echo "i is apple"; break; case "bar": echo "i is bar"; break; case "cake": echo "i is cake"; break; } ?>
Python
if-then-else
if x == 0: foo() elif x == 1: bar() elif x == 2: baz() else: boz()
Ruby
if-then-else
if s == 'Hello World' foo elsif s == 'Bye World' bar else deus_ex end
case-when-else
case cartoon_character when 'Tom' chase when 'Jerry' flee else nil end
ternary
s == 'Hello World' ? foo : bar
SmallTalk
ifTrue/ifFalse
"Conditionals in Smalltalk are really messages sent to Boolean objects" (( balance) > 0) ifTrue: [Transcript cr; show: 'still sitting pretty!'.] ifFalse: [Transcript cr; show: 'No money till payday!'.].
Visual Basic .NET
if-then-else
Basic
Dim result As String, a As String = "pants", b As String = "glasses" If a = b Then result = "passed" Else result = "failed" End If
Condensed
Dim result As String, a As String = "pants", b As String = "glasses" If a = b Then result = "passed" Else result = "failed" If a = b Then result = "passed" Else : result = "failed" End If If a = b Then : result = "passed" Else result = "failed" End If
if-then-elseif
Dim result As String, a As String = "pants", b As String = "glasses" If a = b Then result = "passed" ElseIf a <> b Then result = "failed" Else result = "impossible" End If
select-case-else
Dim result As String, a As String = "pants", b As String = "glasses" Select Case a Case b result = "match" Case a : result = "duh" Case Else result = "impossible" End Select
inline-conditional
Imports Microsoft.VisualBasic ... Dim result As String = CType(IIf("pants" = "glasses", "passed", "failed"), String)
generic-inline-conditional
Compiler: Microsoft (R) Visual Basic Compiler version 8.0
Imports Microsoft.VisualBasic ... Function IIf2(Of T)(ByVal condition As Boolean, ByVal truepart As T, ByVal falsepart As T) As T If condition Then Return truepart Else Return falsepart End Function ... Dim result As String = IIf2("pants" = "glasses", "passed", "failed") ' type is inferred