Conditional structures: Difference between revisions

From Rosetta Code
Content added Content deleted
m (woops, typo)
m (Added PHP (switch) part, cleared up a bit)
Line 100: Line 100:
==[[PHP]]==
==[[PHP]]==
[[Category:PHP]]
[[Category:PHP]]

if


'''Interpreter''': [[PHP]] 3.x & 4.x & 5.x
'''Interpreter''': [[PHP]] 3.x & 4.x & 5.x
Line 126: Line 128:
//do another thing
//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;
}
}
Line 133: Line 160:
=See Also=
=See Also=
* [http://www.php.net/manual/en/language.control-structures.php php.net:Control Structures]
* [http://www.php.net/manual/en/language.control-structures.php php.net:Control Structures]
* [http://www.php.net/manual/en/control-structures.switch.php php.net:Control Structures: Switch]

Revision as of 16:22, 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
  }
}

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