HST 952 Computing for Biomedical Scientists Lecture 3.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Chapter 4: Control Structures I (Selection)
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Visual C++ Programming: Concepts and Projects
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
DAT602 Database Application Development Lecture 5 JAVA Review.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Flow of Control Part 1: Selection
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Chapter 3 1 l Branching l Loops l exit(n) method l Boolean data type and logic expressions Flow of Control – Part 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
COMP Loop Statements Yi Hong May 21, 2015.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Information and Computer Sciences University of Hawaii, Manoa
The switch Statement, and Introduction to Looping
A mechanism for deciding whether an action should be taken
Flow of Control.
Expressions and Control Flow in JavaScript
Control Structures – Selection
3 Control Statements:.
Chapter 3: Program Statements
Summary Two basic concepts: variables and assignments Basic types:
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Using Decision Structures
Basic Programming Concepts
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
Presentation transcript:

HST 952 Computing for Biomedical Scientists Lecture 3

Recap Algorithm: A series of unambiguous steps for solving a problem Object-oriented programming: Approach to programming that relies on objects and interactions among objects

Recap Object: –Has attributes (properties) –Has methods (methods define the way the object interacts with “the world”) Each method associated with an object implements an algorithm that solves a particular problem that is specific to the object

Recap Class definition: –Provides a template of the attributes and methods associated with a kind of object –An object is a specific instantiation of a class

Recap Example class: Vehicle Attributes might include: –Number of wheels –Type –Color –Manufacturer –Mileage Methods might include: –getMileage() –setMileage(int miles) –setMileage(Vehicle otherv)

Recap Objects (instances) of class Vehicle VolvoS60 Number of wheels : 4 Type: Car Color: Blue Manufacturer: Volvo Mileage: 5,000 miles DodgeNeon Number of wheels : 4 Type: Car Color: Green Manufacturer: Dodge Mileage: 10,000 miles

Recap Objects (instances) of class Vehicle HarleyDynaGlide Number of wheels : 2 Type: Motorcycle Color: Black Manufacturer: Harley-Davidson Mileage: 1,000 miles MackVision Number of wheels : 18 Type: Truck Color: Red Manufacturer: Mack Mileage: 50,000 miles

Recap Imagine that we want to keep track of how many tons a truck can haul, but are not interested in this for cars or motorcycles One approach to doing this could involve making the Vehicle class a parent class (superclass) of the Car, Motorcycle, and Truck (sub)classes

Recap Attributes and methods common to all vehicles would be described in the Vehicle class Attributes that are specific to a Car, Truck, or Motorcycle would be specified in the appropriate subclass For our example, the Truck class could have the attribute tonnage and methods getTonnage() and setTonnage() which the Car and Motorcycle classes would not have

This Lecture We will examine some Java constructs that serve as building blocks/tools for implementing an algorithm in Java Java constructs include: –Expressions (boolean, arithmetic, logical, etc.) –Operators (comparison, logical, arithmetic) –Statements (assignment, branch, loop, etc.) Our focus will be on constructs that are important for determining the flow of control in a program

Boolean Expressions Evaluate to true or false May be used with other language constructs to determine the flow of control in a program Involve comparison operators and/or logical operators

Comparison Operators Symbol Description ==is equal to !=is not equal to < is less than <=is less than or equal to >is greater than >=is greater than or equal to

Comparison Operators The last four comparison operators may only be used to compare values from ordered sequences (numbers, characters) Examples of boolean expressions: ‘y’ < ‘z’ (evaluates to true) 5.9 >= 23(evaluates to false) true == false(evaluates to false) 25 != 25(evaluates to false)

Logical Operators Binary operator (requires two operands) Unary operator (requires just one operand) Symbol Description &&and (binary operator) ||or (binary operator) ! not (unary operator)

Logical Operators How the && operator works true && true evaluates to true true && false evaluates to false false && true evaluates to false false && false evaluates to false Once one of its operands is false, a boolean expression involving && evaluates to false

Logical Operators How the || operator works true || true evaluates to true true || false evaluates to true false || true evaluates to true false || false evaluates to false Once one of its operands is true, a boolean expression involving || evaluates to true

Logical Operators How the ! operator works ! true evaluates to false ! false evaluates to true a boolean expression involving ! evaluates to the opposite of the operand’s truth value

Logical Operators ! has the highest precedence && has the next highest precedence || has the lowest precedence may use parentheses to group parts of an expression to force a particular order of evaluation

Logical Operators What do the boolean expressions in these assignment statements evaluate to? boolean firstBool = true || false && false; boolean secondBool = (true || false) && false; boolean thirdBool = firstBool || secondBool; boolean fourthBool = !true || false; boolean fifthBool = !(true && false);

