Download presentation
Presentation is loading. Please wait.
1
Control Statements:
2
Control Structures (Cont.)
Java has three kinds of control structures Sequence statement, Selection statements (three types) and Repetition statements (three types)
3
if Single-Selection Statement
if statements Execute an action if the specified condition is true Example: if (grade >=50) System.out.println(“pass”);
4
if…else Double-Selection Statement
if…else statement Executes one action if the specified condition is true or a different action if the specified condition is false Example: if (grade >=50) System.out.println(“Pass”); else System.out.println(“Failed”);
5
if…else Double-Selection Statement (Cont.)
Nested if…else statements if…else statements can be put inside other if…else statements Example: if (grade >=90) System.out.println(“A”); else if (grade >=80) System.out.println(“B”); if (grade >=70) System.out.println(“C”); if (grade >=60) System.out.println(“D”); System.out.println(“F”);
6
Common Programming Error
Using the value of a local variable before it is initialized results in a compilation error. All local variables must be initialized before their values are used in expressions.
7
Compound Assignment Operators
8
Increment and Decrement Operators
Unary increment and decrement operators Increment operator (++) adds one to its operand Decrement operator (--) subtracts one from its operand Prefix increment (and decrement) operator Changes the value of its operand, then uses the new value of the operand in the expression Postfix increment (and decrement) operator Uses the current value of its operand in the expression in which the operation appears, then changes the value of the operand
9
Fig. 4.15 | Increment and decrement operators.
10
public class Increment
{ public static void main( String args[] ) int c; c=5; System.out.println( c ); System.out.println( c++ ); System.out.println( ++c ); }
11
Counter-Controlled Repetition
Counter-controlled repetition requires: a counter variable to count the number of times a loop is iterated Initial value of the control variable Increment/decrement of control variable through each loop Loop-continuation condition that tests for the final value of the control variable
12
while statement Repeats an action while its loop-continuation condition remains true
15
Fig. 5.3 | for statement header components.
16
for Repetition Statement (Cont.)
for ( initialization; loopContinuationCondition; increment ) statement; can usually be rewritten as: initialization; while ( loopContinuationCondition ) { statement; increment; }
17
Common Programming Error
When a for statement’s control variable is declared in the initialization section of the for’s header, using the control variable after the for’s body is a compilation error.
18
Examples Using the for Statement
Vary control variable from 1 to 100 in increments of 1 for ( int i = 1; i <= 100; i++ ) Vary control variable from 100 to 1 in increments of –1 for ( int i = 100; i >= 1; i-- ) Vary control variable from 7 to 77 in increments of 7 for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in decrements of 2 for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 )
20
do…while Repetition Statement
do…while structure Similar to while structure Tests loop-continuation after performing body of loop i.e., loop body always executes at least once
22
Good Programming Practice
Always include braces in a do...while statement, even if they are not necessary. This helps eliminate ambiguity between the while statement and a do...while statement containing only one statement.
23
Sentinel-Controlled Repetition
int grade, total; total=0; System.out.print( "Enter grade or -1 to quit: " ); grade = input.nextInt(); while ( grade != -1 ) { total = total + grade; }
24
Good Programming Practice
In a sentinel-controlled loop, the prompts requesting data entry should explicitly remind the user of the sentinel value.
25
break and continue Statements
break statement Causes immediate exit from control structure Used in while, for, do…while or switch statements continue statement Skips remaining statements in loop body Proceeds to next iteration Used in while, for or do…while statements
28
Logical Operators Logical operators Java logical operators
Allows for forming more complex conditions Combines simple conditions Java logical operators && (conditional AND) || (conditional OR) ! (logical NOT)
29
Logical Operators (Cont.)
Conditional AND (&&) Operator Consider the following if statement if ( gender == FEMALE && age >= 65 ) ++s; Combined condition is true if and only if both simple conditions are true Combined condition is false if either or both of the simple conditions are false
30
Logical Operators (Cont.)
Conditional OR (||) Operator Consider the following if statement if (( Average >= 90)||(finalExam >= 90 System.out.println(“ grade is A ”); Combined condition is true if either or both of the simple condition are true Combined condition is false if both of the simple conditions are false
31
| Precedence/associatively of the operators discussed so far.
32
public class LogicalOperators
{ public static void main( String args[] ) System.out.println("Conditional AND (&&)\n"+"false && false "+ ( false && false )+ "\nfalse && true "+ ( false && true )+ "\ntrue && false "+ ( true && false )+"\ntrue && true "+ ( true && true ) ); System.out.println("Conditional OR (||)\n"+"false || false "+( false || false )+ "\nfalse || true "+ ( false || true )+"\ntrue || false "+ ( true || false )+"\ntrue || true "+( true || true ) ); System.out.println("\nLogical NOT (!) "+"\n!false "+( !false )+ "\n!true "+ ( !true ) ); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.