Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.

Similar presentations

Presentation on theme: "CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3."— Presentation transcript:

1 CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3

2 CSM-Java Programming-I Lesson-1 Objectives Review of last class Statements and Blocks if-else Comparing values and objects switch while, do-while for break, labels, continue

3 CSM-Java Programming-I Lesson-1 Statements Expression Statement: Those that are terminated by a semi-colon. Assignment Expression: Those that contain =. Declaration statements: Those that declare a variable and initialize it to a value. Method calls and control flow statements. Object creation expressions. Prefix or postfix forms of ++ and --.

4 CSM-Java Programming-I Lesson-1 Block A block statement groups together several statements, by enclosing them in braces {}. A block can be used where any single statement is allowed because a block is a compound statement.

5 CSM-Java Programming-I Lesson-1 if-else The if-else statement lets a program carry out different actions depending on the outcome of a condition. The syntax is: if (boolean-expression) statement1 else statement2 The boolean expression is evaluated first. If its value is true, then statement1 is executed; otherwise, if there is an else clause, statement2 is executed. The else clause is optional.

6 CSM-Java Programming-I Lesson-1 Comparing Values The relational operators are used to compare values. Eg: >, >=, <, <=, ==. != The == operator tests for equality. To compare strings, use the equals method, not the == operator. if (str1.equals(str2)) tests if they are equal to each other. if (str1 == str2) tests whether the two string variables refer to the identical string object.

7 CSM-Java Programming-I Lesson-1 Comparing Strings To ignore the letter case, use the equalsIgnoreCase method. if (str1.equalsIgnoreCase(str2)) To compare strings in dictionary order, use compareTo method. if (str1.comapreTo(str2)) < 0 // str1 comes before the str2 in the dictionary. Eg: str1 =“Harry” and str2=“Hell”. if ( str1.compareTo(str2)) > 0 // str1 comes after str2 in the dictionary if (str1.comapreTo(str2)) == 0 // str1 equals str2

8 CSM-Java Programming-I Lesson-1 Comparing objects The == operator tests whether two object references are identical. To compare the contents of the objects, use the equals method. Rectangle cerealBox = new Rectangle(5,10,20,30); Rectangle r = cerealBox; Rectangle oatmealBox = new Rectangle(5,10,20,30); cerealBox == r is true cerealBox == oatmealBox is false cerealBox.equals(oatmealBox); is true The null reference refers to no object. if (account == null ) is valid.

9 CSM-Java Programming-I Lesson-1 switch A sequence of if/else/else that compares a single integer value against several constant alternatives can be implemented as a switch statement. int digit; ……….. switch (digit) { case 1: Sytem.out.print(“one”); break; case 2: Sytem.out.print(“two”); break; case 3: Sytem.out.print(“three”); break; default: Sytem.out.print(“error”); break; }

10 CSM-Java Programming-I Lesson-1 switch For if statement the same example would be: int digit; …….. if (digit == 1) System.out.print(“one”); else if (digit == 2) System.out.print(“two”); else if (digit == 2) System.out.print(“three”); else System.out.print(“error”);

11 CSM-Java Programming-I Lesson-1 switch The test cases (in the above example “digit”) in a switch statement must be integers or characters. switch (name) { case “one”: …break; //error …… } If the break is missing, execution falls through to the next branch, and so on, until finally a break or end of the switch is reached.

12 CSM-Java Programming-I Lesson-1 Boolean Expressions Complex tests can be performed using the operators &&, || and !. if (0 < amt <1000)… // Error if (0 < amt && amt <1000)… // correct //tests if amt greater than 0 and less than 1000. if (ch == ‘S’ || ‘M)… // Error if (ch == ‘S’ || ch == ‘M)… // Correct // tests if the ch is ‘S’ or ‘M’.

13 CSM-Java Programming-I Lesson-1 Boolean Expressions if (!input.equals(“S))…. // tests if the string input is not equal to “S”. private boolean married; // test of boolean in if if (married) …… else …….

14 CSM-Java Programming-I Lesson-1 Logical operations ABA || B trueanytrue falsetrue false ABA &&B true false anyfalse De-Morgan’s Law !(A && B) is same as !A || !B !(A || B) is same as !A && !B A!A truefalse true

15 CSM-Java Programming-I Lesson-1 while A while statement executes a block of code repeatedly. A termination condition controls how often the loop is executed. while (condition) statements If the condition is true then the loop will never end.

16 CSM-Java Programming-I Lesson-1 while – Infinite loops A common reason for infinite loops is forgetting to advance the variable that controls the loop. int years = 0; while (years < 20) { double interest = balance * rate/100; balance = balance + interest; } // value of years is always 0

17 CSM-Java Programming-I Lesson-1 while – Infinite loops Another common reason for infinite loop is accidentally incrementing a counter that should be decremented (or vice versa). int years = 20; while (years > 0) { years++; // should have been years--; double interest = balance * rate / 100; balance = balance + interest; }

18 CSM-Java Programming-I Lesson-1 do-while Sometimes you want to execute the body of a loop at least once and perform the loop test after the body was executed. do statement while (condition); The statement is executed while the condition is true. The condition is tested after the statement is executed, so the statement is executed at least once.

19 CSM-Java Programming-I Lesson-1 do-while Suppose you want to make sure that a user enters a positive number. double value; do { // keep prompting the user for a positive number as // long as the user enters a negative number. } while (value <= 0); You need to get the user input before you can test it.

20 CSM-Java Programming-I Lesson-1 for The for statement is used to loop over a range of values from beginning to end. for (init-expr; condition; incr-expr) statement; This is equivalent to init-expr; while (condition) { statement incr-expr; }

21 CSM-Java Programming-I Lesson-1 for for (i = 1; i <= n; i++) { statements……. } The initialization and iteration statements of a for loop can be a comma-separated list of expressions. for (;;)- infinite for loop statement

22 CSM-Java Programming-I Lesson-1 Labels Statements can be labeled. Labels are typically used on blocks and loops. A label precedes a statement. label : statement Java has no goto construct to transfer control to an arbitrary statement in a method. Use a labeled break statement for purpose.

23 CSM-Java Programming-I Lesson-1 Labels outerloop: while (outer loop condition) {……. while (inner loop condition) {…… if (condition) break outerloop; } jumps here once the if condition is true.

24 CSM-Java Programming-I Lesson-1 break A break statement is used to exit from any block, not just from a switch. It can be used to exit a while, for or a do loop. public void breakLoop(String str) { int i = 0; while (i < 100) { if (i == 10) break; // terminate loop if i is 10 System.out.println(“i: “ + i); i++; }

25 CSM-Java Programming-I Lesson-1 continue A continue statement skips to the end of a loop's body and evaluates the condition that controls the loop. It has meaning only inside a loops. It forces early iteration of a loop.

26 CSM-Java Programming-I Lesson-1 continue class Continue { public static void main (String args[]) { for (int i=0; I < 10; i++) { System.out.print(i + “ “); if (i%2 == 0) continue; System.out.println(“”); }

27 CSM-Java Programming-I Lesson-1 continue In this example continue causes two numbers to be printed on each line. The % operator checks if i is even. If it is, the loop continues without printing a newline. Result is : 0 1 2 3 4 5 6 7 8 9

Download ppt "CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3."

Similar presentations

Ads by Google