C7nControlStatements
C7nControlStatements
a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement.
If statement
If statement consists a condition, followed by statement or a set of statements
as shown below:
if(condition){
Statement(s);
}
The statements get executed only when the given condition is true. If the
condition is false then the statements inside if statement body are completely
ignored.
if(condition_1) {
Statement1(s);
if(condition_2) {
Statement2(s);
}
}
Statement1 would execute if the condition_1 is true. Statement2 would only
execute if both the conditions (condition_1 and condition_2) are true.
if(condition) {
Statement(s);
}
else {
Statement(s);
}
The statements inside “if” would execute if the condition is true, and the
statements inside “else” would execute if the condition is false.
if-else-if Statement
if-else-if statement is used when we need to check multiple conditions. In this
statement we have only one “if” and one “else”, however we can have multiple
“else if”. It is also known as if else if ladder. This is how it looks:
if(condition_1) {
/*if condition_1 is true execute this*/
statement(s);
}
else if(condition_2) {
/* execute this if condition_1 is not met and
* condition_2 is met
*/
statement(s);
}
else if(condition_3) {
/* execute this if condition_1 & condition_2 are
* not met and condition_3 is met
*/
statement(s);
}
.
.
.
else {
/* if none of the condition is true
* then these statements gets executed
*/
statement(s);
}
Note: The most important point to note here is that in if-else-if statement, as
soon as the condition is met, the corresponding set of statements get
executed, rest gets ignored. If none of the condition is met then the
statements inside “else” gets executed.
Example of if-else-if
public class IfElseIfExample {
}
}
}
First step: In for loop, initialization happens first and only one time, which
means that the initialization part of for loop only executes once.
Third step: After every execution of for loop’s body, the increment/decrement
part of for loop executes that updates the loop counter.
Fourth step: After third step, the control jumps to second step and condition
is re-evaluated.
class ForLoopExample3 {
public static void main(String args[]){
int arr[]={2,11,45,9};
//i starts with 0 as array index starts with 0 too
for(int i=0; i<arr.length; i++){
System.out.println(arr[i]);
}
}
}
Let’s take the same example that we have written above and rewrite it
using enhanced for loop.
class ForLoopExample3 {
public static void main(String args[]){
int arr[]={2,11,45,9};
for (int num : arr) {
System.out.println(num);
}
}
}
Note: The important point to note when using while loop is that we need to use
increment or decrement statement inside while loop so that the loop variable
gets changed on each iteration, and at some point condition returns false. This
way we can end the execution of while loop otherwise the loop would execute
indefinitely.
Syntax:
continue word followed by semi colon.
continue;
System.out.print(j+" ");
}
}
}
a) Use break statement to come out of the loop instantly. Whenever a break
statement is encountered inside a loop, the control directly comes out of loop
and the loop gets terminated for rest of the iterations. It is used along with if
statement, whenever used inside loop so that the loop gets terminated for a
particular condition.
The important point to note here is that when a break statement is used inside
a nested loop, then only the inner loop gets terminated.
b) It is also used in switch case control. Generally all cases in switch case are
followed by a break statement so that whenever the program control jumps to
a case, it doesn’t execute subsequent cases (see the example below). As
soon as a break is encountered in switch-case block, the control comes out of
the switch-case body.