• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Md04 flow control
 

Md04 flow control

on

  • 837 views

 

Statistics

Views

Total Views
837
Views on SlideShare
837
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Md04 flow control Md04 flow control Presentation Transcript

            • Module 4
            • Expressions and Flow Control
      • Objectives
        • • Distinguish between instance and local variables
        • • Describe how instance variables are initialized
        • • Identify and correct a possible reference before
        • assignment compiler error
        • • Recognize, describe, and use Java software operators
        • • Distinguish between legal and illegal assignments of
        • primitive types
      • Objectives
        • • Identify boolean expressions and their requirements
        • in control constructs.
        • • Recognize assignment compatibility and required casts in fundamental types.
        • • Use if, switch, for, while, and do constructions
        • and the labeled forms of break and continue as flow
        • control structures in a program
      •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    • Instanceof operator : (Example InstanceOf.java ) Test the class of an object at runtime, the left argument can be any variable or array element whereas the right operand must be a class,interface or array type. The instanceof operator return true if the class of left argument is the same as, or is some subclass of the class specified by right argument. The right operand may be an interface. In such case the test determines if the object at left argument implement the specified interface.
    •  
    •  
    •  
    • Example IfElse.java
      • Consider this code
        • boolean boo = false;
        • if (boo = true) { }
      • What would be the output?
      • 1. The code compiles and runs fine, and the if test fails because
      • boo is false.
      • 2. The code won’t compile because you’re using an assignment (=)
      • rather than an equality test (==).
      • 3. The code compiles and runs fine and the if test succeeds because
      • boo is SET to true (rather than TESTED for true) in the if argument!
    •  
    •  
      • Will the following codes compile
      • 1. byte g = 2;
          • switch(g) {
          • case 23:
          • case 128:
          • }
      • 2. int temp = 90;
        • switch(temp) {
        • case 80 : System.out.println("80");
        • case 80 : System.out.println("80");
        • case 90 : System.out.println("90");
        • default : System.out.println("default");
        • }
      • 3. switch(new Integer(4)) {
      • case 4: System.out.println("boxing is OK");}
      • 1.
        • Attempting to compile the code gives you an error something like
        • Test.java:6: possible loss of precision
        • found : int
        • required: byte
        • case 128:
      • 2.
        • Block of code won't compile because it uses two cases with the
        • same value of 80:
      • 3.
      • It is legal to leverage the power of boxing in a switch expression.
    •  
      • Enhanced for Loop
      • Instead of having three components, the enhanced for has two .
      • Looping through an array
      • The basic (old) way
        • int [] a = {1,2,3,4};
        • for(int x = 0; x < a.length; x++) // basic for loop
        • System.out.print(a[x]);
      • Enhanced for
        • for(int n : a) // enhanced for loop
        • System.out.print(n);
    • The two pieces of the for statement are declaration The newly declared block variable, of a type compatible with the elements of the array you are accessing. This variable will be available within the for block, and its value will be the same as the current array element. expression This must evaluate to the array you want to loop through. This could be an array variable or a method call that returns an array. The array can be any type: primitives, objects, even arrays of arrays.
    •  
    •  
    •  
    •  
    •  
      • Example
        • class Continue {
        • public static void main(String args[]) {
        • for(int i=0; i<10; i++) {
        • System.out.print(i + &quot; &quot;);
        • if (i%2 == 0) continue;
        • System.out.println(&quot;&quot;);
        • }
        • }
        • }
    • Example BreakLoop4.java
      • Example
        • class Break {
        • public static void main(String args[]) {
        • boolean t = true;
        • first: {
        • second: {
        • third: {
        • System.out.println(&quot;Before the break.&quot;);
        • if(t) break second; // break out of second block
        • System.out.println(&quot;This won't execute&quot;);
        • }
        • System.out.println(&quot;This won't execute&quot;);
        • }
        • System.out.println(&quot;This is after second block.&quot;);
        • }
        • }
        • }
    • Example ContinueLabel.java