1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.

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
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.
Chapter 3: Program Statements Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, All rights.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering
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.
Unit 3 1 Flow of control H We will talk about: conditional statements  if-then-else  logical and conditional operators  switch repetition statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Program Elements -- Introduction
Chapter 3: Program Statements
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.
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.
© 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.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
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.
© 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)
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
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)
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 3: Program Statements
Chapter 3: Program Statements
Chapter 3: Program Statements
Outline Altering flow of control Boolean expressions
Chapter 3: Program Statements
Chapter 3: Program Statements
Chapter 3: Program Statements
3.5- The while Statement The while statement has the following syntax:
Chapter 3: Program Statements
Comparing Data & the ‘switch’ Statement
Outline Software Development Activities
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Chapter 3: Program Statements
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)
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:

1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3

2 Boolean Expressions b 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  Note the difference between the equality operator ( == ) and the assignment operator ( = )

3 Logical Operators b Boolean expressions can also use logical operators: ! Logical NOT && Logical AND || Logical OR b operands and result are boolean

4 Logical NOT b logical negation or logical complement  If boolean condition a is true, then !a is false; if a is false, then !a is true b Logical expressions can be shown using truth tables a true false !a false true

5 Logical AND and Logical OR b Logical and: a && b true if both a and b are true, and false otherwise b Logical or: a || b true if a or b or both are true, and false otherwise

6 Truth Tables b 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

7 Logical Operators b Conditions in selection statements and loops can use logical operators to form complex expressions if (total < MAX && !found) System.out.println ("Processing…"); total < MAX false true found false true false true !found true false true false total < MAX && !found false true false

8 More Operators b increment and decrement operators: ++, -- b assignment operators: +=, *=,... b conditional operator

9 Increment and Decrement Operators  The increment operator ( ++ ) adds one to its operand  The decrement operator ( -- ) subtracts one from its operand b The statement count++; is essentially equivalent to count = count + 1;

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

11 Increment and Decrement Operators b “prefix” and “postfix” refer to when the value of the variable gets incremented/decremented relative to when it is used in the expression. Prefix increment: increment first, then usePrefix increment: increment first, then use Postfix increment: use, then incrementPostfix increment: use, then increment similarly with decrementsimilarly with decrement

12 Increment and Decrement Operators  Example: Suppose.  Example: Suppose count = 1. b Complete the following table: Statement x = count++ x = ++count x = count-- x = --count count x

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

14 Assignment Operators b There are many assignment operators, including the following: 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

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

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

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

18 The switch Statement b The switch statement provides another means to decide which statement to execute next b The switch statement evaluates an expression, then attempts to match the result to one of several possible cases b Each case contains a (constant) value and a (possibly empty) list of statements b The flow of control transfers to first statement in list associated with the first value that matches and continues thereon

19 The switch Statement b The general syntax of a switch statement is: switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... }switchandcasearereservedwords If expression matches value2, control jumps to here

20 The switch Statement b Often a break statement is used as the last statement in each case's statement list b A break statement causes control to transfer to the end of the switch statement b If a break statement is not used, the flow of control will continue into the next case b Sometimes this can be helpful, but usually we only want to execute the statements associated with one case

21 The switch Statement b A switch statement can have an optional default case  The default case has no associated value and simply uses the reserved word default b If the default case is present, control will transfer to it if no other case value matches b Though the default case can be positioned anywhere in the switch, it is usually placed at the end b If there is no default case, and no other value matches, control falls through to the statement after the switch

22 The switch Statement b The expression of a switch statement must result in an integral data type, like an integer or character; it cannot be a floating point value b Note that the implicit boolean condition in a switch statement is equality - it tries to match the expression with a value b You cannot perform relational checks with a switch statement b See

23 Repetition Statements b Repetition statements allow us to execute a statement multiple times repeatedly b They are often simply referred to as loops b Like conditional statements, they are controlled by boolean expressions b Java has three kinds of repetition statements: the while loop, the do loop, and the for loop

