OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
If Statements & Relational Operators Programming.
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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
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.
Computer Science Selection Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
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.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Statements: Part1  if, if…else, switch 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
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 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
(Java Looping and Conditional Statements). Flow of control Sequential Executes instructions in order Method Calls Transfer control to the methods, then.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
The Ohio State University
Chapter 4: Control Structures I
INTERMEDIATE PROGRAMMING USING JAVA
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 2 - Introduction to C Programming
Chapter 4: Control Structures I
More important details More fun Part 3
2.5 Another Java Application: Adding Integers
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Boolean Expressions and If
User input We’ve seen how to use the standard output buffer
Chapter 2 - Introduction to C Programming
Chapter 4: Control Structures I
Expression Review what is the result and type of these expressions?
And now for something completely different . . .
Java Programming Control Structures Part 1
Selection Statements.
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Chapter 3: Selection Structures: Making Decisions
Life is Full of Alternatives
OBJECT ORIENTED PROGRAMMING I LECTURE 4 PART 2 GEORGE KOUTSOGIANNAKIS
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
CprE 185: Intro to Problem Solving (using C)
Conditionals and Loops
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS CS 201 OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology- George Koutsogiannakis

PREVIOUS TOPICS Static Variables and their usage. Using Command Line for user input. Using the JOptionPane for user input.

NEW TOPICS FORMING CONDITIONS FOR SELECTION if and else SELECTIONS RELATIONAL OPERATORS BOOLEAN EXPRESSIONS- TRUTH TABLES BOOLEAN OPERATIONS DE MORGANS LAWS if and else SELECTIONS If else SELECTIONS

Flow of Control Sequential Method calls Selection Looping Execute instructions in order Method calls Transfer control to method, execute instructions in method, then return with or without a value Selection Execute different instructions depending on data Looping Repeat a set of instructions for different data

Equality Operators Used to determine if the values of two expressions are equal or not equal Result is true or false Equality operators Type (number of operands) Meaning = = binary is equal to != is not equal to

Example If int variable age holds the value 32: ( age == 32 ) evaluates to true ( age != 32 ) evaluates to false Use the equality operators only with primitive types and object references, not to compare object data! More on this later…

Common Error Trap Do not confuse the equality operator (==) with the assignment operator (=).

Type (number of operands) Relational Operators Used to compare the values of two expressions Result is true or false Relational Operators Type (number of operands) Meaning < binary is less than <= is less than or equal to > is greater than >= is greater than or equal to

Example If int variable age holds value 32: ( age < 32 ) evaluates to false ( age <= 32 ) evaluates to true ( age > 32 ) evaluates to ( age >= 32 ) evaluates to

Evaluation to true or false Suppose that we want to check if the value stored in memory location identified as : int myint has a certain value stored in it like the value 32, then the code for it is: if(myint==32) System.out.println(myint); myint=32; The above code prints out the value held in myint only if it is 32 (in other words if what is inside the parenthesis was evaluated to true). Otherwise, if it is not evaluated to true, the statement System.out… is not executed and instead the next statement is executed which assigns the value 32 to myint.

Logical Operations Logical Operations allow us to evaluate something as being either true or false. Remember that the data type that can have either a true or a false value is: boolean. Therefore quite often we are faced with the problem of having to make choices depending if some value was evaluated true or false.

Logical Operations For example consider the statement: I am going to leave my house at 7:00 a.m. If the bus is outside my door I will take the bus Otherwise I will walk to the subway station. The decision is based on a variable being evaluated as true or false. Suppose we have a variable called bus of type boolean: boolean bus; The variable (identifier) bus can be set to have the value true stored in it if the bus is outside my door or the value false if the bus is not outside my door. I will have to evaluate bus to see what value it has before I make my decision to walk to the train station.

Logical Operations A part of Algebra called Boolean Algebra provides operators and operations to evaluate more complex logical decisions: What If we were to say for instance that I will take the bus if the bus is outside my door (variable bus=true) and at the same time it is a rainy day otherwise I will walk to the subway station. Now, I faced with fact that I have to evaluate the simultaneous true outcome of two conditions: bus=true rain=true If either of the conditions, or both, come out to false then I walk to the train station. The logical procedure that represents this kind of a decision in Boolean Algebra is called: AND Operation

Logical Operators Logical Operator Type (number of operands) Meaning ! Unary NOT && Binary AND || OR Operands must be boolean expressions!

Logical Operators The NOT operator ( ! ) evaluates to the inverse of the value of its operand. If the operand is true, the result will be false; and if the operand is false, the result will be true. The AND operator ( && ) takes two boolean expressions as operands; if both operands are true, the result will be true, otherwise it will be false. The OR operator ( || ) takes two boolean expressions as operands. If both operands are false, the result will be false; otherwise it will be true.

Truth Table a b !a a && b a || b true false THE WORDS FOR TRUE AND FALSE CAN BE SUBSTITUTED WITH BINARY NUMBERS AND THE ABOVE OPARATIONS (!, &&, ||) CAN BE APPLIED TO BINARY NUMBERS.

