Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems.

Similar presentations


Presentation on theme: "Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems."— Presentation transcript:

1 Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems

2 Previous Examples of Statements Declaration with initialization String name = “Obama”; String name = “Obama”; Declaration (without initialization) int age; int age; Declaration of multiple variables double price, taxamount, result; double price, taxamount, result; Assignment statements price = 20.55; price = 20.55; Variable type Variable name (no space, start With alphabet, case sensitive) Variable value (optional) (optional)

3 More Assignment Statements pricewithtax = price * 1.0825;The value on the right-hand-side of an assignment statement can be computed with an arithmetic expression pricewithtax = price * 1.0825; This can include other variables or even the same variable price = 5; price = price + 10.0; 3 Move that value to be the new value of the variable First compute the value

4 Problem with different possible outputs (for the same input) Input Output 1 Output 2 Example: Age > 20? Request Wine Yes, Great! No, you’re not old enough No, you’re not old enough

5 Conditional Statement Conditional statement can decide different actions based on a decision: If-then-else statement If (condition) { [some actions] } else { [some different actions]}

6 Example 1 If your age > 20, serve you wine; else, serve you soda… int age; String drink; age = ??; if (age > 20) { drink = “wine”; } else { drink = “soda”;}System.out.println(drink);

7 Practice 1 Shopping problem. If you purchase $50 or more, you get 10% off for your total bill; otherwise, you get 5% discount.

8 Example 2 If you have data plan, your monthly payment is 59.99; if you don’t have data plan, you only pay 39.99 boolean dataplan; double payment; dataplan = true; //true or false if (dataplan) { payment = 59.99; } else { payment = 39.99;}

9 Multiple conditions If you are VIP user, and you purchased over $100, you get 15% discount boolean VIP; double payment; //VIP and payment input if ( VIP && payment > 100.0) { payment = payment – (payment * 0.15); }

10 Another Shopping problem. Suppose that there is a String variable called PayStatus that is either “Hourly” or “Salaried”, and that we want to compute the Pay of an employee using a variable Hours that tells how many hours they worked that week, and the variable PayRate that tells how much per hour that they make. Salaried employees are always paid as if they worked 40 hours, no matter how many hours they actually worked. If hourly employees work more than 40 hours in a week, then they get 1.5 times their pay rate for any hours over 40.


Download ppt "Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems."

Similar presentations


Ads by Google