Conditional structures

From Rosetta Code
Revision as of 16:30, 25 January 2007 by MikeMol (talk | contribs) (Added Perl)

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>

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
}

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;

}

?>

See Also