CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
©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.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement a selection control.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Logical Operators and Conditional statements
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Decision Structures and Boolean Logic
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
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.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
Control statements Mostafa Abdallah
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
A: A: double “4” A: “34” 4.
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.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Relational Operator and Operations
Chapter 4: Control Structures I
OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS
Selections Java.
Lecture 3- Decision Structures
Flow of Control.
Review Operation Bingo
Chapter 4: Control Structures I
Flow of Control.
Topics The if Statement The if-else Statement Comparing Strings
Flow of Control.
CS139 October 11, 2004.
Control Structure Chapter 3.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Control Structure.
Presentation transcript:

CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions

CS-1010 Dr. Mark L. Hornick 2 Instructions in a Java program are executed in sequence Starting with the first instruction in your main() method and continuing to the end. To alter the control flow, decision-making instructions can be added to a program An instruction that alters the control flow is called a control statement or selection statement. public static void main(…) … }

CS-1010 Dr. Mark L. Hornick 3 The Java if statement specifies whether a block of code should execute …depending on the result of evaluating a test condition called a boolean expression public static void main(…) { if( ){ // block of code that executes when // the boolean expression is true } }

CS-1010 Dr. Mark L. Hornick 4 What is a boolean expression? First recall: A arithmetic expression uses arithmetic operators and is evaluated to a numerical value Some arithmetic expressions using binary arithmetic operators: x+y x-y x*y x/y x%y Arithmetic operators operate on numerical datatypes byte, short, int, long, float, double Exception: (+ operator works for the String datatype)

CS-1010 Dr. Mark L. Hornick 5 A boolean expression uses relational operators between numerical values and is evaluated to a boolean value: either true or false. x < y// less than operator; evaluates to true if x is less than y; otherwise false x <= y// less than or equal to; note =< is not valid x > y// greater than x >= y// greater than or equal to x == y// equal to; be careful w.r.t. x=y x != y// not equal to

CS-1010 Dr. Mark L. Hornick 6 A boolean expression can also involve the used of arithmetic expressions 2 < 1// less than operator; evaluates to true if 2 is less than 1; otherwise false x <= 3*y// less than or equal to; note =< is not valid x > Math.PI// greater than x >= 3/2// greater than or equal to x == 0// equal to; be careful w.r.t. x=y x != 1// not equal to

CS-1010 Dr. Mark L. Hornick 7 The term block of code means 0 or more Java instructions within the braces if ( ){ // begin block … } // end block Every instruction between the braces is executed if the boolean expression is true. If false, then none of them are executed.

CS-1010 Dr. Mark L. Hornick 8 … boolean exp A flow diagram illustrating execution

CS-1010 Dr. Mark L. Hornick 9 The { } braces are optional when the block of code is the single instruction following the if statement if ( ) // executed if true // executed always This means that the Java statement following the if statement will be executed when the boolean expression is true And only the single instruction following the if(…)

CS-1010 Dr. Mark L. Hornick 10 A more complex if statement if ( ){ // executed when exp. is true } else { }// executed when exp. is false The else clause is optional When present, the instructions in the else block are executed only when the boolean expression is false

CS-1010 Dr. Mark L. Hornick 11 The if-else flow diagram boolean exp …

CS-1010 Dr. Mark L. Hornick 12 The Boolean operators Repeat: relational operators take numerical operands and return a boolean value: either true or false. Example: (x<y) Exception: == and != operators can be used between boolean datatypes A boolean operator takes boolean values as its operands and returns a boolean value. The three boolean operators are &&// means “and” ||// means “or” !// means “not”

CS-1010 Dr. Mark L. Hornick 13 Boolean operators and their meanings: PQP && QP || Q!P false true falsetruefalsetrue false truefalse true false

CS-1010 Dr. Mark L. Hornick 14 A compound boolean expression using && if ( && <expression2){ // executed when both are true } else { }// executed when either is false

CS-1010 Dr. Mark L. Hornick 15 Another variant of a compound expression using || if ( || <expression2){ // executed when either are true } else { }// executed when both are false

CS-1010 Dr. Mark L. Hornick 16 Another variant of a compound expression using ! if ( ! ){ // executed when expr is FALSE } else { }// executed when TRUE

SE-1010 Dr. Mark L. Hornick 17 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)