Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez

Similar presentations


Presentation on theme: "COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez"— Presentation transcript:

1 COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez djrg@cise.ufl.edudjrg@cise.ufl.edu http://cise.ufl.edu/~djrg/http://cise.ufl.edu/~djrg/ https://ufcprog2015.wordpress.com/

2 ADMINISTRATIVE STUFF Not so new rule: whenever you participate tell me your name! Reminder: First quiz will be on Friday (5/22). It will cover everything we’ve covered until today! (Just what is actually covered in class, extra slides not necessary) Do we have any runners in the class? This includes: Treadmill Outside Long distance Short distance But no one who uses the ‘j’ word

3 HOMEWORK #2 Pace calculator: Inputs: Race distance (char) (a. 5k, b. 10k, c. 15k, d. Half marathon, e. Full marathon). Distance unit (char) (k for km, m for miles) Expected finish time (3 ints – hours, minutes, seconds) (hh:mm:ss) Output Constant pace required to finish that race in that time (using the selected unit) Due date: Monday June 1 st (11:59pm) Distance conversions and expected format detailed on the homework specification By Friday, we will have all the knowledge required to code this program

4 Aprox 10k

5 ASSIGNMENT AND OPERATIONS Sometimes we want to do a quick operation on a variable and save the result in the variable itself. For example: int a = 8; a = a * 2; // get the double of 8. A quick shortcut for this is: a *= 2; //same as a = a * 2; This also works for other operators!

6 ASSIGNMENT AND OPERATIONS An even more specific common operation that has it’s own operand is: Adding or subtracting 1 from a variable! For example: int a = 8; a = a + 1; // adding 1 Of course we could do: a += 1; // adding 1 However an even easier way is a++; // adding 1 Same story for subtracting 1 (--).

7 MAKING DECISIONS Sometimes we need to make decisions based on inputs and provide different output based on the decision. Examples: Odd/Even program Which one is the larger of two numbers. Letter grade based on numeric grade Others? Homework #2! Decision making in C: if statement switch statement Conditional operator

8 IF STATEMENT if( ) When is true, is executed. What does looks like? We have relational operators! OperatorMeaningExample ==Equal tocount == 10 !=Not equal toa != b <Less thani < 0 <=Less than or equal toi <= 100 >Greater thanpointer > end >=Greater than or equal toj >= 0

9 #include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 == 0) printf("The number is even.\n"); return 0; }

10 #include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 != 0) printf("The number is odd.\n"); return 0; }

11 #include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 == 0) printf("The number is even.\n"); if(num % 2 != 0) printf("The number is odd.\n"); return 0; }

12 ELSE if( ) else Whenever the is true is executed Whenever the is false is executed

13 #include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 == 0) printf("The number is even.\n"); else // much more efficient than if(num % 2 != 0) printf("The number is odd.\n"); return 0; }

14 #include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 == 0) { printf("The number is even.\n"); } else { printf("The number is odd.\n"); } return 0; }

15 #include int main(void) { char a = '\0'; char b = '\0'; printf("Input your first letter"); scanf("%c", &a); printf("Input your second letter"); scanf("%c", &b); if(a < b) { printf("%c comes before %c.\n", a,b); } else if(a == b) { printf("You input %c twice!.\n", a); } else { printf("%c comes before %c.\n", b,a); } return 0; } Why doesn’t this work??

16 (SIDENOTE) GOING DEEPER INTO SCANF Spoiler alert : If you miss this part in class you will likely struggle with HW2. Why didn’t our example work? Scanf doesn’t ignore the newline character that pressing “Enter” generates. The second scanf takes that newline character, so how do we ignore it? How to fix it. There is a function called getchar(). getchar() consumes one character from the console. That includes the newline character!

17 #include int main(void) { char a = '\0'; char b = '\0'; printf("Input your first letter"); scanf("%c", &a); getchar(); printf("Input your second letter"); scanf("%c", &b); getchar(); if(a < b) { printf("%c comes before %c.\n", a,b); } else if(a == b) { printf("You input %c twice!.\n", a); } else { printf("%c comes before %c.\n", b,a); } return 0; }

