Monday, June 26, 2023

Java Programming Constructs

 

Chapter 3:  Programming Constructs  

If

if (condition)

        statement1;

if (condition)

        statement2;


Example 1:

File name:IfDemo.java

class IfDemo{

     public static void main(String args[]){

    int a=10;

    if(a>0)

    {

         System.out.println("A is positive");

     }

    if(a<0)

   {

         System.out.println("A is Negative");

    }

   }

}

The output appears as given below:


If else

if (condition)

        statement1;

else

        statement2;

Example2:

File name: IfDemo2.java

class IfDemo2{

            public static void main(String args[]){

               int a,b,c;

               a=20;

               b=34;

               c=25;

               if (a>b&&a>c){

                        System.out.println("A is bigger");

          }

               else{

                      if(b>c){

                                 System.out.println("B is bigger");

             }

                      else{

                                 System.out.println("C is bigger");

                 }

          }

         }

      }

Ø The output appears as given below:

 


Nested if's

A nested if is if statement that is the target of another if or else. Nested if's are very common in programming.

File name : NestedIf.java

class NestedIf{

            public static void main(String args[]){

               int m=74;

              

               if (m>=50){

          if(m>=80){

                        System.out.println("A Grade");

             }

          else{

           System.out.println("B Grade");

          }

          }

               else{

                      if(m>=30){

                                 System.out.println("C Grade");

             }

                      else{

                                 System.out.println("Fail");

                 }

          }

         }

      }

Output



The if-else-if Ladder

A common programming construct that is based upon a sequence of nested if's is the if-else-if.

It looks like this:

       if(condition)

                  statement;

       else if (condition)

                  statement;

        else if (condition)

                 statement;

                 ...

        else

               statement;


Ø Example 3:

File name: ElseIfLadder.java

class ElseIfLadder{

           public static void main(String args[]){

                      int mark=65;

                      if(mark>=90){

                                 System.out.println("A Grade");

          }

                      else if(mark >=80){

                              System.out.println("B Grade");

          }       

                    else if(mark >=70){

                         System.out.println("C Grade");

          }

                else if (mark >=60){

                     System.out.println("D Grade");

          }

          else{ 

                     System.out.println("F Grade");

          }

 

     }

}

The output appears as given below:

 


The Conditional operator:

An alternative to use if and  else statement is to use the conditional operator, sometimes called the ternary operator.

The conditional operator is a ternary operator because it has three terms.

Syntax:

test?true:false

Example:

File name: ConOprDemo.java

class ConOprDemo

{

public static void main (String args[])

{

int x=25;

int y=10;

int z=0;

//The  smaller value is returned

z=x>y?x:y;

System.out.println("The value is"+z);

     }

}

The output appears as given below:



Swich case:

syntax

Switch(test){

 case value One:

       resultOne;

       break;

case value Two:

       result Two;

       break;

case value Three:

       result Three;

       break;

      -------

default:

    default result;

}

Example 1:

File name: SwitchDemo.java

import java.util.*;

class SwitchDemo

{

public static void main(String args[])

{

       int val1,val2;

        char opr;

        Scanner sc=new Scanner(System.in);

           val1 = sc.nextInt();

         val2 = sc.nextInt();

        opr = sc.next().charAt(0);

       switch (opr)

       {

case '+':

        System.out.println("Sum = " + (val1+val2));

        break;

case '-':

        System.out.println("Difference = "+ (val1-val2));

        break;

case '*':

        System.out.println("Product = " + (val1*val2));

        break;

case '/':

        System.out.println("Quotient = " + (val1/val2));

        break;

default:

         System.out.println("Invaild choice");

       }

}

}

Output:



Looping Statements:

Java, iteration statement are for, while, and do-while. These statements create what we commonly call loops. As you probably know, a loops. As you probably know, a loop repeatedly executes the same set of instructions until a termination condition is met.

For:

The for loop, repeats a statement or block of statements some number of times until a condition is matched. for loops are frequently used for simple iteration in which you repea a block of statements a certain number of times and then stop, but you can use for loops for just about any kind of loop.

The for loop in Java looks roughly like this:

Syntax

for(initialization; condition checking;increment/decrement){

   statement;

}

Ø Example :

FFile name: ForDemo.java

class ForDemo

{

public static void main (String args[])

{

  for (int i = 1; i<=5;i++)

  {

        System.out.print("\t"+i);

  }

}

}

Ø The output appears as given below:



While loop

 

Syntax:

 

While (condition){

     bodyOfLoop;

}

 

Ø Example:

   File name: WhileDemo.java

class WhileDemo{

public static void main (String args[]){

          int nos = 1;

          int x = 1;

           System.out.println("First five odd nos:\n");

while(nos<=5)

{

    System.out.print("\t"+x);

    x=x+2; 

 nos = nos+1;

   }

}

}

Ø The output appears as given below:



DO........WHILE LOOPS

 The do loop is just like a while loop, except that do executes a given statement or block until the condition is false. The main difference is that while loops test the condition before looping, making it possible that the body of the loop will never execute if the condition is false the first time it's tested.  do loops run the body of the loop at least once before testing the condition. because the condition  to evaluate is given at the end. do loops look like this:

Syntax:

do{

          bodyOfLoop;

}while(condition);

Example

File name: DoDemo.java

class DoDemo

{

          public static void main (String args[])

{

int x=1;

do{

   System.out.println("Looping,round"+x);

    x++;

     }while (x<=10);

}

}

Ø The output  appears as given below:

Break

Ø Example:

FFile name: BreakDemo.java

class BreakDemo

{

 public static void main(String args[])

{

int i=0;

while(i<10)

{

if(i==5)

{

 break;

}

else

{

System.out.println(i);

}

i++;

 }

  }

}

Ø The output appears as given below:


Continue
File name : ContinueDemo.java

class ContinueDemo

{

 public static void main(String args[])

{

int i=0;

for(i=0;i<10;i++)

{

if(i==5)

{

 continue;

}

System.out.println(i);

 

 }

  }

}


Output


Labeled Loops

 File name: BreakLoopDemo.java

class BreakLoopDemo{

                  public static void main(String args[]){

                 boolean t =true;

                 first: {

                               second: {

                                           third: {

                        System.out.println("Before the break.");

                                                    if(t)

                                                                             break second;

                                                                             System.out.println("This won't execute");

                             }

                            

                             System.out.println("This won't execute");

                                                 }

System.out.println("This is after second block.");

                     }

          }

}

Ø The output appears as given below:



0 comments:

Post a Comment