Java Questions Part5 - Programs on If Else, For Loop

1.Write a Java program to demonstrate if..else statement ?

Answer: The if statement executes a certain section of code if the test expression is evaluated to true. The if statement may have an optional else block. Statements inside the body of else statement are executed if the test expression is evaluated to false.

Syntax:
if (expression) {
   // codes
}
else {
  // some other code
}

Program:
class IfElse {

   public static void main(String[] args) {     

      int number = 10; 

      if (number > 0) {

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

      }

      else {

         System.out.println("Number is not positive.");

      } 

      System.out.println("This statement is always executed.");

   }

}

Output:
Number is positive.

This statement is always executed.

2. Write a Java program to demonstrate nested if …else statement?

Answer:  It's possible to have if..else statements inside a if..else statement in Java. It's called nested if...else statement.
Here's a program to find largest of 3 numbers:
class Number {

    public static void main(String[] args) {

        Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber;

        if (n1 >= n2) {

            if (n1 >= n3) {

                largestNumber = n1;

            } else {

                largestNumber = n3;

            }

        } else {

            if (n2 >= n3) {

                largestNumber = n2;

            } else {

                largestNumber = n3;

            }

        }

        System.out.println("Largest number is:  " + largestNumber);

    }

}

Output:
Largest number is:  4.5

3. Write a Java program to demonstrate for loop ?

Answer:  Loop is used in programming to repeat a specific block of code until certain condition is met (test expression is false). Loops are what makes computers interesting machines. Imagine you need to print a sentence 50 times on your screen. Well, you can do it by using print statement 50 times (without using loops). How about you need to print a sentence one million times? You need to use loops.

Syntax:
for (initialization; testExpression; update)
{
    // codes inside for loop's body
}

Program:
// Program to print a sentence 10 times
class Loop {

   public static void main(String[] args) {

      

      for (int i = 1; i <= 10; ++i) {

         System.out.println("Line " + i);

      }

   }

}

Output:
Line 1

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10

4. Write a Java program to demonstrate while loop?

Answer:  How while loop works? The test expression inside parenthesis is a boolean expression.
If the test expression is evaluated to true,
statements inside the while loop are executed.
then, the test expression is evaluated again.
This process goes on until the test expression is evaluated to false.
If the test expression is evaluated to false,
while loop is terminated.

Syntax:

while (testExpression) {
    // codes inside body of while loop
}

Program:

// Program to print line 10 times
class Loop {

   public static void main(String[] args) { 

      int i = 1; 

      while (i <= 10) {

         System.out.println("Line " + i);

         ++i;

      }

   }

}

Output:
Line 1

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10

5. Write a Java program to print the alphabets using for loop?

Answer:
public class Characters {

    public static void main(String[] args) {

        char c;

        for(c = 'A'; c <= 'Z'; ++c)

            System.out.print(c + " ");

    }

}

Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


6. Write a Java program to demonstrate for each loop?

Answer: If you are working with arrays and collections, you can use alternative syntax of for loop (enhanced form of for loop) to iterate through items of  arrays/collections. It is also referred as for-each loop because the loop iterates through each element of array/collection.

Program:
class AssignmentOperator {

   public static void main(String[] args) { 

      char[] vowels = {'a', 'e', 'i', 'o', 'u'};

      // foreach loop

      for (char item: vowels) {

         System.out.println(item);

      }

   }

}

Output:
a

e

i

o

u


7. Write a Java program to print stars using for loop, where the number of stars printed should be equal to the row number?

Answer:
public class JavaProgram

{

    public static void main(String args[])

    {

        int i, j;

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

        {

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

            {

                System.out.print("* ");

            }

            System.out.println();

        }

    }

}

Output:
* 

* * 

* * * 

* * * * 

* * * * *

8. Write a Java program to demonstrate the usage of break statement inside while loop?

Answer:
* Java Program Example - Java break Statement
*  Use break to exit a while loop */
public class JavaProgram

{   

    public static void main(String args[])

    { 

        int i = 0; 

        while(i<100)

        {

            if(i == 10)

                break;

            System.out.println("i is " + i);

            i++;

        } 

        System.out.println("Loop completed."); 

    }

}

Output:
i is 0

i is 1

i is 2

i is 3

i is 4

i is 5

i is 6

i is 7

i is 8

i is 9

Loop completed.

9. Write a Java program to demonstrate the usage of continue statement inside while loop?

Answer:
//Java Program to demonstrate the use of continue statement  
//inside the while loop.  
public class ContinueWhileExample {  

public static void main(String[] args) {  

    //while loop  

    int i=1;  

    while(i<=10){  

        if(i==5){  

            //using continue statement  

            i++;  

            continue;//it will skip the rest statement  

        }  

        System.out.println(i);  

        i++;  

    }  

}  

}  

Output:
1

2

3

4

6

7

8

9

10


No comments:

Post a Comment