Erin's CS stuff

CS stuff!

Selection Quick Guide

Type Application Example Notes
if execute a block of code when a condition is true if
  • statements within curly braces only execute when the expression between the parenthesis evaluates to 1 or true
  • when the expression between the parenthesis evaluates to 0 or false, the block of code is simply skipped
  • you can put anything inside these curly braces
    • this includes more if/else statements (making them "nested")
else if execute a block of code with mututally exclusive conditions else_if
  • only one of the paths can execute
    • the first block of code with an expression that evaluates to 1 or true will execute
    • previous blocks with expresions that evaluated to 0 or false were skipped
    • any following blocks beginning with an else will be skipped
  • these are also known as "cascading" if/else statements
else execute the "leftover" block of code when no conditions are met else these aren't always necessary
switch execute a block of code based on the value stored in a char or int variable switch
  • these do not work for decimal values
  • execution begins immediately after a true case and does not stop until a break; statement or the end
    • omitting the break; statement allows for "fall through" where code under subsequent cases also executes