Week 4 - Decisions

Condition statement1 statement3 start evaluate

Info iconThis preview shows page 1. Sign up to view the full content.

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: cs of if - else if (condition) { statement1; } else { statement2; } statement3; •  statement1 happens if condition is true •  statement2 happens if condition is false •  statement1 and statement2 will both never execute •  statement3 is always executed afterwards 10 Skipping else if (condition) { statement1; } statement3; Start Evaluate condition true false Run statement1 Run statement3 11 Code blocks •  Code can be grouped into blocks –  All lines of code (statements) are executed (sequentially) together –  Syntax: { statement1; statement2; statement3; … } 12 Introduction to Boolean Expressions •  The value of a boolean expression is either true or false •  Examples num > 0 num < 20 balance <= 0 time < limit 13 Comparison operators Math English notation = ≠ > ≥ < ≤ Java Java example notation Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to == != > >= < <= grade == 95 answer != 42 input > 0 notNegative >= 0 change < DOLLAR negative <= - 1 Taken from page 145 of the textbook 14 Math is not Java •  Comparison operators –  Binary operators –  Work from left to right –  How does this evaluate? 0 < score <= 100 15 Math is not Java If score is 95… 0 < score <= 100 true <= 100 Does not compute?! 16 Solution Logical operators combine 2 boolean ex...
View Full Document

This note was uploaded on 02/17/2014 for the course ITP 109 taught by Professor Trinagregory during the Spring '12 term at USC.

Ask a homework question - tutors are online