1 / 11

Program Structures

Chapter 5. Program Structures. Branching. Allows different code to execute based on a conditional test. if , if-else , and switch statements. If , Else , and Else-If. if Statements “ If the sky is blue, I’ll ride my bike.” else Statements

tulia
Télécharger la présentation

Program Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Program Structures

  2. Branching • Allows different code to execute based on a conditional test. • if, if-else, and switch statements

  3. If, Else, and Else-If • if Statements • “If the sky is blue, I’ll ride my bike.” • else Statements • “If the sky is red, I’ll drive; else (otherwise) I’ll ride my bike.” • Else-if Idioms • “If the sky is blue, I’ll ride my bike; else if the sky is red, I’ll drive, else I’ll walk.”

  4. Nested if Statements • if statements can be nested (combined) within other if statements to account for multiple conditions. • If the sky is blue • If there are clouds in the sky • Do something for blue sky, cloudy days • Else • Do something for blue sky, cloudless days • Else • Do something for non-blue sky days

  5. The switch Statement • switch statements are an efficient way to choose between multiple options. • Easier to expand options than else-if structures • Easier to read than multiple else-if structures • Use the break keyword to prevent “falling through” to next statement

  6. Switch Evaluation • switch statements evaluate int values: • int x = 3; switch(x) { 1: x += 2; break; 2: x *= 2; break; 3: x /= 2; break; }

  7. Logical Operators • Combine or negate conditional tests • Logical AND (&&) • “The value of x is greater than 10 and the value of x is less than 20” • (x > 10) && (x < 20); • Logical OR (||) • “The value of x is greater than 10 or the value of x is lessthan 5” • (x > 10) || (x < 5); • Logical NOT (!) • “The value of x is not greater than 10” • ! (x > 10);

  8. Looping • What is iteration? • Iteration is a structure that repeats a block of code a certain number of times, or until a certain condition is met.

  9. The while Loop • “As long as this condition is true, execute this code” • while ( counter < 11) { counter++; } • Might not execute if the condition is already false

  10. The for Loop • Includes (optional) arguments for: • An initialization variable • A conditional test • An action • for ( int counter = 1; counter < 11; counter++ ) { System.out.print(counter + “ ”) }

  11. The do...while Loop • “Take this action, while this is true” • int value = 10; do { value = value + 2; } while ( value < 10 ); • Ensures at least one execution of code

More Related