JAVATM- The Beginnings

if Statements

Java programs normally execute statements in order from the top of a method to the bottom.


Let’s say that under some circumstances we do not want to execute statement 2. It is possible to make the execution of statement 2 dependent on some condition. Statement 2 would then only execute if the condition were true. This is referred to as conditional logic.

Section 1 - The if Statement

Let’s say we have a program with two variables x and y. The program prints the value of x. In addition, we want to issue a warning if variable y is greater than x. If y is less than or equal to y, we do not want to issue the warning.


                PROGRAM OUTPUT:


The program has placed the warning statement inside an if statement, which begins with an if clause that contains the keyword if followed by a boolean expression enclosed in parentheses. Note that there is no semicolon after the boolean expression.

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.

Back to