Flow of Control Java Programming Mrs. C. Furman January 5, 2009.

Slides:



Advertisements
Similar presentations
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Advertisements

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Logical Operators and Conditional statements
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 3: Program Statements
Computer Science Selection Structures.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Computer Science Department Relational Operators And Decisions.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Introduction to Java Java Translation Program Structure
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Decisions, Decisions, Decisions Conditional Statements In Java.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Control statements Mostafa Abdallah
Decision Statements, Short- Circuit Evaluation, Errors.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures I
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 6 More Conditionals and Loops
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Boolean Expressions & the ‘if’ Statement
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,
SELECTION STATEMENTS (1)
Chapter 4: Control Structures I
Chapter 3: Program Statements
Control Structure Chapter 3.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
CprE 185: Intro to Problem Solving (using C)
Conditionals and Loops
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Flow of Control Java Programming Mrs. C. Furman January 5, 2009

Flow of Control The order in which statements are executed Order is linear in fashion unless otherwise specified. Linear – program is executed one statement at a time, one after the other.

Conditional statement/ Selection Statement Allows us to choose which statement will be executed next. In Java  If statement / if-else statement and switch statement.

Decision Making Boolean Expressions / Conditional Decisions are made based upon boolean expressions. The expressions are evaluated as either true or false.

Relationship Operators ==Equal to !=Not equal to <Less than <=Less than or equal to >Greater than >=Greater than or equal to Example: 5==5 X<=3 x+3 != y + 5

Logic Operators ! – NOTChanges the truth value && - AndTrue if both values are true || - ORTrue when one of the values are true Precedence – Evaluate in the order of !, && then ||

If Statements We may want to perform different operations based on different conditions. Ex. We want to print a certificate based on a students grade >= 95 – high honor roll = 90 – honor roll = 85 – Merit roll <85 – no certificate

Grade example continued. if(grade >= 95) { System.out.println (“High Honor Roll”); } If the boolean expression is true  High Honor Roll is printed. If the boolean expression is false  Skip the output line and go to the next line in the program.

if structure if (grade >= 95) keyword boolean condition The boolean condition must be placed inside parenthesis.

if structure continue if(grade >= 95) { System.out.println (“High Honor Roll”); } { } – we use braces to surround the body of the if statement. The body is a block of code.

Using if…else if (grade >= 95) { System.out.println (“High Honor Roll”); } else { System.out.println (“Needs Improvement”); } grade = 98 Output: High Honor Roll grade = 88 Output: Needs Improvement

if structure if (grade >= 95) {System.out.println (“High Honor Roll”);} else { The else comes after the body of the if is closed, then the body of the else is also enclosed in braces.

if structure if (grade >= 95) {System.out.println (“High Honor Roll”);} else { when the boolean condition is true, the body of the if is executed and the else body is skipped. when the boolean condition is false, the body of the if is skipped and the else body is executed.

Two-way Decisions This windshield wiper decision is a two-way decision (sometimes called a binary decision.) It seems small, but in programming complicated decisions are made of many small decisions.

Grade output cont if(grade >= 95) { System.out.println (“High Honor Roll”);} if (grade = 90) {System.out.println (“Honor Roll”); } if (grade = 85) {System.out.println (“Merit Roll”);}

Grade Output Cont. In the previous slide, we see 3 separate if statements. Each if statement will be executed! And executed independent of each – other. What would happen if grade was 98? 93? 80?

Grade Output Cont. if… else would be a much more efficient way of handling the multiple conditions presented in the previous scenario. The next slide shows nested if statements, which will be much more efficient.

Using if…else if (grade >= 95) {System.out.println (“High Honor Roll”);} else {if (grade >=90) {System.out.println (“Honor Roll”);} else {if (grade >=85) {System.out.println (“Merit Roll”);} }

Program Example public class EvenOrOdd { public static void main (String [] args) { int num; Scanner input = new Scanner (System.in); System.out.println (“Enter an integer: “); num = input.nextInt(); if (num % 2 == 0) {System.out.println (num + “ is even.”); System.out.println (“Even is awesome”); } else {System.out.println (num + “ is odd.”); System.out.println (“Odd is weird”); } System.out.println (“Bye Bye”); // this statement is outside the if } num = 4 Output: 4 is even Even is awesome Bye num = 5 Output: 5 is odd Odd is weird Bye

if.. else if (num % 2 == 0) System.out.println (num + “ is even.”); else System.out.println (num + “ is odd.”); System.out.println (“Odd is weird”); System.out.println (“Bye Bye”);

if.. else if (num % 2 == 0) System.out.println (num + “ is even.”); else System.out.println (num + “ is odd.”); System.out.println (“Odd is weird”); System.out.println (“Bye Bye”); What would the output be for an even number? odd number? When you omit the braces, the if and else only take the 1 st statement. If you want to execute more than one statement, you have to put the body of the code into curly braces. The indentation helps our human eyes determine what is in the if and what isn’t. It has no impact on how the computer interprets the code.

Recall: Short Circuit If the left hand operand is sufficient to determine whether or not we are true / false, the right hand operator is not evaluated. Ex. int x = 5; if ((x == 5) || (x != 3)) - true first part true with or if ((x != 5) && (x !=3)) – false first part false with and

Common Errors!! If(total >= 25); { } if(total = 10) { }

Comparing Strings String class contains an equals method which returns a boolean, true, if the two strings contain exactly the same characters, and false otherwise. Ex. String name = “Furman”; boolean same; same = name.equals (“Furman”); same = (name == “Furman”);

Alphabetical Order with Strings To determine alphabetical order use the compareTo method. String name = “Furman”; int greaterOrLess; greaterOrLess = name.compareTo(“Gilbert”) greaterOrLess is negative greaterOrLess = name.compareTo (“Dog”); greaterOrLess is positive greaterOrLess = name.compareTo(“Furman”); greaterOrLess is 0.