24 The while Statement b The while statement has the following syntax: while ( condition ) statement; while is a reserved word If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repeatedly until the condition becomes false.

25 Logic of a while loop statement true condition evaluated false

26 The while Statement  if condition of while statement is false initially, the statement is never executed b Therefore, the body of a while loop will execute zero or more times b See Counter.java (page 133) Counter.java (page 133)Counter.java (page 133) b See Average.java (page 134) Average.java (page 134)Average.java (page 134) b See WinPercentage.java (page 136) WinPercentage.java (page 136)WinPercentage.java (page 136)

27 Infinite Loops  The body of a while loop must eventually make the condition false b If not, it is an infinite loop, which will execute until the user interrupts the program b See Forever.java (page 138) Forever.java

28 Nested Loops b Similar to nested if statements, loops can be nested as well b See PalindromeTester.java (page 137) PalindromeTester.java

29 The do Statement b The do statement has the following syntax: do { statement; } while ( condition ) Uses both the do and whilereservedwords The statement is executed once initially, then the condition is evaluated The statement is repetitively executed until the condition becomes false

30 Logic of a do loop true condition evaluated statement false

31 The do Statement b A do loop is similar to a while loop, except that the condition is evaluated after the body of the loop is executed b Therefore the body of a do loop will execute at least one time b See Counter2.java (page 143) Counter2.java

32 Comparing the while and do loops statement true condition evaluated false while loop true condition evaluated statement false do loop

33 The for Statement b The for statement has the following syntax: for ( initialization ; condition ; increment ) statement;Reservedword The initialization portion is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration

34 The for Statement b A for loop is equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }

35 Logic of a for loop statement true condition evaluated false increment initialization

36 The for Statement b Like a while loop, the condition of a for statement is tested prior to executing the loop body b Therefore, the body of a for loop will execute zero or more times b It is well suited for executing a specific number of times that can be determined in advance b See Counter3.java (page 146) Counter3.java b See Multiples.java (page 147) Multiples.java b See Stars.java (page 150) Stars.java

37 The for Statement b Each expression in the header of a for loop is optional If the initialization is left out, no initialization is performedIf the initialization is left out, no initialization is performed If the condition is left out, it is always considered to be true, and therefore creates an infinite loopIf the condition is left out, it is always considered to be true, and therefore creates an infinite loop If the increment is left out, no increment operation is performedIf the increment is left out, no increment operation is performed b Both semi-colons are always required in the for loop header

38 Program Development b The creation of software involves four basic activities: establishing the requirementsestablishing the requirements creating a designcreating a design implementing the codeimplementing the code testing the implementationtesting the implementation b The development process is much more involved than this, but these basic steps are a good starting point

39 Requirements b Requirements specify the tasks a program must accomplish (what to do, not how to do it) b They often include a description of the user interface b An initial set of requirements are often provided, but usually must be critiqued, modified, and expanded b It is often difficult to establish detailed, unambiguous, complete requirements b Careful attention to the requirements can save significant time and money in the overall project

40 Design b An algorithm is a step-by-step process for solving a problem b A program follows one or more algorithms to accomplish its goal b The design of a program specifies the algorithms and data needed b In object-oriented development, the design establishes the classes, objects, and methods that are required b The details of a method may be expressed in pseudocode, which is code-like, but does not necessarily follow any specific syntax

41 Implementation b Implementation is the process of translating a design into source code b Most novice programmers think that writing code is the heart of software development, but it actually should be the least creative step b Almost all important decisions are made during requirements analysis and design b Implementation should focus on coding details, including style guidelines and documentation b See ExamGrades.java (page 155) ExamGrades.java

42 Testing b A program should be executed multiple times with various input in an attempt to find errors b Debugging is the process of discovering the cause of a problem and fixing it b Programmers often erroneously think that there is "only one more bug" to fix b Tests should focus on design details as well as overall requirements