Boolean Expressions & the if Statement



Similar documents
Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Chapter 2: Elements of Java

Selection Statements

J a v a Quiz (Unit 3, Test 0 Practice)

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Chapter 5. Selection 5-1

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Conditional Statements Summer 2010 Margaret Reid-Miller

Variables, Constants, and Data Types

Lecture 2 Notes: Flow of Control

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Conditionals (with solutions)

Chapter 1: Computer Systems

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Statements and Control Flow

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Conditions & Boolean Expressions

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Informatica e Sistemi in Tempo Reale

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Object Oriented Software Design

United States Naval Academy Electrical and Computer Engineering Department. EC262 Exam 1

CASCADING IF-ELSE. Cascading if-else Semantics. What the computer executes: What is the truth value 1? 3. Execute path 1 What is the truth value 2?

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

JavaScript: Control Statements I

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Programming Languages

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Example of a Java program

Decision Logic: if, if else, switch, Boolean conditions and variables

Chapter 2 Introduction to Java programming

TIP: To access the WinRunner help system at any time, press the F1 key.

Moving from CS 61A Scheme to CS 61B Java

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

ECE 122. Engineering Problem Solving with Java

AP Computer Science Java Subset

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Statements and Control Flow

Introduction to Java

I PUC - Computer Science. Practical s Syllabus. Contents

Computer Programming I

Sample CSE8A midterm Multiple Choice (circle one)

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Chapter 3 Operators and Control Flow

COMP 110 Prasun Dewan 1

Computer Programming I & II*

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

VB.NET Programming Fundamentals

Chapter 14: Boolean Expressions Bradley Kjell (Revised 10/08/08)

Basic Programming and PC Skills: Basic Programming and PC Skills:

ARIZONA CTE CAREER PREPARATION STANDARDS & MEASUREMENT CRITERIA SOFTWARE DEVELOPMENT,

AppendixA1A1. Java Language Coding Guidelines. A1.1 Introduction

Lecture 1 Introduction to Java

Bash shell programming Part II Control statements

USC Marshall School of Business Academic Information Services. Excel 2007 Qualtrics Survey Analysis

LAB4 Making Classes and Objects

CS 241 Data Organization Coding Standards

Fundamentals of Java Programming

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Part I. Multiple Choice Questions (2 points each):

Chapter 8 Selection 8-1

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Unit 6. Loop statements

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Course: ISYS 4373 Application Development with Java Prerequisite: ISYS 3293

Translating C code to MIPS

General Software Development Standards and Guidelines Version 3.5

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

6. Control Structures

Certified PHP Developer VS-1054

C++FA 5.1 PRACTICE MID-TERM EXAM

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 20 July 2015

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Chapter 7D The Java Virtual Machine

10.2 Series and Convergence

Python Programming: An Introduction To Computer Science

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Introduction to Computer Programming (CS 1323) Project 9

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

COURSE SYLLABUS EDG 6931: Designing Integrated Media Environments 2 Educational Technology Program University of Florida

Object Oriented Software Design

Tutorial on C Language Programming

C# programming. Introduction. By Per Laursen

Lecture 8: Binary Multiplication & Division

Java Basics: Data Types, Variables, and Loops

Binary Adders: Half Adders and Full Adders

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

Transcription:

Midterm Results Boolean Expressions & the if Statement September 24, 2007 Average: 91.6 Median: 94 Standard Deviation: 18.80 Maximum: 129 (out of 130) ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev Midterm Results (Fall 2006) Average: 86.1 Median: 97 Standard Deviation: 24.18 Maximum: 127 (out of 130) Top Scores Hrishank Jhildiyal 129 Alaettin Mete 125 Xu Feng 121 Daniel Pratt 120 Kian Chen 120 Alex Cole 119 Paul Wolf 117 Michael Steffen 116 Alexander Reifert 116 Eun Kim 116 Keith Johnson 116 Austin Green 115 HW4 is out Due this Friday It is shorter than normal (programs only) Electronic submission only 1

Chapter 5 Sections 5.1 5.2 Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one statement after another in sequence Some programming statements allow us to: decide whether or not to execute a particular statement execute a statement over and over, repetitively These decisions are based on boolean expressions (or conditions) that evaluate to or The order of statement execution is called the flow of control Method Control Flow If the called method is in the same class, only the method name is needed Method Control Flow The called method is often part of another class or object compute mymethod main doit helpme mymethod(); obj.doit(); helpme(); Encapsulation An encapsulated object can be thought of as a black box -- its inner workings are hidden from the client The client invokes the interface methods of the object, which manages the instance data Client Methods Data Conditional Statements A conditional statement lets us choose which statement will be executed next Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions The Java conditional statements are the: if statement if-else statement switch statement 2

The if Statement Logic of an if statement The if statement has the following syntax: if is a Java reserved word The condition must be a boolean expression. It must evaluate to either or. condition evaluated if ( condition ) statement; statement If the condition is, the statement is executed. If it is, the statement is skipped. Boolean Expressions The if Statement A condition often uses one of Java's equality operators or relational operators, which all return boolean results: == equal to!= not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator (==) and the assignment operator (=) An example of an if statement: if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); First the condition is evaluated -- the value of sum is either greater than the value of MAX, or it is not If the condition is, the assignment statement is executed -- if it isn t, it is skipped. Either way, the call to println is executed next Indentation The statement controlled by the if statement is indented to indicate that relationship Example: Age.java (page 208) The use of a consistent indentation style makes a program easier to read and understand Although it makes no difference to the compiler, proper indentation is crucial "Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding 3

The if Statement What do the following statements do? if (top >= MAXIMUM) top = 0; Sets top to zero if the current value of top is greater than or equal to the value of MAXIMUM if (total!= stock + warehouse) inventoryerror = ; Sets a flag to if the value of total is not equal to the sum of stock and warehouse The precedence of the arithmetic operators is higher than the precedence of the equality and relational operators Boolean expressions can also use the following logical operators:! Logical NOT && Logical AND Logical OR They all take boolean operands and produce boolean results Logical NOT is a unary operator (it operates on one operand) Logical AND and logical OR are binary operators (each operates on two operands) Logical NOT The logical NOT operation is also called logical negation or logical complement If some boolean condition a is, then!a is ; if a is, then!a is Logical expressions can be shown using a truth table a!a Logical AND and Logical OR The logical AND expression a && b is if both a and b are, and otherwise The logical OR expression a b is if a or b or both are, and otherwise Expressions that use logical operators can form complex conditions if (total < MAX+5 &&!found) System.out.println ("Processing "); All logical operators have lower precedence than the relational operators Logical NOT has higher precedence than logical AND and logical OR A truth table shows all possible - combinations of the terms Since && and each have two operands, there are four possible combinations of conditions a and b a b a && b a b 4

Boolean Expressions Specific expressions can be evaluated using truth tables Short-Circuited Operators The processing of logical AND and logical OR is short-circuited total < MAX found!found total < MAX &&!found If the left operand is sufficient to determine the result, the right operand is not evaluated if (count!= 0 && total/count > MAX) System.out.println ("Testing "); This type of processing must be used carefully The if-else Statement An else clause can be added to an if statement to make an if-else statement if ( condition ) statement1; else statement2; Example: Wages.java (page 211) If the condition is, statement1 is executed; if the condition is, statement2 is executed One or the other will be executed, but not both Logic of an if-else statement condition evaluated THE END statement1 statement2 5