Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logic Structures. If… else statements Meaning if the condition is true, execute this command. If the condition is false, execute the else command.

Similar presentations


Presentation on theme: "Logic Structures. If… else statements Meaning if the condition is true, execute this command. If the condition is false, execute the else command."— Presentation transcript:

1 Logic Structures

2 If… else statements Meaning if the condition is true, execute this command. If the condition is false, execute the else command

3 if (condition(s)) { TRUE instructions; } else { FALSE instructions; }

4 If/Else Statements

5 Example Write a decision instruction to indicate if a given integer number is an even number

6 Example A retail store allows part-time workers a rate of $8.00 an hour for a maximum of 20 hours of work in a week. However, a part-time worker earns $10.00 an hour for each additional hour over 20 hours. Write a decision instruction to compute a given part- time worker’s wage for a week.

7 Straight if Instructions If(condition1) { instructions1; } If(condition2) { instruction2; } etc

8 Nested If statments Id statements inside of if statements

9 Nested if loops if(conditon1) { if(condition2) { blah=blah+blah; } else { yadda=yadda+yadda; } else { thing=thing1+thing2; }

10 Logic Calculate the number of bonus air miles earned given that the bonus air miles earned by customers is 100 if traveled miles exceed 5000 in a period of time, but 60 bonus air miles are earned if traveled miles only exceed 3000 while 10 bonus air miles are earned otherwise by the customer.

11 Switch Cases Used to execute an instruction for a given number of cases.

12 Switch switch (EXPRESSION) { case label1:instructions to execute if expression = label1; break; case label2: instructions to execute if expression = label2; break; : case labeln: instructions to execute if expression = labeln; break;

13 Switch Write a program that keeps track of the number of houses in each of the five zones labeled A, C, K, L, Q in a city. It reads the zone of a given house, increments the appropriate zone count and prints the number of houses in each zone.

14 Assignment Pg 154 #’s 1-5


Download ppt "Logic Structures. If… else statements Meaning if the condition is true, execute this command. If the condition is false, execute the else command."

Similar presentations


Ads by Google