csci 144 introduction to computer science
play

CSCI 144 - Introduction to Computer Science Instructor: John - PowerPoint PPT Presentation

1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Fall 2017 PLU Computer Science Before class Quiz on Monday Turn-in Buggy codes Solutions to Buggy


  1. 1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Fall 2017 PLU Computer Science

  2. Before class • Quiz on Monday • Turn-in Buggy codes • Solutions to Buggy codes • CAT QA PLU Computer Science

  3. Boolean Expressions and If Statements PLU Computer Science

  4. Sequential Execution Execution System.out.print("How many credit hours have you taken? "); flow creditHours = scan.nextInt(); System.out.print("How many grade points have you earned? "); gradePoints = scan.nextDouble(); gpa = gradePoints / creditHours; hoursLeft = HOURS_NEEDED - creditHours; System.out.printf("Your GPA is %.2f\n", gpa); System.out.printf("You need %d more hours", hoursLeft); System.out.println(" to complete your degree."); PLU Computer Science 4

  5. Java code Conditional Execution true Is it cold if( coldOutside ) outside? wearCoat(); Wear a coat. false PLU Computer Science 5

  6. if( coldOutside ) { true Is it cold wearCoat(); outside? wearHat(); wearGloves(); Wear a coat. } false Wear a hat. Wear gloves. Curly braces define a block of statements. PLU Computer Science 6

  7. A boolean expression is something that evaluates to a boolean value (true or false) true Boolean Expression? Statement or if( boolean expression ) block false statement or block PLU Computer Science 7

  8. Think/Pair/Share Write down at least two instances where you might have used a conditional in one of your previous lab assignments or pair exercises. PLU Computer Science

  9. Relational Operators PLU Computer Science

  10. Boolean Operators Operator Meaning > is greater than < is less than >= is greater than or equal to <= is less than or equal to == is equal to != is not equal to PLU Computer Science 10

  11. Boolean Expressions Expression Meaning x > y Is x greater than y? x < y Is x less than y? x >= y Is x greater than or equal to y? x <= y Is x less than or equal to y? x == y Is x equal to y? x != y Is x not equal to y? PLU Computer Science 11

  12. GPA Example with Conditional Execution System.out.print("How many credit hours have you taken? "); creditHours = scan.nextInt(); System.out.print("How many grade points have you earned? "); gradePoints = scan.nextDouble(); gpa = gradePoints / creditHours; Note the use of indentation hoursLeft = HOURS_NEEDED - creditHours; and curly braces. System.out.printf("Your GPA is %.2f\n", gpa); if( gpa >= 3.7 ) System.out.println("Wow, you're a hard worker!"); System.out.printf("You need %d more hours to complete your degree.\n", hoursLeft); if( hoursLeft < 0 ) { System.out.println("You can graduate!"); System.out.println("See your advisor to get graduation clearance!"); } PLU Computer Science 12

  13. System.out.print("How many credit hours have you taken? "); creditHours = scan.nextInt(); System.out.print("How many grade points have you earned? "); gradePoints = scan.nextDouble(); gpa = gradePoints / creditHours; hoursLeft = HOURS_NEEDED - creditHours; System.out.printf("Your GPA is %.2f\n", gpa); if( gpa >= 3.7 ) System.out.println("Wow, you're a hard worker!"); System.out.printf("You need %d more hours to complete your degree.\n", hoursLeft); if( hoursLeft < 0 ) System.out.println("You can graduate!"); System.out.println("See your advisor to get graduation clearance!"); Indentation alone does not define a block. What does this do when hoursLeft == 20? PLU Computer Science 13

  14. if( coldOutside ) { wearCoat(); } else { wearShorts(); false true } Is it cold outside? Wear shorts. Wear a coat. In general if( boolean expression ) statement or block else statement or block The " else " part is optional PLU Computer Science 14

  15. if-else System.out.print("How many credit hours have you taken? "); creditHours = scan.nextInt(); System.out.print("How many grade points have you earned? "); gradePoints = scan.nextDouble(); if-else avoids division by zero and computing with invalid if( creditHours > 0 ) (negative) input. { gpa = gradePoints / creditHours; } else { gpa = 0.0; } System.out.printf("Your GPA is %.2f\n", gpa); PLU Computer Science 15

  16. Practice Write an if statement to calculate quizAve ( sum divided by numQuizzes ) only if numQuizzes is greater than 0. PLU Computer Science 16

  17. Practice Write an if statement to calculate quizAve ( sum divided by numQuizzes ) only if numQuizzes is greater than 0. if( numQuizzes > 0 ) { quizAve = sum / numQuizzes; } PLU Computer Science 17

  18. Practice Write an if statement to calculate tax ( totalSales times TAX_RATE ) only if forProfit ( char ) is 'y'. PLU Computer Science 18

  19. Practice Write an if statement to calculate tax ( totalSales times TAX_RATE ) only if forProfit ( char ) is 'y'. if( forProfit == 'y' ) { tax = totalSales * TAX_RATE; } PLU Computer Science 19

  20. Practice Write an if-else statement to print "Go Swimming!" and set cost to $5 if the temperature ( temp ) is at least 80 degrees, otherwise print "Take a hike" and set cost to $0. PLU Computer Science 20

  21. Practice Write an if-else statement to print "Go Swimming!" and set cost to $5 if the temperature ( temp ) is at least 80 degrees, otherwise print "Take a hike" and set cost to $0. if( temp >= 80 ) { System.out.println("Go Swimming!"); cost = 5.0; } else { System.out.println("Take a hike"); cost = 0.0; } PLU Computer Science 21

  22. Practice: IfPractice.java Check the class website and Turn-in on next Monday! PLU Computer Science 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend