Page 161 - computer science (868) class 11
P. 161

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)
                        {



                                                                                                                       159
                                                                                                 Statements and Scope  159
   156   157   158   159   160   161   162   163   164   165   166