1 / 27

CS 108 Computing Fundamentals Notes for Thursday, February 13, 2014

CS 108 Computing Fundamentals Notes for Thursday, February 13, 2014. switch … case Statement. Simple if has only one action part, and if … else statement has only two action parts... else if can be messy.

artan
Download Presentation

CS 108 Computing Fundamentals Notes for Thursday, February 13, 2014

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 108 Computing Fundamentals Notes for Thursday, February 13, 2014

  2. switch … case Statement. • Simple if has only one action part, and if … else statement has only two action parts... else if can be messy. • Often in life three or more cases of actions can be used, based on the condition. • Use either nested else if statements or use a switch … case statement

  3. switch … case Statement. switch (decision criteria or condition) { case Label_1 : Action Block 1; // decision criteria must be break; // data type int only case Label2 : // Labels must be integer Action Block 2; // constants only… if you break; // remember the ASCII … // table then you'll know that default : // catch-all // characters are included Action Block else; break; }

  4. switch … case Statement.

  5. switch … caseExample • Complete example at this link: http://web.cs.sunyit.edu/~urbanc/cs_108_feb_11_8.txt

  6. Relational Operators OperatorMeaning = = Equal to ! = Not Equal to < Less Then < = Less Then or Equal to > Greater Then > = Greater Then or Equal to • Note difference between = and ==

  7. Relational Operators Example : if ( choice = 2) { answer = perimeter_cal (base, height); printf(“ The perimeter is %f”, answer ); } Note the condition above is incorrect… compare: if ( choice = = 2) { answer = perimeter_cal (base, height); printf(“ The perimeter is %f”, answer ); } The variable choice is assigned the value of 2... 2 is not 0 therefore this condition is evaluated as TRUE (never FALSE)

  8. Relational Operator Demo Let’s develop a short program that prompts the user to enter two numbers, and once the numbers are entered the program determines (tests) which of the two numbers is larger. After the determination is made (as to which is larger), a message is printed to the screen indicating the result of the test. Start with an algorithm!!

  9. Prompt user to enter a number (number_1) • Read user's input for number_1 • Prompt user to enter a number (number_2) • Read user's input for number_2 • Test to see if the the numbers are unequal • If test is true • Test to see if number_1 is greater than number_2 • If test is true • Inform the user that number_1 is greater than number_2 • Go to step 7. • If test is false (else) • Inform the user that number_2 is greater than number _1 • Go to step 7. • If test is false (else) • Inform the user that number_1 and number_2 are equal • Terminate • Now Lets Code It!!

  10. Relational Operator Demo #include <stdio.h> // My file 3.c… also demonstrates “nested” int main (void) // if statements { float num1, num2; printf("\n Enter a number: "); scanf("%f", &num1); printf("\n Enter a second number: "); scanf("%f", &num2); if (num1 != num2) { if (num1 > num2) printf("\n The first number s greater than the second.\n\n\n"); else printf("\n The second number is greater than the first.\n\n\n"); } else printf("\n The numbers you entered are equal.\n\n\n"); return 0; }

  11. Relational Operator Demo #include <stdio.h> // My file 3a.c… condition evaluation issues int main (void) { float num1, num2; printf("\n Enter a number: "); scanf("%f", &num1); printf("\n Enter a second number: "); scanf("%f", &num2); if ( 0 ) // After running add a ! before ( 0 ) and 0 { // and rerun if (num1 > num2) printf("\n The first number s greater than the second.\n\n\n"); else printf("\n The second number is greater than the first.\n\n\n"); } else printf("\n The numbers you entered are equal.\n\n\n"); return 0; }

  12. Relational Operator Demo #include <stdio.h> // My file 3b.c… demonstration of // condition evaluation resultant values int main (void) { float answer; answer = 1 < 4 && 4 < 7 ; printf("\n\n Answer: %f " , answer ) ; answer = ! (1 < 3) || ( 2 < 4 ) ; printf("\n\n Answer: %f " , answer ) ; answer = ! ( (1 < 3) || ( 2 < 4 ) ) ; printf("\n\n Answer: %f \n\n\n" , answer ) ; answer = ! (372) ; printf("\n\n Answer: %f \n\n\n" , answer ) ; return 0; }

  13. Relational Operator Demo #include <stdio.h> // My file 3c.c… demonstration of // condition evaluation resultant values int main (void) // using data type int { // The result of all relational and Boolean int answer; // logical operations is always an int answer = 1 < 4 && 4 < 7 ; printf("\n\n Answer: %d " , answer ) ; answer = ! (1 < 3) || ( 2 < 4 ) ; printf("\n\n Answer: %d " , answer ) ; answer = ! ( (1 < 3) || ( 2 < 4 ) ) ; printf("\n\n Answer: %d " , answer ) ; answer = ! (372) ; printf("\n\n Answer: %d \n\n\n" , answer ) ; return 0; }

  14. Logical Operators Operator Meaning ! NOT (Highest Priority) && AND ||OR (Lowest Priority) Condition A ! (NOT)Condition A Condition B &&(AND) ||(OR) true false true true true true false true true false false true false true false true false false false false

  15. Compound Logical Expressions Write an if statement that doubles a number if the number is greater than 0 yet does not exceed 25, triples the number if it’s greater than 25 but less than 50, and assigns the value 0 if the previous conditions aren’t met. Hint: use the if … else if … else construction

  16. Compound Logical Expressions if ( (entry > 0) && (entry <= 25) ) { entry = entry * 2; } else if ( (entry > 25) && (entry < 50) ) { entry = entry * 3; { else { entry = 0; }

  17. Compound Logical Expression Demo Let’s develop a program that transforms a numeric grades entered by the user into a letter grade and then displays that letter grade to the screen. What do we do first?

  18. Prompt user to enter a test score • Read user's input number for test score • Test the test score to see if it is less than 65 • If test is true • Display a message indicating the grade earned is F • Go to step 8 • Test the test score to see if it is greater than or equal to 65 but less than 70 • If test is true • Display a message indicating the grade earned is D • Go to step 8 • Test the test score to see if it is greater than or equal to 70 but less than 80 • If test is true • Display a message indicating the grade earned is C • Go to step 8 • Test the test score to see if it is greater than or equal to 80 but less than 90 • If test is true • Display a message indicating the grade earned is B • Go to step 8 • Display a message indicating the grade earned is A • Terminate • Now Lets Code It!!

  19. Compound Logical Expression Demo #include <stdio.h> //My file 4.c int main (void) { float score; printf("\n Enter your test score :"); scanf("%f",&score); if (score < 65) { printf("\n You earned an F.\n\n"); // Braces } else if ( (score >= 65) && (score < 70) ) // No Braces… why printf("\n You earned a D.\n\n"); else if ( (score >= 70) && (score < 80) ) printf("\n You earned a C.\n\n"); else if ( (score >= 80) && (score < 90) ) printf("\n You earned a B.\n\n"); else printf("\n You earned an A.\n\n"); return 0; }

  20. Let’s Use a Programmer-Created Function #include <stdio.h> // My file 5.c char calc_grade (float); // Function prototype int main(void) { float score; char grade; printf("\n Enter your test score: ") ; scanf("%f",&score); grade = calc_grade (score) ; //Function call printf("\n\nYou received a letter grade of %c. \n\n", grade); return ( 0 ); } /* Continued on next slide… read as one contiguous file */

  21. /* Continued from previous slide… read as one contiguous file */ char calc_grade (float in_score) // Function declaration { char out_grade = ' '; // Local variable initialized to if (in_score <65) // a "space" (see ASCII table) out_grade = 'F' ; else if ((in_score >= 65) && (in_score < 70)) out_grade = 'D' ; else if ((in_score >= 70) && (in_score < 80)) out_grade = 'C' ; else if ((in_score >= 80) && (in_score < 90)) out_grade = 'B' ; else out_grade = 'A' ; return ( out_grade ); }

  22. Let’s Use a Programmer-Created Function /* The action of the previous slide accomplished differently */ if (in_score <65) // Notice multiple return statements return ( 'F' ); else if ((in_score >= 65) && (in_score < 70)) return ( 'D' ); else if ((in_score >= 70) && (in_score < 80)) return ( 'C' ); else if ((in_score >= 80) && (in_score < 90)) return ( 'B' ); else //Only one return statement can execute return ( 'A' ); }

  23. Let’s See a switch Statement Example #include <stdio.h> /* Let’s calculate a worker’s pay… 6.c*/ int main( void ) { int status = 0; double hours = 0.0; double pay = 0.0; printf("\n Enter the hours worked :"); scanf("%lf",&hours); printf("\n Enter the status (7 for full-timers and 9 for part-timers): "); scanf("%d",&status); switch (status) { case 7: pay = hours * 15.0; // full-time rate is $15 per hour break; case 9: pay = hours * 10.0; // part-time rate is $10 per hour break; default: printf("\n\n\n >>>Wrong Employment Status<<<\n"); break; } printf("\n The payment amount is: %4.2lf\n\n\n", pay); return ( 0 ); }

  24. Remember: Steps to Build a Program • What does it need to do (functionality): algorithm • Can it be broken down into simpler pieces • Code the simpler pieces • Code and test one piece at a time • Integrate, test and fix • Finished • Start with a complete algorithm before writing one line of code • Comment as you go • "Urban Inside-Out Method"…one step at a time

  25. Pico Shortcuts • ctrl-w followed by ctrl-t produces a prompt that asks you on which line you like the cursor to be positioned • ctrl-c produces the number of the line on which the cursor is currently positioned • ctrl-y moves the cursor up one page • ctrl-v moves the cursor down one page

  26. Test #1 • Tuesday, February 18, 2014 • Chapters 1, 2, 3, 4 and UNIX only for closed-book part… up through and including programmer-created functions (PCFs) and GHP #4 for open-book part • Use “Quick Check Exercises” and “Review Questions” • I will e-mail everyone a copy of last semester's exam #1 • Use Online Interactive Tests at the following link to prepare: http://ftp.tuwien.ac.at/languages/c/programming-bbrown/onlinet.htm

  27. GHP #5 • Will be posted by 5 PM tomorrow • Due by 8 AM on Friday, February 21st for section 01 • Due by 11:59 PM on Friday, February 21st for section 35W • This gives everyone plenty of time to study for the exam and then complete GHP #5 after the exam. • This also allows folks time to catch-up and get GHP #4 complete prior to the exam • If you do GHP #4 yourself and you understand it, then the Open-Book portion of the exam will be a piece of cake

More Related