Lecture_05 - ENGR 2200U UOIT/FEAS Fall 2007 ENGR 2710U OO Programming and Design Decisions Professor Saman Kumarawadu ENGR 2710U Fall 2010 Saman

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

View Full Document Right Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design 1 Decisions Professor Saman Kumarawadu ENGR 2710U Fall 2010 – Saman Kumarawadu ENGR 2710U OO Programming and Design Contents • To be able to implement decisions using if statements • To understand how to group statements into blocks • To learn how to compare integers, floating-point numbers, strings, and objects • To recognize the correct ordering of decisions in multiple branches • To program conditions using Boolean operators and variables
Background image of page 1

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

View Full DocumentRight Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Decision Statements • Instructions are fetched normally in a sequential order unless decision-making statements are used to break that sequence. • Java has two main selection statements – if and switch ENGR 2710U OO Programming and Design The if Statement The if statement lets a program carry out different actions depending on a condition Continued… if (amount <= balance) balance = balance - amount;
Background image of page 2
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design The if/else Statement Continued… if (amount <= balance) balance = balance - amount; else balance = balance - OVERDRAFT_PENALTY; ENGR 2710U OO Programming and Design Syntax 6.1: The if-else Statement if( condition ) statement if ( condition ) statement1 else statement2 Example: if (amount <= balance) balance = balance - amount; if (amount <= balance) balance = balance - amount; else balance = balance - OVERDRAFT_PENALTY; Purpose: To execute a statement when a condition is true or false
Background image of page 3

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

View Full DocumentRight Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design The switch Statement The if statement lets a program carry out different actions depending on a condition Continued… if (amount <= balance) balance = balance - amount; ENGR 2710U OO Programming and Design The switch Statement The switch is a multiway branch statement. Divert execution to different parts within the boundary of a program code section, observing the value of the switched expression. The general form of the switch statement will look like switch (expression) { case value1: statement_seq1; break; case value2: statement_seq2; break; default: default_statements; }
Background image of page 4
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Switch-example public class Sw itchDem o { public static void main() { for ( int month=1; month <= 12; month++) switch (month) { case 1: System.out.println("There are 31 days in January."); break; case 2: System.out.println("There are 28 days in February if not Leap year."); System.out.println("February will have 29 days in Leap years."); break; case 3: System.out.println("There are 31 days in March."); break; case 4: System.out.println("There are 30 days in April."); break; case 5: System.out.println("There are 31 days in May."); break; case 6: System.out.println("There are 30 days in June."); break; case 7: System.out.println("There are 31 days in July.");
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/10/2010 for the course PROGRAMMIN 2860U taught by Professor Saman during the Fall '10 term at UOIT.

Page1 / 24

Lecture_05 - ENGR 2200U UOIT/FEAS Fall 2007 ENGR 2710U OO Programming and Design Decisions Professor Saman Kumarawadu ENGR 2710U Fall 2010 Saman

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

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