The Anatomy of an IF statement
|
In programming, there are cases when you need the program to perform a certain function when a certain condition is met. If such conditions are met then a certain piece of code is executed, otherwise skip it and execute an alternative code. This is achieved by using what is called conditional statements. Among these conditional statements are; the IF Statement, the While statement, the DO-While statement and the conditional operator.
We will be addressing all these conditional blocks one at a time in order to make sure that you get a detailed understanding of how they work. This tutorial is focused on the IF statement. Bellow is a diagram that shows how the IF statement works:

Deciphering the IF statement
As you can see from the diagram above, an expression is first evaluated. If that expression returns a TRUE value then whatever statement that follows the (if) is executed. If the expression returns FALSE then the statement or block following the (if) is skipped, and if there is an else following that statement whatever that follows the (else) is executed.
For example:
if(expression)
value1 = "Management";
value2 = "Service";
In this case if the expression returns TRUE value1 will be assigned the value "Management" and then the program will go on and assign the value "Service" to value2. In other words, value2 will be assigned the value "Service" regardless of whether the expression returns TRUE or FALSE.
If we wanted to assign both values only when the expression return TRUE, we will have to enclose both statements in braces like this:
if(expression){
value1 = "Management";
value2 = "Service";
}
The Conditional Operator
Another very simple and easy to use conditional statement is what is called the conditional operator (?). The general syntax for the operator is:
logical_expression ? expression1 : expression2
What happens here is that, the logical expression is evaluated, if it returns TRUE then expression1 is executed, otherwise expression2 is executed. It works more like the IF statement only that it is much easier to remember the syntax and can be used in more complex expressions.
|
RELATED ARTICLES: Introduction to Java programming Computer program as we learned in the posting Introduction to Programming
What the cash flow statement tell us The cash flow statement is a financial document that shows how changes in balance sheet and income statements affect cash and cash equivalents. As an analytical tool, the cash flow statement is useful in determining the short-te
Starting to program in Java Most java developers would describe it as an innovative programming language that enables a developer to write what is called "Applets" or pieces of java code that can be embedded in web pages. In addition to this benefit, compiled java code can be run on a variety of
Compiling and executing a java program Java source code is always stored in a file with a .java file extension. Once you have created your source code, you simply save it with a .java extension and it is ready to be compiled. To compile your java code, you use the f
Java variables and data types A variable is defined as a named piece of memory that a java program uses to store data. Each of these storage pieces can only store data of a certain type. In other words, if you defined as variable of type integer, that variable can only hold inte
Breaking out of loops with break statement div class='floatleft'> So far, we have discussed how the while and for loops work. Assuming that you were looping through a set of values, dividing each value by a number between -10 and 10. Given that you are aware of the rule that "Anything divided by zero
| |
|