{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CH03 - Chapter 3 Java an Introduction to Computer...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 3 ● Branching ● Loops ● exit(n) method ● Boolean data type and expressions Flow of Control Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch Review of “Flow of Control”? ● Flow of Control » Execution order of instructions in program ● All programs can be written with three control flow elements: Sequence- just go to the next instruction Branching or Selection – either go to next instruction or jump to some other instruction Loop or Repetition – - Repeat block of code - At end of loop either: a) go back and repeat the block of code b) or continue with the next instruction after the block Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch Java Flow Control Statements Sequence ● Default ● Executes next instruction unless use branching Branching ● if ● if-else ● if-else if-else if- … - else ● switch Loop ● while ● do-while ● for Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch Java if Statement ● Simple selection ● Do the next statement if test is true or skip it if false ● Syntax: if (Boolean_Expression) Action if true; //execute only if true next action; //always executed Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch if Example if (eggsPerBasket < 12) //begin body of the if statement System.out.println(“Less than a dozen eggs per basket”); //end body of the if statement totalEggs = numberOfEggs * eggsPerBasket; System.out.println(“You have a total of + totalEggs + “ eggs.”); Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch Multiple Statements in if if(eggsPerBasket < 12) { //begin body of the if statement System.out.println(“Less than a dozen ...”); costPerBasket = 1.1 * costPerBasket } //end body of the if statement totalEggs = numberOfEggs * eggsPerBasket; System.out.println(“You have a total of “ + totalEggs + “ eggs.”); All statements between braces are controlled by if Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch Two-way Selection: if-else Syntax: if (Boolean_Expression) { Action1 //execute only if true } else { Action2//execute only if false } Action3//always executed Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch if-else Examples ● Example with single-statement blocks: if(time < limit) System.out.println(“You made it.”); else System.out.println(“You missed the deadline.”); ● Example with compound statements: if(time < limit) { System.out.println(“You made it.”); bonus = 100; } else { System.out.println(“You missed the deadline.”); bonus = 0; } Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch Definition of Boolean Values ● Branching: » here is more than one choice for the next instruction...
View Full Document

{[ snackBarMessage ]}

Page1 / 46

CH03 - Chapter 3 Java an Introduction to Computer...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online