Java branching statement and its example?

 



 Java branching statement

o   ( if )  statement (if-else) statement and  (else – if  ) statement

 

Q.1] What is a Statement?

·         If the statement is a conditional selection statement that is used to execute the program through two different paths.

§  Syntax:-  if  ( condition) –condition must return a Boolean value  

{

//all statement is executed if the condition is true

}

 else     {

//statement is executed if condition false.

}

omean the condition is all time a Boolean value return if true condition then statements 1 execute and false condition statement2 execute.

o   Or condition a no { string, char, int, float, long, double value } do not declare .and no logical value declare.

Q.3 ] write an if-else statement using the program?

Example:-   class Example    {

   public static void main(String  args[])  {

int  a=10;  

if   (a>5)  {

System.out.println(“ the value of grater than 5”)

}

    else  {

System.out.println(“ the value of less than 5”)

}

}

}

o   Now we know that if block a condition true then if block all statement are print. and if the condition are false then else block all statements are executed.

·         Using block in if conditions:-

1.      If we are adding multiple statements after  if and else statement then we must warp our code inside a block

2.      By using blocks our program looks readable as well as in a proper format.

3.      If we are using blocks then we can add multiple statements after the conditional if and else statement but we are not using the blocks we cannot add a declarative statement after the conditional statement if and else.

Q.4] write a java program which well check a number is prime or not?

Example        class  PrimeNumber   {

                          Public static void main(String   args[])    {

                            Int   x=10;

                                if (x% 2 == 0)  {

                                            System.out.println(“ the value of prime :-”  +x)

                                           }

                                    else  {

                                            System.out.println(“ the value not prime :-”  +x)

                                           }

                                }

            }

Post a Comment

0 Comments