How do I use the if-then statement?

Category: fundamental, viewed: 313 time(s).

The if-then is the most basic control flow statement. It will execute the block of statement only if the test expression evaluated equals to true. Let's see the example below:

package org.kodejava.example.lang;

public class IfThenDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        //
        // If the evaluation of a > b is true than the if block is
        // executed. In the block below we just print that the value
        // of a is bigger than b.
        //
        if (a > b) {
            System.out.println("A(" + a + ") is bigger than B(" + b + ")");
        }

        //
        // When we have only a single command inside a block we can
        // remove the opening and closing braces for the block ({..}).
        // But it is a good practice to always enclose the block with
        // braces.
        //
        if (b < a) 
            System.out.println("B(" + b + ") is smaller that A(" + a + ")");
    }
}

The result of the above program is:

A(10) is bigger than B(5)
B(5) is smaller that A(10)
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats