Presentation is loading. Please wait.

Presentation is loading. Please wait.

If-else 03/13/15. Program 6 Due 4/3/14 See the 115 website.

Similar presentations


Presentation on theme: "If-else 03/13/15. Program 6 Due 4/3/14 See the 115 website."— Presentation transcript:

1 if-else 03/13/15

2 Program 6 Due 4/3/14 See the 115 website.

3 if - else Example: –Want a program to ask the user for savings and extra income –If the total of savings and extra income is over $2000 the program will print a message that there is enough for a cruise. –Otherwise it will print that there is not enough and tell them the amount still needed. –ifelse.c

4 if - else Format: if(expression)‏ statement1; //expression true else statement2; //expression false callVoter.c

5 Careful with == vs. = Common Error to mix signs. Program Compiles –if (angle = 90.0)‏ Angle assigned value of 90.0 Expression evaluated to 90.0 Non-Zero is equivalent to true Zero is false angle.c

6 Careful with == vs. = Some programmers write all equal comparison with variable on right to avoid this problem. if(90.0 == angle)‏ if(90.0 = angle) //Compile Error

7 if-else Chain 3/21/14

8 Nested-If An if can be a statement within an if statement. Example: In a program a gameIsOver value of 1 indicates it is over, 0 indicates it is not over –If the game is not over, if it's your turn to play, it asks you to move. –This takes an "if" within an "if" game.c

9 Nested-If Another example. – game2.c Intent is to have it say “game over” when gameIsOver is 1, or not 0. Indentation doesn't show how the compiler sees it. else paired with the last "if" without an else.

10 if-else Chain if statement can be a statement within another if. –In either if or else Nested-if can be confusing. Else if chain easier to follow. –Series of ifs in the else clauses.

11 if-else Chain Format: if(expression1)‏ statement1; else if(expression2)‏ statement2; else statement3;

12 if-else Chain Formatted with Different Spacing: if(expression1)‏ statement1; else if(expression2)‏ statement2; else statement3;

13 Example A program checks to see if an angle is right, obtuse or acute. –angle2.c

14 Potluck Program Potluck Program Friends had a barbecue potluck and assigned dishes according to last name. –A-H – Salad –I-P – Side Dish –P-Z – Dessert Write a program to ask for the first letter of a last name and outputs the dish to bring. Program is started in potluck.c

15 Participation ➲ Write a program to ask the user for the date. If 15 is input output a message that says, "Beware of the Ides of March." Otherwise output "March Winds."

16 Participation: Stock Program I bought a stock at $30.00. I want program that asks for today's price. If the stock's price is $10.00 above the purchased price the program will say “Sell!”. If the price is more than $10.00 below the price should output “Buy more”. If the price is within $10.00 of the purchased price it should say “Hold.” –Program is started in stock.c


Download ppt "If-else 03/13/15. Program 6 Due 4/3/14 See the 115 website."

Similar presentations


Ads by Google