18 GOING DEEPER INTO SCANF Remember how I said we wouldn’t need to read more than one input per line using scanf ? I lied… or at least I didn’t expect my HW idea to require it. Expected finish time (3 ints – hours, minutes, seconds) (hh:mm:ss) So, how do we do that? Read two or more variables in a particular format in one scanf ? We add more characters in the string and more parameters! (very similar to how we print more than one variable using printf )

19 #include int main(void) { char a = '\0'; char b = '\0'; printf("Input two letters separated by a hyphen: "); scanf("%c-%c", &a, &b); getchar(); //printf("Input your second letter"); // scanf("%c", &b); getchar(); if(a < b) { printf("%c comes before %c.\n", a,b); } else if(a == b) { printf("You input %c twice!.\n", a); } else { printf("%c comes before %c.\n", b,a); } return 0; }

20 ADMINISTRATIVE STUFF We have the first Quiz today! (5/22) It will 11:40am. You are free to leave as soon as you are done. This Monday (5/25) is Memorial Day! UF Holiday! No class Second Quiz will still be (5/29)

21 (BACK TO CONDITIONALS) COMPOUND RELATIONAL TESTS Sometimes we want to check more than just one condition. We already have some of that with = (greater than or equal)

22 COMPOUND RELATIONAL TESTS Sometimes we want to check more than just one condition. We already have some of that with = (greater than or equal) How can we test if a number is in a range? For example if a float grade is between 92 and 88 and is therefore a B+. (Such an strict instructor!) We use the logical AND (&&) and logical OR(||) operators. if( grade >= 88 && grade < 92) printf("You got a B+\n");

23 NESTED IFS Remember an if statement looks like this: if( ) Just happens that the if statement is also a program statement. So we can have things that look like this: if( ) This is what we call a nested if! An if statement inside another one.

24 NESTED IFS When? Why? Let’s go back to: if(grade >= 88 && grade < 92) printf("You got a B+\n"); We could change it for: if(grade >= 88) if(grade < 92) printf("You got a B+\n"); Same result, the printf only happens if grade is both greater than or equal to 88 but less than 92.

25 NESTED IFS if(grade >= 88) if(grade < 92) printf("You got a B+\n"); What happens if I want to do something else? Like assign an A- or A? if(grade >= 88) if(grade < 92) printf("You got a B+\n"); else printf(“Either an A- or A, right?\n”);

26 NESTED IFS Curly brackets/parenthesis are your friends. Use them! The previous ifs are the same as: if(grade >= 88) { if(grade < 92) { printf("You got a B+\n"); } else { printf(“Either an A- or A, for sure!\n”); } }else But just in case, know how C works if they are not there. [Confusing brackets will never be a part of my quizzes]

27 SWITCH STATEMENTS What happens when the variable of interest can take multiple values and we need to do something different for each one? For example, let’s take a simple two operand calculator.

28 #include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //do the calculations here… printf("The result is: %f\n“, result); return 0; }

29 #include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: if(operator == '+') { result = operand1 + operand2; } else if(operator == '-') { result = operand1 + operand2; } else if(operator == '*') { result = operand1 * operand2; } else if(operator == '/') { result = operand1 / operand2; } printf("The result is: %f\n“, result); return 0; }

30 SWITCH STATEMENTS switch( ) { case : break; case : break; … case : break; default: }

31 #include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; } printf("The result is: %f\n“, result); return 0; }

32 #include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: printf(“Unknown operator %c\n“, operator); } printf("The result is: %f\n“, result); return 0; }

33 #include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': case 'x': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: printf(“Unknown operator %c\n“, operator); } printf("The result is: %f\n“, result); return 0; }

34 #include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case 'x': printf(“Warning the recommended operator is * not x\n“); case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: printf(“Unknown operator %c\n“, operator); } printf("The result is: %f\n“, result); return 0; } No break;

35 CONDITIONAL OPERATOR ? : ; This is useful to assign a variable a value for example: float result = operator == ‘+’? operand1 + operand2 : operand1 – operand2; float result = (operator == ‘+’)?(operand1 + operand2) : (operand1 – operand2) ;


Download ppt "COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez"

Similar presentations


Ads by Google