بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement a selection control.

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to –Improve the reliability.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Selection Statements & Exam 1 Review Recitation – 2/13/2009 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.
©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.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Animated Version.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Selection Statements.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement repetition control.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
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 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Flow of Control Part 1: Selection
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
SE-1020 Dr. Mark L. Hornick 1 The switch statement int gradeLevel = kbd.nextInt(); // can be byte or short also, but not long switch( gradeLevel ) { //
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Most of these slides are based on “Intro to.
 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.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
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: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
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.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.
© 2000 McGraw-HillIntroduction to Object-Oriented Programming with Java--WuChapter The if Statement if ( testScore >= 95 ) { System.out.println("You.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
Intro to OOP with Java, C. Thomas Wu Selection Statements
Java Review Most of these slides are based on
Control Structures – Selection
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Java Review Most of these slides are based on
Chapter 6 Selection Statements
Control Structure.
Presentation transcript:

بسم الله الرحمن الرحيم CPCS203: Programming II

Objectives After you have read and studied this chapter, you should be able to Implement a selection control using if statements Implement a selection control using switch statements Write boolean expressions using relational and boolean expressions Evaluate given boolean expressions correctly Nest an if statement inside another if statement Describe how objects are compared Choose the appropriate selection control statement for a given task

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter The if Statement int testScore; testScore = //get test score input if (testScore < 70) JOptionPane.showMessageDialog(null, "You did not pass" ); else JOptionPane.showMessageDialog(null, "You did pass" ); This statement is executed if the testScore is less than 70. This statement is executed if the testScore is 70 or higher.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter if ( testScore < 70 ) JOptionPane.showMessageDialog(null, "You did not pass" ); else JOptionPane.showMessageDialog(null, "You did pass " ); Syntax for the if Statement if ( ) else Then Block Else Block Boolean Expression

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Control Flow JOptionPane. showMessageDialog (null, "You did pass"); JOptionPane. showMessageDialog (null, "You did pass"); false JOptionPane. showMessageDialog (null, "You did not pass"); JOptionPane. showMessageDialog (null, "You did not pass"); true testScore < 70 ?

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter 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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter 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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter if ( ) { … } else { … } Style Guide if ( ) { … } else { … } Style 1 Style 2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter The if-then Statement if ( ) if ( testScore >= 95 ) JOptionPane.showMessageDialog(null, "You are an honor student"); Then Block Boolean Expression

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Control Flow of if-then false JOptionPane. showMessageDialog (null, "You are an honor student"); JOptionPane. showMessageDialog (null, "You are an honor student"); true testScore >= 95?

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Control Flow of if-then print “Passed” Grade >= 60 true false Grade >= 60 Condition Grade < 60

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter The Nested-if Statement The then and else blocks 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 did pass"); //test score >= 70 } //and age >= 10 } else { //test score < 70 System.out.println("You did not pass"); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Control Flow of Nested-if Statement messageBox.show ("You did not pass"); messageBox.show ("You did not pass"); false inner if messageBox.show ("You did pass"); messageBox.show ("You did pass"); false true messageBox.show ("You did a great job"); messageBox.show ("You did a great job"); true testScore >= 70 ? studentAg e< 10?

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Writing a Proper if Control if (num1 < 0) if (num2 < 0) if (num3 < 0) negativeCount = 3; else negativeCount = 2; else if (num3 < 0) negativeCount = 2; else negativeCount = 1; else if (num2 < 0) if (num3 < 0) negativeCount = 2; else negativeCount = 1; else if (num3 < 0) negativeCount = 1; else negativeCount = 0; if (num1 < 0) negativeCount++; if (num2 < 0) negativeCount++; if (num3 < 0) negativeCount++; The statement: negativeCount++; negativeCount = negativeCount + 1; increments the variable by one The statement: negativeCount++; negativeCount = negativeCount + 1; increments the variable by one

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter 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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter 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 means… A A B B

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Boolean Operators A boolean operator takes boolean values as its operands and returns a boolean value. The three boolean operators are –and:&& –or:|| –not :! if (temperature >= 20 && distanceToDestination < 2) { System.out.println("Let's walk"); } else { System.out.println("Let's drive"); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Semantics of Boolean Operators Boolean operators and their meanings: PQP && QP || Q!P false true falsetruefalsetrue false truefalse true false

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter De Morgan's Law De Morgan's Law allows us to rewrite boolean expressions in different ways Rule 1: !(P && Q)  !P || !Q Rule 2: !(P || Q)  !P && !Q !(temp >= 65 && dist < 2)  !(temp >=65) || !(dist < 2) by Rule 1  (temp = 2)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Short-Circuit Evaluation Consider the following boolean expression: x > y || x > z Expression is evaluated left to right. If x > y is true, then no need to evaluate x > z because the whole expression will be true whether x > z is true or not. To stop the evaluation once result of expression is known is called short-circuit evaluation. What would happen if the short-circuit evaluation is not done for the following expression? z == 0 || x / z > 20 divide by zero error

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Operator Precedence Rules

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Boolean Variables The result of a boolean expression is either true or false. These are the two values of data type boolean. We can declare a variable of data type boolean and assign a boolean value to it. boolean pass, done; pass = 70 < x; done = true; if (pass) { … } else { … }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Boolean Methods A method that returns a boolean value, such as private boolean isValid(int value) { if (value < MAX_ALLOWED) return true; } else { return false; } Can be used as if (isValid(30)) { … } else { … }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Comparing Objects With primitive data types, we have only one way to compare them, but with objects (reference data type), we have two ways to compare them. 1. We can test whether two variables point to the same object (use ==), or 2.We can test whether two distinct objects have the same contents.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Using == With Objects (Sample 1) String str1 = new String("Java"); String str2 = new String("Java"); if (str1 == str2) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } They are not equal Not equal because str1 and str2 point to different String objects.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Using == With Objects (Sample 2) String str1 = new String("Java"); String str2 = str1; if (str1 == str2) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } They are equal It's equal here because str1 and str2 point to the same object.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Using equals with String String str1 = new String("Java"); String str2 = new String("Java"); if (str1.equals(str2)) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } They are equal It's equal here because str1 and str2 have the same sequence of characters. equals and equalsignoreCase are comparison methods in the String class

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter The Semantics of ==

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter In Creating String Objects

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter The switch Statement int gradeLevel; gradeLevel = JOptionPane.showInputDialog("Grade (Frosh-1,Soph-2,...):" ); switch (gradeLevel) { case 1: System.out.print("Go to the Gymnasium"); break; case 2: System.out.print("Go to the Science Auditorium"); break; case 3: System.out.print("Go to Harris Hall Rm A3"); break; case 4: System.out.print("Go to Bolt Hall Rm 101"); break; } This statement is executed if the gradeLevel is equal to 1. This statement is executed if the gradeLevel is equal to 4.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Syntax for the switch Statement switch ( gradeLevel ) { case 1: System.out.print("Go to the Gymnasium"); break; case 2: System.out.print("Go to the Science Auditorium"); break; case 3: System.out.print("Go to Harris Hall Rm A3"); break; case 4: System.out.print("Go to Bolt Hall Rm 101"); break; } switch ( ) { : … : } Case Body Arithmetic Expression Case Label

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter switch With No break Statements switch ( N ) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } x = 10; false true x = 20; x = 30; false true N == 1 ? N == 3 ? N == 2 ?

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter switch With break Statements switch ( N ) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x = 10; false true x = 20; x = 30; false true break; N == 1 ? N == 3 ? N == 2 ?

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter switch With the default Block switch (ranking) { case 10: case 9: case 8: System.out.print("Master"); break; case 7: case 6: System.out.print("Journeyman"); break; case 5: case 4: System.out.print("Apprentice"); break; default: System.out.print("Input error: Invalid Data"); break; }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Drawing Graphics Chapter 5 introduces four standard classes related to drawing geometric shapes. They are –java.awt.Graphics –java.awt.Color –java.awt.Point –java.awt.Dimension

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter Sample Drawing

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr. Abdullah Basuhail,CS, FCIT,KAU,1431H 4 th Ed Chapter The Effect of drawRect