Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3."— Presentation transcript:

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

2 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 3 Logical Operators b Boolean expressions can also use logical operators: ! Logical NOT && Logical AND || Logical OR b operands and result are boolean

4 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 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 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 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 8 More Operators b increment and decrement operators: ++, -- b assignment operators: +=, *=,... b conditional operator

9 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 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 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 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 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 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 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 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 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 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 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 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 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 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 http://www.csc.villanova.edu/~map/7000/progs/SwitchExample.java http://www.csc.villanova.edu/~map/7000/progs/SwitchExample.java

23 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 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 25 Logic of a while loop statement true condition evaluated false

26 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 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 28 Nested Loops b Similar to nested if statements, loops can be nested as well b See PalindromeTester.java (page 137) PalindromeTester.java

29 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 30 Logic of a do loop true condition evaluated statement false

31 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 32 Comparing the while and do loops statement true condition evaluated false while loop true condition evaluated statement false do loop

33 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 34 The for Statement b A for loop is equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }

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

36 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 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 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 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 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 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 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


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

Similar presentations


Ads by Google