Flow of control May need to write methods that have to choose one path out of several possible paths (programs for an ATM machine have to choose how much money to dispense based on your input and balance) May need to repeat an action several times to obtain a desired result (e.g., solution to the GCD problem)

Branching statements Allow us to make a choice of an action given two or more options Use implicit or explicit boolean expressions in making the choice Examples of Java branching statements: if /if-else statement (uses explicit boolean expr.) switch statement (uses implicit boolean expr.)

if/if-else statement Syntax: if (boolean expression) { // perform action1 } else { // perform action2 }

if/if-else statement The actions following the if part of the statement are performed only when the boolean expression evaluates to true If the boolean expression evaluates to false, the actions following the else part of the statement are executed (when an else is present) The curly braces group together all the actions to be performed If only one action is to be performed, the curly braces may be omitted

if/if-else statement Example: if (score < 100) System.out.println(“Score is less than 100); if (score < 90) System.out.println(“Score is less than 90); if (score < 80) System.out.println(“Score is less than 80); what happens when score is 70?

if/if-else statement if (score < 80) System.out.println(“Score is less than 80); else { if (score < 90) System.out.println(“Score is less than 90); else { if (score < 100) System.out.println(“Score is less than 100); } what happens when score is 70?

if/if-else statement Easier way to write the same sequence of statements: if (score < 80) System.out.println(“Score is less than 80); else if (score < 90) System.out.println(“Score is less than 90); else if (score < 100) System.out.println(“Score is less than 100); what happens when score is 120?

Switch statement Multi-way branching statement Makes a decision on which branch to take based on the value of an integer or character expression (called the controlling expression) Can be mapped to an equivalent if-else sequence (but not always the other way around) Syntax next

Switch statement switch(int or char expression) { case int or char constant: // perform action1 break; case int or char constant: // perform action2 break; default: // perform action3 break; }

Switch statement Note: there is an implicit equality comparison between the int or char expression in the switch and the constant in a case default case is optional break statement ends each case and is necessary to prevent fall-through

Switch statement Example (firstInitial is a variable of type char): switch(firstInitial) { case ‘A’: System.out.println(“Instructor is Aziz”); break; case ‘O’: System.out.println(“Instructor is Omolola”); break; Example continued on next slide

Switch statement case ‘Q’: System.out.println(“Instructor is Qing”); break; default: System.out.println(“Unknown instructor”); break; }

Switch statement Another example (myNum is a variable of type int): switch(myNum) { case 1: System.out.println(“The number is one”); case 2: System.out.println(“The number is two”); default: System.out.print(“The number is neither”); System.out.println(“ one nor two”); } What happens when myNum is 1?

Switch statement Another example (myNum is a variable of type int): switch(myNum) { case 1: System.out.println(“The number is one”); case 2: System.out.println(“The number is two”); default: System.out.print(“The number is neither”); System.out.println(“ one nor two”); } What happens when myNum is 3?

Switch statement Example: Cases with no breaks (firstInitial is a variable of type char): switch(firstInitial) { case ‘A’: case ‘a’: System.out.println(“Instructor is Aziz”); break; case ‘O’: case ‘o’: System.out.println(“Instructor is Omolola”); break; Example continued on next slide

Switch statement case ‘Q’: case ‘q’: System.out.println(“Instructor is Qing”); break; default: System.out.println(“Unknown instructor”); break; } Fall-through is desired in this example

Loop Statements Allow us to repeat an action/several actions until a particular condition is met Examples of Java loop statements: while do-while for

While Loop Syntax: while(boolean expression) { // perform action(s) }

While Loop The actions in the loop body are performed only when the boolean expression evaluates to true If the boolean expression is true, the actions are performed until it is false If the boolean expression is never false, we may have an infinite loop (actions performed until program runs out of memory resources, etc.) This implies that there should be a statement in the body of the loop that alters the loop’s course

While Loop Example: int iterator = 0; while(iterator < 10) { System.out.println(“Iterator is ” + iterator); iterator = iterator + 1; // another way of writing the line above is iterator += 1 }

Do-While Loop Syntax: do { // perform action(s) } while(boolean expression);

Do-While Loop The actions in the loop body are performed until the boolean expression evaluates to false If the boolean expression is never false, we may also have an infinite loop This implies that there should be a statement in the body of the loop that alters the loop’s course (ensures that the boolean expression is eventually false)

Do-While Loop Example: int iterator = 0; do { iterator = iterator + 1; // another way of writing the line above is iterator++ System.out.println(“Iterator is ” + iterator); } while(iterator < 10); How does this differ from the while loop example in terms of what gets printed?

For Loop Syntax: for(initializer; boolean expression; update action) { // perform actions }

For Loop Example: int iterator; for(iterator = 0; iterator < 10; iterator++) { System.out.println(“Iterator is ” + iterator); }

Before next class Read Chapter 4 and Chapter 5