This statement acts as a conditional control statement of Java and checks the specified condition. If the given condition is true the given instruction is executed otherwise the instruction is ignored. There are different formats of if statement and is widely used for computation, logical reasoning and decision making within a program. A simple if statement has the following format : if (expression)
statement1;
where an expression is the condition which if found true executes statement1 otherwise the control comes to the next statement.
attention@computerscienceexpertise.com PDF created with pdfFactory Pro trial version www.pdffactory.com
IF – ELSE Statement
This conditional control statement of Java conducts the execution of the statement if the condition is true and conducts other specified execution if the condition is false. It consists of an if statement, followed by a statement or block of statements, followed by the keyword else, followed by another statement or block of statements. It has the following syntax : if (expression)
statement 1;
else
statement 2;
If the expression is true, the statement 1 is executed otherwise the statement 2 is executed. The statement 1 and statement 2 can be a single statement or a block/compound statements.
attention@computerscienceexpertise.com PDF created with pdfFactory Pro trial version www.pdffactory.com
Switch Statement
It has the following format :
switch(expression)
{
case value1:
block1
break;
case value2:
block2
break;
———default:
block
break;
}The break statement used at the end of each case acts as an exit point from the switch statement. It works as a jumping statement and the execution is transferred to the statement following the switch statement. The Default statement is the executing condition in the condition when the value of switch does not match with the case. It does not require any usage of break as it is always the last segment of the switch structure.
attention@computerscienceexpertise.com PDF created with pdfFactory Pro trial version www.pdffactory.com
Difference Between
SWITCH / IF-ELSE
i). The IF-ELSE statement is used for manipulating multiple conditions whereas SWITCH touches upon only one conditional variable. And different values of the variable passes the control to the required case. (ii). The IF-ELSE statement uses both integer as well as floating type values whereas the SWITCH statement only accepts integer values. (iii). In SWITCH statement all the branches or compound statements are executed depending on the value of the same variable on the other hand IF-ELSE follows usage of long instruction based statements and may create confusion within blocks.
attention@computerscienceexpertise.com PDF created with pdfFactory Pro trial version www.pdffactory.com
While Statement of JAVA
The while statement is another looping statement of Java with an entry control option for execution.
It acts as one of the simplest looping structures of Java and has the following format.
Java Learning Made Easy
Start from the beginning
