Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Advertisements

1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Logical Operators and Conditional statements
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3: Program Statements
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 3: Program Statements
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
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.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Control statements Mostafa Abdallah
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science.
© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Chapter 3: Program Statements
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,
Chapter 3: Program Statements
Logical Operators & Truth Tables.
Chapter 3: Program Statements
Chapter 3: Program Statements
Chapter 3: Program Statements
Chapter 3: Program Statements
Logic of an if statement
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Program Statements
CSC 1051 – Data Structures and Algorithms I
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Chapter 3: Program Statements
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 (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed  by John Lewis and William Loftus to accompany  by John Lewis and William Loftus to accompany D Java Software Solutions: Foundations of Program Design, Second Edition and  by Mark Hutchenreuther for CSC-101 at Cal Poly, SLO.  by Mark Hutchenreuther for CSC-101 at Cal Poly, SLO. D

CSC Formatting Output - review  The DecimalFormat class can be used to format a floating point value in generic ways D For example, you can specify that the number be printed to three decimal places  The constructor of the DecimalFormat class takes a string that represents a pattern for the formatted number D See CircleStats.java, which uses 0.### where: CircleStats.java 0 means show the leading 0 if the value is less than 1, and 0 means show the leading 0 if the value is less than 1, and ### means round to three decimal places. ### means round to three decimal places.

CSC DecimalFormat versus NumberFormat D DecimalFormat does use the new operator to instantiate it. See CircleStats.java See CircleStats.javaCircleStats.java D NumberFormat does not use the new operator... See Price.java See Price.javaPrice.java

CSC Flow of Control D Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written D Some programming statements modify that order, allowing us to: decide whether or not to execute a particular statement, or decide whether or not to execute a particular statement, or perform a statement over and over repetitively perform a statement over and over repetitively D The order of statement execution is called the flow of control

CSC Conditional Statements D A conditional statement lets us choose which statement will be executed next D Therefore they are sometimes called selection statements D Conditional statements give us the power to make basic decisions D Java's conditional statements are the if statement, the if-else statement, and the switch statement

CSC Logic of an if statement condition evaluated false statement true

CSC Logic of an if-else statement condition evaluated statement1 true false statement2

CSC The if Statement D The if statement has the following syntax: if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.

CSC The if Statement D 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 true, the assignment statement is executed. If it is not, the assignment statement is skipped. Either way, the call to println is executed next. D See Age.java Age.java

CSC The if Statement D 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 true, the assignment statement is executed. If it is not, the assignment statement is skipped. Either way, the call to println is executed next. D See Age.java Age.java

CSC Block Statements D Several statements can be grouped together into a block statement D A block is delimited by braces ( { … } ) D A block statement can be used wherever a statement is called for in the Java syntax D For example, in an if-else statement, the if portion, or the else portion, or both, could be block statements D See Guessing.java Guessing.java

CSC Another if Statement D Another example of an if statement: if (sum > MAX) { delta = sum - MAX; System.out.print ("Delta is " + delta + ".\t"); } 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 true, them the assignment statement and the first S.o.p are executed. If it is not, the assignment statement is skipped. Either way, the call to println is executed next.

CSC Boolean Expressions D 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 D Note the difference between these: the equality operator ( == ) the equality operator ( == ) the assignment operator ( = ) the assignment operator ( = )

CSC Logical Operators D Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR D They all take boolean operands and produce boolean results D Logical NOT is a unary operator (it has one operand), but logical AND and logical OR are binary operators (they each have two operands)

CSC Logical NOT D The logical NOT operation is also called logical negation or logical complement  If some boolean condition a is true, then !a is false; if a is false, then !a is true D Logical expressions can be shown using truth tables a true false !a false true

CSC Logical AND and Logical OR D The logical and expression a && b is true if both a and b are true, and false otherwise D The logical or expression a || b is true if a or b or both are true, and false otherwise

CSC Truth Tables D A truth table shows the possible true/false combinations of the terms  Since && and || each have two operands, there are four possible combinations of true and false a true false b true false true false a && b true false a || b true false

CSC The if-else Statement D An else clause can be added to an if statement to make it an if-else statement: if ( condition ) statement1; else statement2; D See Wages.java Wages.java D If the condition is true, statement1 is executed; if the condition is false, statement2 is executed D One or the other will be executed, but not both

CSC Nested if Statements D The statement executed as a result of an if statement or else clause could be another if statement D These are called nested if statements D See MinOfThree.java MinOfThree.java D An else clause is matched to the last unmatched if (no matter what the indentation implies)

CSC Comparing Characters D We can use the relational operators on character data D The results are based on the Unicode character set D The following condition is true because the character '+' comes before the character 'J' in Unicode: if ('+' < 'J') System.out.println ("+ is less than J"); D The uppercase alphabet (A-Z) and the lowercase alphabet (a-z) both appear in alphabetical order in Unicode

CSC Comparing Strings D Remember that a character string in Java is an object D We cannot use the relational operators to compare strings  The equals method can be called on a string to determine if two strings contain exactly the same characters in the same order  The String class also contains a method called compareTo to determine if one string comes before another alphabetically (as determined by the Unicode character set)

CSC Comparing Floating Point Values  We also have to be careful when comparing two floating point values ( float or double ) for equality  You should rarely use the equality operator ( == ) when comparing two floats D In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal D Therefore, to determine the equality of two floats, you may want to use the following technique: if (Math.abs (f1 - f2) < ) System.out.println ("Essentially equal.");

CSC Increment and Decrement Operators D The increment and decrement operators are arithmetic and operate on one operand  The increment operator ( ++ ) adds one to its operand  The decrement operator ( -- ) subtracts one from its operand D The statement count++; is essentially equivalent to count = count + 1;

CSC Increment and Decrement Operators D The increment and decrement operators can be applied in prefix form (before the variable) or postfix form (after the variable) D When used alone in a statement, the prefix and postfix forms are basically equivalent. That is, count++; is equivalent to ++count;

CSC Increment and Decrement Operators D When used in a larger expression, the prefix and postfix forms have a different effect D In both cases the variable is incremented (decremented) D But the value used in the larger expression depends on the form: Expression count++ ++count count-- --count Operation add 1 subtract 1 Value of Expression old value new value old value new value

CSC Increment and Decrement Operators  If count currently contains 45, then total = count++; assigns 45 to total and 46 to count  If count currently contains 45, then total = ++count; assigns the value 46 to both total and count

CSC Assignment Operators D Often we perform an operation on a variable, then store the result back into that variable D Java provides assignment operators to simplify that process  For example, the statement  For example, the statement num += count; is equivalent to is equivalent to num = num + count; D For CPE101, however, avoid those assignment operators: they tend to obscure what you are really doing, so... they tend to obscure what you are really doing, so... they are a major source of logical errors, and they are a major source of logical errors, and they make your code harder for others to quickly understand. they make your code harder for others to quickly understand.

CSC The Conditional Operator D Java has a conditional operator that evaluates a boolean condition that determines which of two other expressions is evaluated D The result of the chosen expression is the result of the entire conditional operator D Its syntax is: condition ? expression1 : expression2 D If the condition is true, expression1 is evaluated; if it is false, expression2 is evaluated

CSC The Conditional Operator D The conditional operator is similar to an if-else statement, except that it is an expression that returns a value D For example: larger = (num1 > num2) ? num1 : num2;  If num1 is greater that num2, then num1 is assigned to larger ; otherwise, num2 is assigned to larger D The conditional operator is ternary, meaning that it requires three operands

CSC The Conditional Operator D Another example: System.out.println ("Your change is " + count + (count == 1) ? "Dime" : "Dimes");  If count equals 1, then "Dime" is printed  If count is anything other than 1, then "Dimes" is printed