Page 231 - ComputerScience_Class_11
P. 231

9.  What is a compound statement in Java? How is it used within conditional and iterative statements? Provide an example.
                   Ans.  When a group of statements is written within a curly bracket, then it is known as a compound statement. This group of statements
                       executes together in a sequential manner to provide the result.
                        These statements are useful for all types of conditional and iterative statements.
                        For example,

                       if(a>b && a>c)
                       {
                       System.out.println(a+ "is largest");
                       System.out.println(b + " and" +c+ "are less than" +a);
                       }
                       else
                       if(b>a && b>c)
                       {
                       System.out.println(b+ "is largest");
                       System.out.println(a+ "and" +c+ "are less than" +b);
                       }
                       else
                       {
                       System.out.println(c+ "is largest");
                       System.out.println(a+ "and" +b+ "are less than" +c);
                       }
                    10.  What is a sequential statement in Java? Provide a program that demonstrates the sequential flow of control.
                   Ans.  In  Java,  the  statements  are  executed  sequentially  which  represents  the  default  flow  of  statements.  In  the  default  flow  of
                       statements, the compiler executes the first statement and then goes to the next statement for its execution and so on, unless it
                       receives a conditional or an iterative statement. The sequential statement follows the top-down approach.
                        The following program shows the sequential flow of statements.
                       import java.util.*;
                       class sequential_flow
                       {
                       public static void main()
                       {
                       Scanner sc = new Scanner(System.in);
                       int n, pens=35;
                       double cost1, cost2;
                       System.out.print("Enter cost of 35 pens: ");
                       cost1=sc.nextDouble();
                       System.out.print("Enter number of pens: ");
                       n=sc.nextInt();
                       cost2=cost1/pens*n;
                       System.out.println("Cost of " +n+ " pens: " +Math.round(cost2));
                       }
                       }







                                                                                              Statements and Scope  229
   226   227   228   229   230   231   232   233   234   235   236