Chapter 3 1 l Branching l Loops l exit(n) method l Boolean data type and logic expressions Flow of Control – Part 1.

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Branching Loops exit(n) method Boolean data type and expressions
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Lecture 6 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
ITM352 Conditionals Lecture #5. 1/28/03ITM352 Fall 2003 Class 5 – Control Flow 2 Announcements r Assignment 1 m Create a Zip file of your JBuilder project.
true (any other value but zero) false (zero) expression Statement 2
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
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.
ITM 352 Flow-Control: if and switch. ITM © Port, KazmanFlow-Control - 2 What is "Flow of Control"? Flow of Control is the execution order of instructions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 4 – PHP Flow Control SFDV3011 – Advanced Web Development 1.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Flow of Control Chapter 3 Flow of control Branching Loops
Introduction To Scientific Programming Chapter 3.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
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 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Lecturer: Dr. AJ Bieszczad Chapter 3 COMP 150: Introduction to Object-Oriented Programming 3-1 l Branching l Loops l exit(n) method l Boolean data type.
Decision Statements, Short- Circuit Evaluation, Errors.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Branching statements.
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Chapter 3 Branching Statements
ITM 352 Flow-Control: if and switch
Branching Loops exit(n) method Boolean data type and expressions
Chapter 4: Control Structures I (Selection)
Web Programming– UFCFB Lecture 20
Announcements/Reminders
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

Chapter 3 1 l Branching l Loops l exit(n) method l Boolean data type and logic expressions Flow of Control – Part 1

Chapter 3 2 “Flow of Control” - control of the execution of instructions in a program 1. Sequence - execute next instruction 2. Selection - conditional selection of next –if, switch/case, try/catch 3. Repetition - repeat (loop) code block until a particular condition is met; either, –proceed to next (or first) instruction in block –jump back to beginning (first instruction) of block –exit block, continue with the next instruction after block l Selection & Repetition are called Branching Controls (there may be more than one path)

Chapter 3 3 Java Flow Control Statements Sequence »default; Java automatically executes the next instruction Branching: Selection »if, if-else [and if-else if] »switch/case Branching: Repetition (Looping) »while, for  "pre-check" loops »do-while  "post-check" loop

Chapter 3 4 Definition of Boolean Values l Branching »decisions are based on the outcome of a boolean expression that evaluates in either true or false »boolean expression also called a "condition" l All branching controls utilise boolean expressions, »if ( boolean expr. ) »while ( boolean expr. ), for (__, boolean expr., __) »do { } while ( boolean expr. ) »switch/case is special because is uses an implicit boolean expression (discussed later)

Chapter 3 5 Comparison Operators (each results in a boolean value)

Chapter 3 6 Variables and Objects: memory! l a variable points directly to its data, »the variable directly stores its data »this is called a "direct reference" to the variable data l an object identifier is a "reference variable" that stores a memory address as its data, »the memory address is the location where the object's data is stored (as variable values and methods) »this is called an "indirect reference" to the object data l example: int size=10; String name="Bob"; 10 size &f22c name 'B' 'o' 'b'.equals().length().charAt() &f22c

Chapter 3 7 Object Comparisons l == does not work as expected for objects!! »unlike variables, objects store memory addresses »in using “==“ to test if objects are "equal to each other," the test actually performed is, –if "the memory addresses stored by the object reference variables are the same" (is this ever true?!) »note: all objects reference variables (regardless of the class they are instances of) have the same data type; and so can be compared together (but is this useful?) l Most classes/objects have a method to test for "data equality" rather than "memory address equality" to test if the objects share similar data

Chapter 3 8 String Comparisons: special method l for String objects, use their.equals( ) method, String s1 = “Mondo”, s2; s2 = SavitchIn.readLine(); if ( s1.equals(s2) ) System.out.print("User typed 'Mondo'); else System.out.print("User did not type.."); »s1.equals(s2) is true both Strings have the same data; false otherwise l.equals() is case sensitive, so to compare meaning an ignore case, use.equalsIgnoreCase() »how do you think.equalsIgnoreCase() works?

Chapter 3 9 Compound Boolean Expressions l To build longer, more complex, boolean expressions, [boolean LHS] logic operator [boolean RHS] »&& or & (AND) – both LHS and RHS must be true »|| or | (OR) – either LHS or RHS is true l example: a test to see "if A is either equal to 0, or between the values of B and C (equal to neither)", (A == 0) || (A < B && B < C) l parentheses are sometime not required but always added for clarity (along with a comment!!) »see text (and later slides) for Precedence rules »single & and | are used to avoid short-circuit evaluation and force complete evaluation of a boolean expression

Chapter 3 10 if statement l "do the next statement if test is true or skip it if false" l Syntax: if ( boolean_expression ) action if true; //only if true next action; //always executed »indentation for program readability if (eggsPerBasket < 12) // if less than a dozen eggs System.out.println("Less than a dozen eggs per basket"); totalEggs = numberOfEggs * eggsPerBasket; System.out.println("A total of "+ totalEggs + " eggs.");

Chapter 3 11 More than 1 statement: Compound Statements: l although the if can perform only a single statement, statements can be grouped together and treated as one l these are called compound statements, code blocks, or just a blocks, and are defined by enclosing the statements in curly brackets (like the main() method) l example: if (eggsPerBasket < 12) // if less than 12 eggs { System.out.println(“Less than a dozen...”); costPerBasket = 1.1 * costPerBasket; } //end of if statement

Chapter 3 12 Two-way Selection: if-else l Select either one of two options, »either perform the true statement block or the false statement block l syntax: if (Boolean_Expression) // expression is true { statement block if true } else // assumption is that expression is false { statement block if false } statements outside of/after if; always executed

Chapter 3 13 if-else Examples l example with single-statement blocks: if(time < limit) // time less than deadline System.out.println(“You made it.”); else System.out.println(“Missed deadline.”); l example with compound statements: if(time < limit) // time less than deadline { System.out.println(“You made it.”); bonus = 100; // sweet!! } else // time is >= than deadline { System.out.println(“Missed deadline.”); bonus = 0; // doh!! } // end of if

Chapter 3 14 Multi-way if if-else if an extended form of "two-way if" (also called a cascading if-else), if (score >= 90 // scr >= 90 grade = ‘A’); else if (score >= 80) // 80<=scr<90 grade = ‘B’; else if (score >= 70) // 70<=scr<80 grade = ‘C’; else if (score >= 60) // 60<=scr<70 grade = ‘D’; else // scr<60 grade = ‘E’; »to see how if works, match up each else to an if

Chapter 3 15 Multibranch selection: switch l an other branching technique, similar to multi-way if l comments on switch/case »the controlling_expression must be an integer data type: byte, char, short, int, or long »controlling_expression and case_label have same type »when a break statement is encountered, control immediately goes to the first statement after the switch statement, –if no break is encountered to program proceeds directly to the next instruction in the following case statement –the switch/case statement is very old (written for C/C++) but is very efficient after being compiled

Chapter 3 16 switch Example switch ( seatLocationCode ) { case 1: System.out.println(“Orchestra”); price = 40.00; break; case 2: System.out.println(“Mezzanine”); price = 30.00; break; case 3: System.out.println(“Balcony”); price = 15.00; break; default: System.out.println(“Unknown seat"); break; // last statement; optional }// end of switch()