Page 185 - Cs_withBlue_J_C11_Flipbook
P. 185

Statements inside loop;
                    }
                    1st statement after loop;
                   For example:
                   To Print the average of the first five even numbers.
                    class average
                    {
                    public static void main()
                        {
                            int i, s=0, c=0;
                            double avg;
                            for(i=1; i<=10; i++)
                            {
                                if(i%2==1)
                                {
                                    continue;
                                }
                                s=s+i;
                                c++;


                            }
                            avg=s/c;
                            System.out.println("Average of even numbers "+ avg);
                        }
                    }
                   The output of the preceding program is as follows:
                   Average of even numbers 6.0
                 •  return: In Java, when a method is called, the control is transferred to the method and it must return back to the
                   caller module after the execution of the method. This is done by the return statement. If the method does not
                   return any value, we must use the keyword “void” else we can use the datatype of the returned value.
                   Syntax:
                    return function_name(arguments)
                    {
                        Statements;
                        return statement;
                    }
                   For example:
                   To return the sum of the digits of the given number.
                    import java.util.*;
                    class sumofdigits
                    {
                        int sod(int n)
                        {



                                                                                                                       183
                                                                                                 Statements and Scope  183
   180   181   182   183   184   185   186   187   188   189   190