Control Statements
Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure
Sequence structure The computer executes java statements one after the other in the order in which they are written. Add grade to total Add 1 to counter Total = total +grade; Counter = counter + 1;
Selection statements If statement (single-selection statement) If else statement (double-selection statement) Switch statement (multiple-selection statement)
If & If else statements If single-selection statement If else double-selection statement It select or ignores a single action. Ex: if(grade >= 60) System.out.println( passed ); It select between two different actions Ex: if(grade >= 60) System.out.println( passed ) ; else System.out.println( failed ) ;
Conditional operator(?:) Conditional operator(?:) can be used in place of an if else statement. This operator is called ternary operator this mean it takes three operands. System.out.println( grade>=60? passed : failed ); Boolean expression Value if the boolean expression is true Value if the boolean expression is false
Example using conditional operator public static void main (String args[]) { int n1=3,n2=4; System.out.println( n2> n1? 5+1 : 3+2); System.out.println((n2 <n1? 2 : 3) * 4); } output: 6 12
A program can test multiple cases by placing if else statements inside other if else statements to create nested if else statements Ex: if(grade>=90) System.out.println( A ); else if(grade>=80) System.out.println( B ); else if (grade >=70) System.out.println( C ); else if (grade >= 60) System.out.println( D ); else System.out.println( F );
dangling-else problem the java compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces({ }) if (x > 5) if( y > 5) System.out.println ( x and y are > 5 ); else System.out.println ( x is <= 5 ); if (x > 5) { if( y > 5) System.out.println ( x and y are > 5 ); } else System.out.println ( x is <= 5 );
Switch statements perform different action based on the possible values of an integer variable or expression. switch (num) { Case1: System.out.println ( 1 ); Case 2: System.out.println ( 2 ); Case 3: System.out.println ( 3 ); Default: System.out.println ( nothing ); } Num =1 num=3 1 3 2 nothing 3 nothing
Example using switch statement { } switch (num) Default: System.out.println ( nothing ); Case1: System.out.println ( 1 ); break; Case 2: System.out.println ( 2 ); break; Num =1 num=3 1 nothing 1 switch (num) { Case 1: Case 2: Case 3: System.out.println ( ok ); } Num =1 ok num=3 ok
Conditional AND Operator (&&) Conditional OR Operator ( ) Logical Negation Operator (!) Logical operators Boolean Logical AND (&) Boolean Logical OR ( ) Boolean Logical Exclusive OR (^)
Conditional AND operator(&&) Expression 1 Expression 2 Expression 1 && expression 2 false false false false true false true false false true true true
Conditional OR Operator( ) Expression 1 Expression 2 Expression 1 && expression 2 false false false false true true true false true true true true
Example using AND/OR Operator int a =5, b=7; if (a==10 && b>=3) a=a + b; else ++a; // a=6 int a=5, b=7; if (a==5 b<3) a=a + b; // a =12 Note: The parts of an expression containing &&/ operator are evaluated only until it is known whether the condition is true or false. This feature is called short-circuit evaluation
Boolean Logical AND(&) and OR( ) Operator This operators work identically to the (&&/ )operators, with one exception: the boolean logical operators always evaluate both of their operands (they don t perform short-circuit evaluation). This is useful if the right operand of the boolean logical (&/ ) has a required a modification of a variable s value. For example, the expression (birthday == true) (++ age >=65) Guarantees that the condition (++ age >= 65) will be evaluated. Thus the variable age is increment in the preceding expression, regardless of whether the overall expression is true or false.
Example using &/ operator int x=4,y=9; if (x>= 5 && ++y == 10) System.out.println ( ok ); System.out.println (y); int x=4,y=9; if (x>= 5 & ++y == 10) System.out.println ( ok ); System.out.println (y); 9 10 int i = 1, j = 5; if (i>= 1 j++ > 5) System.out.println (j); int i = 1, j = 5; if (i>= 1 j++ > 5) System.out.println (j); 5 6
Boolean Logical Exclusive OR (^) Expression 1 Expression 2 Expression 1 && expression 2 false false false false true true true false true true true false This operator is also guaranteed to evaluate both of its operand.
Logical Negation Operator(!) expression! expression false true true false if(!( grade == 60)) is equivalent to If ( grade!= 60 ) ex: if(!( grade == 60)) System.out.println(grade);
Repetition statements It enables program to perform statements repeatedly as long as a condition remains true There are three repetition statements (looping statements) While statement Do While statement For statement
While/Do While statements While statements Do While statements int product = 3; While (product <= 100) Product = 3*product; int counter = 1; Do { System.out.println(counter); ++counter; } while (counter <= 10);
Do while repetition statements is similar to the while statements whereas: In the while statement, the program tests the condition at the beginning of the loop before executing the loops body. If the condition is false, the body never executes, and so the action in the body will be performed zero or more times. Do While statement tests the condition after executing the loop s body; therefore, the body always executes at least once. When a do while statement terminates, execution continues with the next statement in sequence.
For statements It specifies the counter controlled repetition details in a single line of code. Initialization Loop condition Increment for ( int i = 1 ; i <= 10 ; i++) System.out.println(i); Statement
In most cases, for statement can be represented with an equivalent while statement Initialization; While (loop condition) { Statement; Increment; } Example: int i = 1; While (i<= 10) { System.out.println(i); i++; } If the initialization expression in the for header declare the variable, the variable can be used only in that for statements ( it will not exist outside the for statement), and this is called the variable scope
Examples using the for statements Print the variable over the following sequence of values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0. For (int i = 99; i >= 0; i -= 11) System.out.print( i +, ); summing the even integers from 2 to 20 public static void main(string args[]) { int total = 0; For ( int num =2;num<=20;num+=2) Total+=num; System.out.println(total); }
Increment & decrement operators operator called Sample expression explanation ++ Prefix increment ++a Increment (a) by 1, then use the new value of (a) in the expression in which (a) resides. ++ Postfix increment a++ Use the current value of (a) in the expression in which (a) resides, then increment (a) by 1. -- Prefix decrement --b decrement (b) by 1, then use the new value of (b) in the expression in which (b) resides. -- Postfix decreent b-- Use the current value of (b) in the expression in which (b) resides, then decrement (a) by 1.
Public static void main(string args[]) { Int c=5; System.out.println(c++); // print 5 System.out.println(++c); // print 7 System.out.println(c--); // print 7 System.out.println(--c); // print 5 }
Break statement The break statement when executed in a while, for, do while or switch, causes immediate exit from that statement. Public static void main(string arg[]) { int count; For (count=1;count<=10;count++) { if (count==5) Break; System.out.print (count); } } 1234
Continue statement The continue statement, when executed in a while, for, do while, skip the remaining statements in the loop body and proceeds with the next iteration of the loop. Public static void main(string arg[]) { int count; For (count=1;count<=10;count++) { if (count==5) continue; System.out.print (count); } } 1234678910