Page 34 - DCAP312_WEB_TECHNOLOGIES_II
P. 34

Web Technologies-II



                   Notes         of execution of other statements, create an object, or assign a value to a variable, property, or
                                 field. Statements are usually terminated by a semicolon.
                                 Statements can be grouped into comma-separated statement lists or brace-enclosed statement
                                 blocks.

                                 Examples
                                 int sampleVariable; // declaring a variable
                                 sampleVariable = 5; // assigning a value
                                 SampleClass sampleObject = new SampleClass(); // constructing a new object
                                 sampleObject.SampleInstanceMethod(); // calling an instance method
                                 SampleClass.SampleStaticMethod(); // calling a static method
                                 // executing a “for” loop with an embedded “if” statement
                                 for(int i = 0; i < upperLimit; i++)
                                   {
                                    if (SampleClass.SampleStaticMethodReturningBoolean(i))
                                    {
                                     sum += sampleObject.SampleMethodReturningInteger(i);
                                    }
                                   }
                                 Statement Blocks

                                 A series of statements surrounded by curly braces form a block of code. Among other purposes,
                                 code blocks serve to limit the scope of variables defined within them. Code blocks can be nested
                                 and often appear as the bodies of methods, the protected statements of a try block, and the code
                                 within a corresponding catch block.

                                 private void MyMethod ()
                                 {
                                 // This block of code is the body of “MyMethod()”
                                 CallSomeOtherMethod();
                                 try
                                 {
                                 // Here is a code block protected by the “try” statement.
                                 int variableWithLimitedScope;
                                 // “variableWithLimitedScope” is accessible in this code block.
                                 }
                                 Catch (Exception)
                                    {
                                     // Here is yet another code block.
                                     // “variableWithLimitedScope” is not accessible here.
                                    }
                                     // “variableWithLimitedScope” is not accessible here, either.
                                    CallYetAnotherMethod ();
                                     // Here ends the code block for the body of “MyMethod ()”.
                                 }


        28                                LOVELY PROFESSIONAL UNIVERSITY
   29   30   31   32   33   34   35   36   37   38   39