Program Control Statemements

Page history last edited by Lucas Wojciechowski 1 yr ago

If/Then Statements

  • "If" expression is true, execute some code
  • "Else" do this code if statement is not true

if (expression) {

     statement

} else {

     statement

}


  • Expression can be anything that will return a numerical value
    • 0 is false, anything else is true (as always)
  • If statements can be nested

 

Else if

  • Can be placed after if{} and before else{}
  • Allows other expressions to be tested before runnning the code in "else"
  • As soon as one if statement is executed, all others will be skipped.

 

Switch Statement

  • Provides a multi-way branch
  • Allows a program to select amoung several pathways
  • Value of an expression is tested against a list of constants until a match is found
  • When a match is found, that code is executed

Switch(expression){

     case "constant":

          statement;

          break;

}


  • Must be either a character or interger
    • No floating point values
  • A "default:" section can be added to run if no matches are found
  • Differs from an if statement in that it can only test for equality
  • No two case constants can be the same
  • Generally more efficent than nested if statements
  • Each statement is not its own block, the entire thing is its own block.
  • If no break statement is in the case, the program will continue to execute the statements contained in other cases until a break is encountered.

 

While Loop

  • When expression is false, the loop will end.
  • The loop will be run until the expression is false

while(expression) statement;


 

Do While Loop

  • Checks contition at bottom of loop unlike a while loop

Do {statement;} while (condition);


 

Special Statements Within Loops

  • break; statement will end a loop early
  • continue; statement will cause the loop ti immediantly skip to the next ineration

 

Goto Statement

  • Considered bad programming practice
  • Jumps from a goto call to a specified location

Comments (0)

You don't have permission to comment on this page.