BOOLEAN OPERATIONS-NOT ! MEANS INVERSE. THUS IF A=100100 C=!A RESULTS IN C=011011

BOOLEAN OPERATIONS-AND Suppose A AND B REPRESENT A BINARY NUMBERS A= 10011 WHERE THE LEADING NUMBER IS THE MOST SIGNIFICANT DIGIT B= 10101 WHERE THE LEADING NUMBER IS THE MOST SIGNIFICANT DIGIT THE BOOLEAN OPERATION AND RESULTS C= A&&B C A B 1

BOOLEAN OPERATIONS-OR C=A||B C A B 1

BOOLEAN OPERATIONS- NAND C=!(A&&B) C A B 1

BOOLEAN OPERATIONS- NOR C=!(A||B) C A B 1

BOOLEAN EXPRESSIONS D=(A&&B)||C&&!(A||C) THE FOLLOWING IS A BOOLEAN EXPRESSION: D=(A&&B)||C&&!(A||C) WHERE A, B, C A ARE LOGICAL VARIABLES (THEY CAN BE BINARY NUMBERS OR TRUE OR FALSE VALUES OF VARIABLES) EVALUATION IS DONE LEFT TO RIGHT (PARENTHESIS FIRST). ANOTHER RULE IS THAT AND PRECEDES OR TRUTH TABLE SCAN HELP WITH THE EVALUATION. FIRST EVALUATE E=A&&B THEN F=A||C THEN G=!F NOW THE EXPRESSION IS D=E||C&&G NEXT H=C&&G LAST D=E||H

DE MORGAN ‘ S THEOREM IF BOOLEAN EXPRESSIONS ARE COMPLICATED: USE THE EQUATIONS SHOWN IN THE NEXT SLIDES TO SIMPLIFY COMPLEX BOOLEAN EXPRESSIONS THE EQUATIONS ARE BASED ON DE MORGAN’S THEOREMS

Example using De Morgan’ s Laws Suppose we want to simplify the expression: D=!(A||B)&&!(B||C) !(A||B)=!A&&!B !(B||C)=!B&&!C D= !A&&!B&&!B&&!C but !B&&!B=!B D=!A&&!B&&!C= !(A||B||C)

Equivalence of Expressions DeMorgan's Laws: NOT( A AND B ) = ( NOT A ) OR ( NOT B ) !(A&&B) IS THE SAME AS : !A||!B NOT( A OR B ) = ( NOT A ) AND ( NOT B ) !(A||B) IS THE SAME AS : !A&&!B Thus, to find an equivalent expression: change && to || change || to && negate each operand expression

Negation of Equality and Relational Operators Expression !( Expression ) a == b a != b a < b a >= b a <= b a > b a < b

Example These expressions are equivalent: !( age > 18 && age < 65 ) !( age > 18 ) || !( age < 65 ) ( age <= 18 || age >= 65 )

Distributive Property in Boolean Expressions i.e. A||( C&&D) = ( A ||C ) && A||D

Simple if Statement if ( condition ) Used when the program should perform an operation for one set of data, but do nothing for all other data. Syntax: if ( condition ) { // true block // executed if condition is true } next line of code Note: Curly braces are optional if the true block contains only one statement Code within the brackets is executed only when the condition evaluates to true. If the condition evaluates to false the code within the brackets is skipped and the next lien of code is executed.

Simple if Flow of Control If the condition evaluates to true, the true block is executed. If the condition evaluates to false, the true block is skipped. See Text Example 5.2 TestGrade.java

EXAMPLE import javax.swing.JOptionPane; public class Selections { public static void main(String[] args) int x=10; Scanner scan=new Scanner(System.in); System.out.println(“Please enter an integer less than 10”); int y=scan.nextInt(); if(y>=x) JOptionPane.showMessageDialog(null, “You entered an integer greater than 10”); System.exit(0); } int z=x+y; JOptionPane.showMessageDialog(null, “Your new number is”+z);

If and else if (condition) { //execute the code inside the brackets } execute the code within these brackets Now continue with execution of the next line of code Note: Gives you a way to test two possibilities. One or the other must be executed (but not both) before the next line of code (after the selections) is executed.

if/else Flow of Control If the condition is true, the true block is executed. If the condition is false, the false block is executed. See Example 5.3 PassingGrade.java

Else if usage Start with an if if(condition) { execute this code } else if(another_condition) else if (third_condition) Continue with the next line of code Note: one of the conditions could be true and therefore its code will be executed.The line of code after the selections is executed next. Note: Possibly none of the conditions could evaluate to false and therefore no selection code is executed. The line of code after the selections is executed next.

else if and else usage if(condition) { execute this code } else if(another_condition) else if (third_condition) else Next line of code What is the difference between this and the previous slide code?

if/else if Flow of Control If the first condition is true, the first true block is executed. The rest of the if/else statement is skipped. If the nth condition is true, the nth true block is executed. The rest of the if/else statement is skipped. If no condition is true, the false block is executed (optional). See Example 5.4 LetterGrade.java