Introduction to programming in java Lecture 11 Boolean Expressions and Assignment no. 2.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
 Control structures  Algorithm & flowchart  If statements  While statements.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Chapter 6 Horstmann Programs that make decisions: the IF command.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
1 Selection Statements Overview l Relational and Logical Operations l Selection structures »if statement »if-else statement l Preview: More on Selection.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Systems Architecture I1 Propositional Calculus Objective: To provide students with the concepts and techniques from propositional calculus so that they.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Branches and Program Design
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
TK 1914 : C++ Programming Control Structures I (Selection)
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Control statements Mostafa Abdallah
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
Decision Making and Branching
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Random Functions Selection Structure Comparison Operators Logical Operator
Introduction to programming in java Lecture 05 Review of 1 st four lectures and Practice Questions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
The Ohio State University
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Chapter 4: Control Structures I
The Selection Structure
Chapter 4: Decision Structures and Boolean Logic
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Control Statement Examples
ICS 3U Tuesday, September 21st.
Conditions and Ifs BIS1523 – Lecture 8.
Computers & Programming Languages
Compound Assignment Operators in C++
Relational Operators Operator Meaning < Less than > Greater than
Chapter 4: Decision Structures and Boolean Logic
Selection Control Structure
Chapter 5: Control Structure
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Life is Full of Alternatives
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
CS2011 Introduction to Programming I Selections (I)
Relational Operators.
Life is Full of Alternatives
Chapter 3: Selection Structures: Making Decisions
Life is Full of Alternatives
Selection Control Structure
Chapter 4: Decision Structures and Boolean Logic
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

Introduction to programming in java Lecture 11 Boolean Expressions and Assignment no. 2

Boolean Expressions Topics – Relational Operators (review) – Logical Operators – AND Operator – How to check that a number is in range – Boolean Expressions – OR Operator – Comparison between AND and OR – NOT Operator

Relational operators

Relational expression The symbol && means AND. The if statement asks a question with two parts: if ( x >= 5 && x <= 10 ) Each part is a relational expression. A relational expression uses a relational operator to compute a true or false value. 1 st part2 nd part

AND Operator The and operator && is a logical operator. A logical operator examines two true/false values and outputs a single true/false value. Here is what && does: – true && true = true – false && true = false – true && false = false – false && false = false Use and when every requirement must be met.

AND operator Example

OR operator The symbol || (vertical-bar vertical-bar) means OR operator. The OR operator evaluates to true when either qualification is met or when both are met. The if statement asks a question with two parts: if (cash >= || credit >= 25000) If either part is true, or both parts are true, then the entire boolean expression is true.

OR operator (Cont…) Here is how || works: – true || true = true – false || true = true – true || false = true – false || false = false

OR operator Example

NOT! Operator The NOT operator in Java is this: ! (exclaimation point). The NOT operator changes true to false and false to true, as seen in the truth table.

Example if ( !(cost < 50) ) System.out.println("Reject these shoes") else System.out.println("Acceptable shoes");

Practice Question The front tires of a car should both have the same pressure. Also, the rear tires of a car should both have the same pressure (but not necessarily the same pressure as the front tires). Write a program that reads in the pressure of the four tires and writes a message that says if the inflation is OK or not. Example: Input right front pressure 38 Input left front pressure 38 Input right rear pressure 42 Input left rear pressure 42 Inflation is OK

Homework Assignment # 2 (Total marks: 5) Submission deadline: 8 th Oct 2013 NOTES: 1.Only HAND WRITTEN assignments are acceptable on A4 size white paper. 2.If any student copy assignment from other then -5 marks will be deducted from the overall grade. 3.Late submission is not allowed.

Question 01 Write a program called PassOrFail.java that takes two positive integers as command-line arguments and prints PASS if sum of both the integers is greater than 60 otherwise it should prints FAIL. For example: > java PassOrFail Total = 70. Your are PASS.

Question 02 Write a program that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise.

Question 3 A bank has the following rule: if a customer has more than $1000 dollars in their checking account or more than $1500 dollars in their savings account, then there is no service charge for writing checks. Otherwise there is a $0.15 charge per check. Write a program that asks for the balance in each account and then writes out the service charge.

Question 4 Write a program called MyCalculator.java which reads two integer values and one operator ( +, -, /, *) from the standard input and print the result. Note use switch statement in this program. For example: > java MyCalculator Enter 1st number: 5 Enter 2nd number: 10 Enter operator: + Answer: = 15

Question 5 The maximum possible efficiency of a steam engine depends on the temperature of the steam in the boiler and the temperature of the outside air: efficiency = 1 - Tair / Tsteam where Tair is the air temperature and Tsteam is the steam temperature. The temperatures are given in degrees above absolute zero. Normal air temperature is about 300  K. Boiling is 373  K. Write a program that asks the user for the air temperature and the steam temperature and writes out the maximum possible efficiency of a steam engine. However, if the steam temperature is less than 373  K there is no steam, so the efficiency is zero.