JAVA programs

Java Program Using if statement

In this program we see the use of the  if statement. If is  a flow control statement that operate on particular condition given in it. Use if statement to execute a block of Java code if a condition is true.

Program

class IfDemo

{

        public static void main(String args[])

        {

            int limit=50;

            System.out.println("printing even number betqween 1 to 50");

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

            {

                if(i%2==0)

                {

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

                }

            }

        }

}

When Run The Program Following Output:

See in following output window

Description

 

Use if statement to execute a block of Java code if a condition is true.                                                                                                                  

Syntax of if

if (condition) {

  // block of code to be executed if the condition is true

}

For ex:

if(a> b)

{

System.out.println(“a is greater”);

}

In above example if a is greater that time only the statement “a is greater” will print.

 

Releted Programs

© 2017 codewmAll rights reserved.