Conditional structures: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added SmallTalk, moved Java to its alphabetical position in the list. (oops!))
m (→‎AppleScript: Added internal link to AppleScript)
Line 1: Line 1:
==AppleScript==
==[[AppleScript]]==
[[Category:AppleScript]]
[[Category:AppleScript]]
===if-then-else===
===if-then-else===

Revision as of 17:38, 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
  }
}

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!'.].


See Also