Conditional and Looping Statements

Conditional and Looping Statements

Java If Statement
 

Syntax : If Statement 


Example:

if (booleanExpression) {

    statement(s)

}

 

Syntax : If-Else Statement 

Example:

if (booleanExpression) {

    statement(s)

} else {

    statement(s)

}

 

Syntax : If-Else Ladder Statement

 

Example:

if (booleanExpression1) {

    // statements

} else if (booleanExpression2) {

    // statements

}

...

else {

     // statements

}

 

 

Java for loop

 


 

Syntax For Loop : Java Programming

  

Example:

for (initialization; condition ; increment) {

    Statement1

    Statement2

    .

    .

    .

    StatementN

}

 
Java while loop

 

Syntax : 

Example:

while(condition) {

  // body of loop
}

 

 

Syntax : Do-While Loop

Example:

do {

     Statement1;

     Statement2;

     Statement3;

     .

     .

     .

     .

     StatementN;

} while (expression);

  

Java do-while loop

 

Syntax : Do-While Loop

Example:

do {

     Statement1;

     Statement2;

     Statement3;

     .

     .

     .

     .

     StatementN;

} while (expression);

 

 

     EXERCISE: 

1) Java program to find largest of three numbers
2)
Java program to swap two numbers
3)
Java program to find factorial
4)
Java program to print Floyd's triangle
5)
Java program to reverse a string
6)
Java program to check palindrome
7)
Infinite For loop Example
8)
Prime Numbers Java Example
9)
List Odd Numbers Java Example
10)
Fibonacci Series Java Example

 

 

 

1)  Java program to find largest of three numbers

 

import java.util.Scanner; 
classLargestOfThreeNumbers
{
   public static void main(String args[])
   {
      int x, y, z;
      System.out.println("Enter three integers ");
      Scanner in = new Scanner(System.in);
 
      x = in.nextInt();
      y = in.nextInt();
      z = in.nextInt();
 
      if ( x > y && x > z )
         System.out.println("First number is largest.");
      else if ( y > x && y > z )
         System.out.println("Second number is largest.");
      else if ( z > x && z > y )
         System.out.println("Third number is largest.");
      else   
         System.out.println("Entered numbers are not distinct.");
   }
}

 

2)  Java program to swap two numbers

import java.util.Scanner;
class SwapNumbers
{
   public static void main(String args[])
   {
      int x, y, temp;
      System.out.println("Enter x and y");
      Scanner in = new Scanner(System.in);
 
      x = in.nextInt();
      y = in.nextInt();
 
      System.out.println("Before Swappingnx = "+x+"ny = "+y); 
      temp = x;
      x = y;
      y = temp; 
      System.out.println("After Swappingnx = "+x+"ny = "+y);
   }
}

 

3)  Java program to find factorial

 

import java.util.Scanner;
 class Factorial
{
   public static void main(String args[])
   {
      int n, c, fact = 1;
 
      System.out.println("Enter an integer to calculate it's factorial");
      Scanner in = new Scanner(System.in);
 
      n = in.nextInt(); 
      if ( n < 0 )
         System.out.println("Number should be non-negative.");
      else
      {
         for ( c = 1 ; c <= n ; c++ )
            fact = fact*c;
          System.out.println("Factorial of "+n+" is = "+fact);
      }
   }
}

 

      4) Java program to print Floyd's triangle

import java.util.Scanner; 
class FloydTriangle
{
   public static void main(String args[])
   {
      int n, num = 1, c, d;
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter the number of rows of floyd's triangle you want");
      n = in.nextInt();
 
      System.out.println("Floyd's triangle :-");
 
      for ( c = 1 ; c <= n ; c++ )
      {
         for ( d = 1 ; d <= c ; d++ )
         {
            System.out.print(num+" ");
            num++;
         
         System.out.println();
      }
   }
}

 

 

              5)  Java program to reverse a string

 

import java.util.*; 
class ReverseString
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in); 
      System.out.println("Enter a string to reverse");
      original = in.nextLine(); 
      int length = original.length();
 
      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);
 
      System.out.println("Reverse of entered string is: "+reverse);
   }
}

 

               6) Java program to check palindrome

 

import java.util.*; 
class Palindrome
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in); 
      System.out.println("Enter a string to check if it is a palindrome");
      original = in.nextLine();
       int length = original.length();
 
      for ( int i = length - 1; i >= 0; i-- )
         reverse = reverse + original.charAt(i);
 
      if (original.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
         System.out.println("Entered string is not a palindrome."); 
   }
}

 

         7) Infinite For loop Example

  public class InfiniteForLoop {

   

    public static void main(String[] args) {

            /*

       * Its perfectely legal to skip any of the 3 parts of the for loop.

       * Below given for loop will run infinite times.

       */

       for(;;)

         System.out.println("Hello");

       

       /*

        * To terminate this program press ctrl + c in the console.

        */

    }

  }   

 

           8) Prime numbers java Example

  public class GeneratePrimeNumbersExample {   

          public static void main(String[] args) {                

                  //define limit

                  int limit = 100;

                 

                  System.out.println("Prime numbers between 1 and " + limit);

              //loop through the numbers one by one

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

                         

                          boolean isPrime = true;

                         

                          //check to see if the number is prime

                          for(int j=2; j < i ; j++){

                                 

                                  if(i % j == 0){

                                          isPrime = false;

                                          break;

                                  }

                          }

                          // print the number

                          if(isPrime)

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

                  }

          }

  }

 

 

          9) List Odd Numbers Java Example

     public class ListOddNumbers {

             public static void main(String[] args) {

                   //define the limit

                  int limit = 50;

                 

            System.out.println("Printing Odd numbers between 1 and " + limit);

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

                  //if the number is not divisible by 2 then it is odd

                  if( i % 2 != 0){

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

                      }

                  }

          }

  }  

 

 

         10) Fibonacci Series Java Example

 

    public class JavaFibonacciSeriesExample {

   

          public static void main(String[] args) {

                 

                  //number of elements to generate in a series

                  int limit = 20;

              long[] series = new long[limit];

                      //create first 2 series elements

                  series[0] = 0;

                  series[1] = 1;

                 

                  //create the Fibonacci series and store it in an array

                  for(int i=2; i < limit; i++){

                          series[i] = series[i-1] + series[i-2];

                  }

                 

                  //print the Fibonacci series numbers

                 

                  System.out.println("Fibonacci Series upto " + limit);

                  for(int i=0; i< limit; i++){

                          System.out.print(series[i] + " ");

                  }

          }

  }