Java Control Statements
Java Control Statements
Pranaya
Java Control Statements
Pranaya
* if statements
Simple if statement
if-else statement
if-else-if ladder
Nested if-statement
* switch statement
2- Loop statements
do while loop
while loop
for loop
for-each loop
Simple if-statement
SYNTAX:
Pranaya
if(condition) {
statement 1; //executes when condition is true
}
Example:
Output:
pass
The if-else statement
is an extension to the if-statement, which uses another block of code,
i.e., else block. The else block is executed if the condition of the if-
block is evaluated as false.
Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Pranaya
Example
}
if-else-if :
The if-else-if statement contains the if-statement followed by
multiple else-if statements. In other words, we can say that it is the
chain of if-else statements that creates a decision tree where the
program may enter the block of code where the condition is true.
We can also define an else statement at the end of the chain.
Syntax:
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
Pranaya
}
Example
Nested if-statement
Pranaya
Example
if(address.endsWith("India")) {
if(address.contains("Meerut")) {
System.out.println("Your city is Meerut");
}
else if(address.contains("Noida")) {
System.out.println("Your city is Noida");
}
else {
System.out.println(address.split(",")[0]);
}
}
else {
System.out.println("You are not living in India");
}
}
}
Output:- Delhi
Switch Statement:
Pranaya
4. The break statement terminates the switch block when the
condition is satisfied.
5. It is optional, if not used the next case is executed.
6. While using switch statements, we must notice that the case
expression will be of the same type as the variable. However, it
will also be a constant value.
Syntax
1. switch (expression){
2. case value1:
3. statement1;
4. break;
5. .
6. .
7. .
8. case valueN:
9. statementN;
10. break;
11. default:
12. default statement;
13. }
example
Pranaya
break;
case "usa":
System.out.println("capital of usa is Washingtone-Dc");
break;
case "uae":
System.out.println("capital of uae is abu dhabi");
break;
case "australia":
System.out.println("capital of australia is canberra");
break;
default:
System.out.println("There is no information about this name");
}
}
}
Output:
capital of nepal is Kathmandu
Loop Statements
1. while loop
2. for loop
3. do-while loop
for loop
Pranaya
When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:
Syntax
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Example
Syntax:
Example
Pranaya
3.// TODO Auto-generated method stub
4.String[] names = {"Java","C","C++","Python","JavaScript"};
5.System.out.println("Printing the content of the array names:\n");
6.for(String name:names) {
7.System.out.println(name);
8.}
9.}
10. }
Output:
Printing the content of the array names:
Java
C
C++
Python
JavaScript
While Loop
Pranaya
System.out.println(i);
i++;
}
}
}
Output: 0 1 2 3 4
Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute
the code block once, before checking if the condition is true, then it
will repeat the loop as long as the condition is true.
Syntax
do {
// code block to be executed
}
while (condition);
Example
Pranaya
a++;
}
while(a<=3);
Output: nice
nice
nice
nice
Thank
y !
o u
Pranaya !