Introduction
Java is a set of statements which executed sequentially in the order in which they appear. This will happen when option or repetitions of certains are not necessary. However, We have a number of situation, where we may have to change the order of execution of statement on bases of certain conditions. This involves a kind of dicision making to see the particular condition occured or not and the direction of execution will execute according to it.
When program braks the sequential flow and jump on the another part of code then it's called branching. When branching is on particular condition then it's called conditional branching. If branches executes without any particular conditions then it's call unconditional branching.
Java supports such decision making capabilities and supports following statements known as Control statement or Decision making statement.
- if statements
- switch statement
- conditional operator statement.
1. if statement:
The if statement is powerful decision making statement and is use to control the flow of execution of statements.It is basically two-way decision statement base on condition or expression. If statement may be implemented in different forms depending on complexity of conditions to be tested.
- Simple if statement
- if....else statement
- Nested if.....else statement
- else if ladder.
syntax:
if(text condition){
statement-block
}
statements;
The statement-block may be a single statement or group of statements. If the text condition is true then statement-block will executed, otherwise statement-block will skipped and other statemnts will executed.
Fig. Simple if controlif....else statement:
The if....else statement is an extension of simple if statement. The general form is:
if(text condition){
true-block;
}
else{
false block;
}
statement-x;
If the text condition is true, then true blok will execute otherwise false block is executed after that the statement-x will executed.
nested if....else statement:
Comments
Post a Comment