Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection 03/7/15. Programs Get help if you are not getting homework. –It's 40% of your grade.

Similar presentations


Presentation on theme: "Selection 03/7/15. Programs Get help if you are not getting homework. –It's 40% of your grade."— Presentation transcript:

1 Selection 03/7/15

2 Programs Get help if you are not getting homework. –It's 40% of your grade

3 Selection Lets us make decisions in a program. – e.g. "Shall I buy this jacket?" If statement is used for selection. if(price < 100.0)‏ printf(“Buy the jacket.\n”);

4 Relational Expression Is either true or false. Used in if expression. –(price < 100.0)‏

5 Relational Operators CMath<> === <=≤ >=≥ !=≠

6 Examples int n = 5, p = 10; //true or false? (n < 5)‏ (p >= 10)‏ (p + n <= 12)‏

7 Character Comparison ‘A’ < ‘B’ < ‘C’ … < ‘Z’ ‘a’< ‘b’ < ‘c’ … < ‘z’ ‘0’ < ‘1’ < ‘2’ … < ‘9’ True or False? ‘a’ > ‘w’ ‘W’ < ‘Z’ ‘Y’ == ‘y ‘8’ >= ‘4’

8 Participation int x = 4, y = 8; //true or false? (x != y) (x + 4 == y) (24 > x * y) 'b' == 'B' 'e' > 'i'

9 Write Relational Expressions For: ➲ The high today is below 70 ➲ The average on an exam was 88 ➲ The month, given as an integer, is in the 1 st quarter of the year.

10

11 if statement Simple if in C Syntax: (indentation is good style) if(expression)‏ statement; Statement executed only when expression is true. No ";" on the if line. ( ) are required.

12 if statement Example: int age = 19; if(age >= 18)‏ printf(“Vote on November 4th\n”);

13 if Example Program gasif.c

14 Compound Statement When you want to do more than one statement when the condition is true. Inside { } if (expression)‏ { statement 1; statement 2;. statement n; }

15 Example freeShip.c

16 Write a Program Write a program let the user input the time it takes to drive from their home to a prospective job. If the driving time is more than 30 min., output “too far”. No matter the outcome, output “Good Luck!”. Algorithm? Started in jobDrive.c

17 Programming Style Indentation –Indent statements between { } Spacing –Use blank lines to break up the program into logical parts. Comments –Name, Date, Description of program Meaningful identifiers –“time” instead of “x” I will put some comments in jobDrive.c

18 Questions ➲ Give an example of a relation operator in C. ➲ Is indentation required in the syntax of an if statement? ➲ What is wrong with this piece of a C program? scanf("%d", count); if count >= 5 ; printf("Basketball team.\n");

19 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."


Download ppt "Selection 03/7/15. Programs Get help if you are not getting homework. –It's 40% of your grade."

Similar presentations


Ads by Google