Your SlideShare is downloading. ×
Md04 flow control
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Md04 flow control

695

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
695
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1.
          • Module 4
          • Expressions and Flow Control
  • 2.
    • 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
  • 3.
    • 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
    •  
  • 4.  
  • 5.  
  • 6.  
  • 7.  
  • 8.  
  • 9.  
  • 10.  
  • 11.  
  • 12.  
  • 13. 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.
  • 14.  
  • 15.  
  • 16.  
  • 17. Example IfElse.java
  • 18.
    • 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!
  • 19.  
  • 20.  
  • 21.
    • 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");}
  • 22.
    • 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.
  • 23.  
  • 24.
    • 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);
  • 25. 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.
  • 26.  
  • 27.  
  • 28.  
  • 29.  
  • 30.  
  • 31.
    • 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;);
      • }
      • }
      • }
  • 32. Example BreakLoop4.java
  • 33.
    • 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;);
      • }
      • }
      • }
  • 34. Example ContinueLabel.java

×