You are on page 1of 3

IF/ELSE SELECTION

Perform action only when condition is true Perform different specified action when condition is false Its like Conditional operator (? :) Nested if/else selection structures

SWITCH
Switch structure Used for multiple selections Difference Switch & If Switch is used for only matching (=) & when the cases r in great number but if is used for any condition = =, <=. Conditions and Operators The condition consists of an expression that evaluates to a truth-value, true or false. These types of expressions are also known as Boolean expressions. A simple Boolean expression compares the value of two data items. A simple Boolean expression is composed of two data items and a relational operator to compare the two data items. There are six relational operators: Equal, == Not equal, != Less than, < Less or equal to, <= Greater than, > Greater or equal to, >= Examples of simple conditions that can be expressed with the relational operators in KJP are: x >= y time ! = start_t

For
This structure has a start value, an end, and an increase. Syntax For (int x=0; x<5; x++)

While
The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

while (expression) { statement(s) } The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be accomplished as in the following While program:

class While { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } }

Do - while

The Java programming language also provides a do-while statement, which can be expressed as follows:

do { statement(s) } while (expression); The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhile program:
class DoWhile { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } }

You might also like