Conditional structures

From Rosetta Code
Revision as of 17:22, 26 January 2007 by 66.213.222.18 (talk)

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
  }
}

Compile-Time Control Structures

Preprocessor Techniques

  1. ifdef, #ifndef, etc..

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


See Also