Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Logic & program control part 2: Simple selection structures.
Python Programming Language
Conditional Statements Introduction to Computing Science and Programming I.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Data types and variables
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
The character data type char
CIS Computer Programming Logic
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
Flow of Control Part 1: Selection
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
1 2. Program Construction in Java. 2.4 Selection (decisions)
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Primitive Variables.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Computer Science 101 If Statement. Python Relational Operators.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Chapter 7 JavaScript: Control Statements, Part 1
Control Structures I Chapter 3
Java Programming Fifth Edition
Java Primer 1: Types, Classes and Operators
Representation, Syntax, Paradigms, Types
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
JavaScript: Control Statements I
Multiple variables can be created in one declaration
Operator Precedence Operators Precedence Parentheses () unary
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Java Programming: From Problem Analysis to Program Design, 4e
Computers & Programming Languages
The Boolean (logical) data type boolean
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Representation, Syntax, Paradigms, Types
Chapter 8: More on the Repetition Structure
Introduction to Programming Using Python PART 2
Representation, Syntax, Paradigms, Types
Computer Science Core Concepts
Representation, Syntax, Paradigms, Types
Presentation transcript:

Conditional statements and Boolean expressions

The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only if a given condition is satisfied An if-statement makes sure that the correct condition is satisfied before a certain action is performed

The if-statement in Java (2) Syntax of the if-statement: The keyword if announces (to the Java compiler) that we started an if-statement A conditional clause ( CONDITION ) follows the keyword if Following the condition clause, you can write (only) one statement

The condition clause of the if- statement (1) The condition clause of the if-statement is an expression that evaluates to true or false Expressions that evaluates to true or false are known in Computer Science as: Boolean expressions Example: a < 0

The condition clause of the if- statement (2)

Multiple statements in the "then"- part of the if-statement (1) Block = a pair of "{" and "}" braces that groups components in a Java program together

Multiple statements in the "then"- part of the if-statement (2) Statement block = multiple statements grouped together by braces {.... } A statement block in Java is considered as one (single) statement

Representation techniques: Flow chart (1) A flow chart is a diagram that represents a computer algorithm. It uses a rectangular box to represent an assignment statement It uses a diamond box to represent a condition Lines are used to indicate the flow of the steps in the algorithm

Representation techniques: Flow chart (2)

Representation techniques: Structure diagram(1) A structure diagram is another type of diagram to represent a computer algorithm. It uses different shapes of boxes to represent assignment statements and conditions The structure diagram does not use lines

Representation techniques: Structure diagram(2)

The Boolean (logical) data type boolean The boolean data type is a built-in (primitive) data type of Java is used to represent the logical values There are 2 logical values: true and false Encoding scheme used in the boolean data type: 0 represents false 1 represents true uses 1 byte of memory (to store 0 or 1)

Boolean literals There are 2 boolean literals (= logical constants) in Java: These 2 words are keywords (reserved words) in Java

Defining boolean typed variables (1) Syntax to define an boolean typed variable: boolean NameOfVariable ; The keyword boolean announces the variable definition clause The NameOfVariable is an identifier which is the name of the variable. The variable definition clause is must be ended with a semi- colon ";“ A boolean typed variable can store true (1) or false (0)

Defining boolean typed variables (2)

Operations that return a boolean result 1. Compare operators: A compare operator compares 2 numerical expressions and return a Boolean result. 2. Logical operators: A logical operator compares 2 Boolean (logical) expressions and return a Boolean result

Compare operators (1) A compare operator will return the value true if the test is successful A compare operator will return the value false if the test is unsuccessful The same automatic conversion rules used for arithmetic operators apply for compare operators

Compare operators (3) you can assume that all compare operators have the same priority

Compare operators (4)

Logical operators (1)

Logical operators (2)

Logical operators (3)

Logical operators (4)

Programming trick: test if a number is between 2 numbers (1) The program prints "yes" when 10 ≤ a ≤ 20 It is illegal to use the comparison operator "<=" on a Boolean value and a number

Programming trick: test if a number is between 2 numbers (2)

Programming trick: test if a number is between 2 numbers (3)