Selection Statements & Exam 1 Review Recitation – 2/13/2009 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
 Control structures  Algorithm & flowchart  If statements  While statements.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Recitation 02/6/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Animated Version.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement a selection control.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Selection Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
Chapter Chapter 5 Selection Statements. Objectives Understand selection control statement –if statements –switch statements Write boolean expressions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
 2005 Pearson Education, Inc. All rights reserved. 1 A class A class is the blueprint from which objects are generated. In other words, if we have six.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Objectives After you have read and studied this chapter, you should be able to Implement a selection control using if statements Implement a.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
Control Statements: Part1  if, if…else, switch 1.
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter The if Statement if ( testScore >= 95 ) { System.out.println("You.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 4: Control Structures I
Decisions Chapter 4.
Chapter 7 Conditional Statements
Chapter 2 Programming Basics.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 6 Selection Statements
Controlling Program Flow
Control Structure.
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

Selection Statements & Exam 1 Review Recitation – 2/13/2009 CS 180 Department of Computer Science, Purdue University

Announcements EXAM Feb 18, Wednesday 6:30-7:30pm MTHW 210 Chapter 0 to 5 20 Multiple choice, 3 programming Project 4 Posted Milestone due Feb 18, 10pm This will be graded! Make sure you submit Final due Feb 25, 10pm 2

3 if ( testScore < 70 ) JOptionPane.showMessageDialog(null, "You did not pass" ); else JOptionPane.showMessageDialog(null, "You passed" ); Syntax for the if Statement if ( ) else Then Block Else Block Boolean Expression Evaluated to either true or false

4 testScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius <= Relational Operators <//less than <=//less than or equal to ==//equal to !=//not equal to >//greater than >=//greater than or equal to

5 if (testScore < 70) { JOptionPane.showMessageDialog(null, "You did not pass“ ); JOptionPane.showMessageDialog(null, “Try harder next time“ ); } else { JOptionPane.showMessageDialog(null, “You did pass“ ); JOptionPane.showMessageDialog(null, “Keep up the good work” ); } Compound Statements Use braces if the or block has multiple statements. Then Block Else Block

6 Control Flow with no else previous statement; next statement; JOptionPane. showMessageDialog(null, "You are an honor student”); true false if (testScore >= 95) JOptionPane.showMessageDialog (null,You are an honor student"); is testScore >=95 ?

7 The Nested-if Statement The then and else block of an if statement can contain any valid statements, including other if statements. An if statement containing another if statement is called a nested-if statement. if (testScore >= 70) { if (studentAge < 10) { System.out.println("You did a great job "); } else { System.out.println("You passed"); //test score >= 70 } //and age >= 10 } else { //test score < 70 System.out.println("You did not pass"); }

8 if – else if Control if (score >= 90) System.out.print("Your grade is A"); else if (score >= 80) System.out.print("Your grade is B"); else if (score >= 70) System.out.print("Your grade is C"); else if (score >= 60) System.out.print("Your grade is D"); else System.out.print("Your grade is F"); Test ScoreGrade 90  score A 80  score  90 B 70  score  80 C 60  score  70 D score  60 F

9 Matching else if (x < y) if (x < z) System.out.print("Hello"); else System.out.print("Good bye"); A A if (x < y) if (x < z) System.out.print("Hello"); else System.out.print("Good bye"); B B Are and different? A A B B if (x < y) { if (x < z) { System.out.print("Hello"); } else { System.out.print("Good bye"); } Both and mean… A A B B Each else paired with nearest unmatched if -- use braces to change this as needed.

10 Boolean Operators &&// AND ||// OR !// Not if (65 <= temp <= 80) System.out.println("Let's walk"); else System.out.println("Let's drive"); if (temp >= 65 && temp <= 80) System.out.println("Let's walk"); else System.out.println("Let's drive"); Boolean expressions

11 Semantics of Boolean Operators Truth table for boolean operators: Sometimes true and false are represented by 1 and 0 (NOT in Java). In C and C++, 0 is false, everything else is true. pqp && qp || q!p false true falsetruefalsetrue false truefalse true false

12 Short-Circuit Evaluation Stop the evaluation once the result of the whole expression is known Example:  Let p = 1, q = 2, r = 3, s = 4 p > q || s > r s > r || p > q p < q && r < s s < r && p < q Not evaluated

13 Simple switch statement previous statement; next statement; x=10; true false switch ( N ) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } N ==1? N ==2? x=20; N ==3? false true x=30; Must be constant!

14 switch with break, and default previous statement; next statement; x=10; true false N ==1? N ==2? false true switch ( N ) { case 1: x = 10; break; case 2: x = 20; case 3: x = 30; break; default: x = 0; } break; x=0; N ==3? x=30; x=20;

15 Missing case body previous statement; next statement; x=10; true false N ==1? true switch ( N ) { case 1: x = 10; break; case 2: case 3: x = 30; break; default: x = 0; } break; false true false break; x=0; N ==3? x=30; N ==2?

If and Switch Example Write a program that print “A” if the input (x) is 10, 20, or 30. Print “B” if the input is 40, print C if otherwise 16 if(x == 10 || x == 20 || x == 30) System.out.println(“A”); else if(x == 40) System.out.println(“B”); else System.out.println(“C”); Switch(x) { case 10: case 20: case 30: System.out.println(“A”); break; case 40: System.out.println(“B”); break; default: System.out.println(“C”); } Optional, why?

String String a = “Hello World”; String b = new String(“Hello World”); a.subString(1, 5)  “ello” (Extract from i to j-1) a.indexOf(“Wor”)  6 (Zero-based index) a.indexOf(“Ello”)  -1 (Case sensitive) a.length()  11 a.subString(1, 5) + a.subString(6, 9)  “elloWor” a.subString(1, 5)  “ello89” a.subString(1, 5) + (8 + 9)  “ello17” 17

Input and Output System.out.println(“Study for Exam”); System.out.print(“Study for Exam”); JOptionPane.showMessageDialog(null, “Studying”); Scanner s = new Scanner(System.in); s.next();  Read the next word JOptionPane.showInputDialog(null, “Your name?”); 18 Specify where to read from

Primitive Types and Object Primitive Types int, long, double, float, … Do not need to call new to allocate space Can assign a value after declaration Example: int x = 5; Reference Data Types String, Maze, Calculator, … The variable refers to the object allocated by new operator Must be created using new before manipulation otherwise it is null Example: Maze m = new Maze(“Name”); 19 X = 5 m = Maze object

20 Facts about Primitive Types

Generating Random Numbers Generate a random integer between A & B (int)(Math.random() * (B – A + 1)) + A 21 [0, 1) [0, B-A+1) [0, B-A] [A, B]

Generating Random Numbers Generate a random real number between A & B Math.random() * (B – A) + A 22 [0, 1) [0, B-A) [A, B)

Conventional Class Definition Structure 23

import java.util.Date; /** * Book -- a book that knows only its name * Henry David Thoreau **/ class Book { private String name; private Date dateMade; public Book( ) { name = “I have no name”; dateMade = new Date(); } public String getName( ) { return name; } public void setName(String newName) { name = newName; } 24 What Your Class Would Like Import Statement Comments Class Name Data Member Constructor Methods

Solving Problem! Write a function to compute the probability of a student getting a x points in the exam. The input parameters are x, μ (class average), σ (variance). The probability function is given by 25 public double Probability(double x, double mu, double sigma) { double k = 1.0 / sigma / Math.sqrt(2 * Math.PI); return k * Math.exp(( -(x-mu)*(x-mu)) / 2 / sigma / sigma); }

Quiz Java is a machine independent language. True or false? Any switch statement can be written using if statements. True or false? 26