Chap 7. Advanced Control Statements in Java

Slides:



Advertisements
Similar presentations
1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Advertisements

Slide 1 of 64 Lecture C Lesson 3: Conditions and Loops Unit 1: The if Statement.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
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.
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
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
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Unit 3 1 Flow of control H We will talk about: conditional statements  if-then-else  logical and conditional operators  switch repetition statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
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.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Control statements Mostafa Abdallah
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CprE 185: Intro to Problem Solving (using C)
The switch Statement, and Introduction to Looping
Chapter 6 More Conditionals and Loops
Boolean Expressions and If
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Chapter 6 More Conditionals and Loops
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Chapter 3: Program Statements
With Assignment Operator
Chapter 3: Program Statements
3.5- The while Statement The while statement has the following syntax:
What output is produced by the following fragment?
CprE 185: Intro to Problem Solving (using C)
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
CSCI 1100/1202 February 6, 2002.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Chap 7. Advanced Control Statements in Java Chapter 7

More Operators There are many more in Java to make use of: increment and decrement operators logical operators assignement operators the conditional operator Chapter 7

The Increment and Decrement Operators The increment operator (++) adds one to its integer or floating point operand The decrement operator (--) subtracts one The statement count++; is essentially equivalent to count = count + 1; Chapter 7

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

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

Logical Operators There are three logical operators in Java: They all take boolean operands and produce boolean results Logical NOT is unary (one operand), but logical AND and OR are binary (two operands) Operator ! && || Operation Logical NOT Logical AND Logical OR Chapter 7

Logical Operators Conditions in selection statements and loops can use logical operators to form more complex expressions if (total < MAX && !found) System.out.println ("Processing..."); Logical operators have precedence relationships between themselves and other operators Chapter 7

Logical Operators Full expressions can be evaluated using truth tables total < MAX false true found !found && !found Chapter 7

Assignment Operators Often we perform an operation on a variable, then store the result back into that variable Java provides assignment operators that simplify that process For example, the statement num += count; is equivalent to num = num + count; Chapter 7

Assignment Operators There are many such assignment operators, always written as op= , such as: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y Chapter 7

Assignment Operators The right hand side of an assignment operator can be a complete expression The entire right-hand expression is evaluated first, then combined with the additional operation Therefore result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num); Chapter 7

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

The Conditional Operator It is similar to an if-else statement, except that it is an expression that returns a value 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 The conditional operator is ternary, meaning it requires three operands Chapter 7

The Conditional Operator Another example: System.out.println ("Your change is " + count + (count == 1) ? "Dime" : "Dimes"); If count equals 1, "Dime" is printed, otherwise "Dimes" is printed Chapter 7

More Repetition Constructs In addition to while loops, Java has two other constructs used to perform repetition: the for statement the do statement Each loop type has its own unique characteristics You must choose which loop type to use in each situation Chapter 7

The for Statement The syntax of the for loop is which is equivalent to for (intialization; condition; increment) statement; which is equivalent to initialization; while (condition) { increment; } Chapter 7

The for Statement Like a while loop, the condition of a for statement is tested prior to executing the loop body Therefore, a for loop will execute zero or more times It is well suited for executing a specific number of times, known in advance Note that the initialization portion is only performed once, but the increment portion is executed after each iteration Chapter 7

The for Statement initialization false condition true statement increment Chapter 7

The for Statement Examples: for (int count=1; count < 75; count++) System.out.println (count); for (int num=5; num <= total; num *= 2) { sum += num; System.out.println (sum); } Chapter 7

The for Statement Each expression in the header of a for loop is optional If the initialization is left out, no initialization is performed If the condition is left out, it is always considered to be true, and therefore makes an infinite loop If the increment is left out, no increment opertion is performed Both semi-colons are always required Chapter 7

The do Statement The do statement has the following syntax: do while (condition); The statement is executed until the condition becomes false It is similar to a while statement, except that its termination condition is evaluated after the loop body Chapter 7

The do Statement statement condition false true Chapter 7

The do Statement The key difference between a do loop and a while loop is that the body of the do loop will execute at least once If the condition of a while loop is false initially, the body of the loop is never executed Another way to put this is that a while loop will execute zero or more times and a do loop will execute one or more times Chapter 7

Another Selection Statement The if and the if-else statements are selection statements, allowing us to select which statement to perform next based on some boolean condition Another selection construct, called the switch statement, provides another way to choose the next action The switch statement evaluates an expression, then attempts to match the result to one of a series of values Execution transfers to statement list associated with the first value that matches Chapter 7

The switch Statement The syntax of the switch statement is: switch (expression) { case value1: statement-list1 case value2: statement-list2 case … } Chapter 7

The switch Statement The expression must evaluate to an integral value, such as an integer or character The break statement is usually used to terminate the statement list of each case, which causes control to jump to the end of the switch statement and continue A default case can be added to the end of the list of cases, and will execute if no other case matches Chapter 7

The break and continue statements The break statement, which we used with switch statements, can also be used inside a loop When the break statement is executed, control jumps to the statement after the loop (the condition is not evaluated again) A similar construct, the continue statement, can also be executed in a loop When the continue statement is executed, control jumps to the end of the loop and the condition is evaluated Chapter 7

The break and continue Statements They can calso be used to jump to a line in your program with a particular label Jumping from one point in the program to another in an unstructured manner is not good practice Therefore, as a rule of thumb, avoid the break statement except when needed in switch statements, and avoid the continue statement altogether Chapter 7