Professional Documents
Culture Documents
Codes
Codes
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
Here, condition after evaluation will be either true or false. if statement accepts boolean
values – if the value is true then it will execute the block of statements under it.
If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by default if
statement will consider the immediate one statement to be inside its block. For example,
if(condition)
statement1;
statement2;
Example:
// Java program to illustrate If statement
class IfDemo
{
public static void main(String args[])
{
int i = 10;
if (i > 15)
System.out.println("10 is less than 15");
https://hajsoftutorial.com/simple-statement/
IF ELSE STATEMENT
Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Example:
// Java program to illustrate if-else statement
class IfElseDemo
{
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Output:
i is smaller than 15
https://hajsoftutorial.com/if-statement/
NESTED IF STATEMENT
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
Example:
// Java program to illustrate nested-if statement
class NestedIfDemo
{
public static void main(String args[])
{
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
System.out.println("i is smaller than 15");
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
System.out.println("i is smaller than 12 too");
else
System.out.println("i is greater than 15");
}
}
}
Output:
i is smaller than 15
i is smaller than 12 too
https://hajsoftutorial.com/nested-if-statement/
IF ELSE LADDER
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Example:
// Java program to illustrate if-else-if ladder
class ifelseifDemo
{
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
Output:
i is 20
https://hajsoftutorial.com/if-else-if-ladder/
SWITCH CASE
Syntax:
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
Example:
// Java program to illustrate switch-case
class SwitchCaseDemo
{
public static void main(String args[])
{
int i = 9;
switch (i)
{
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
default:
System.out.println("i is greater than 2.");
}
}
}
Output:
i is greater than 2.
ITERATION
WHILE STATEMENT
while(condition) {
// body of loop
}
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1
1. class WhileDemo {
2. public static void main(String[] args){
3. int count = 1;
4. while (count < 11) {
5. System.out.println("Count is: " + count);
6. count++;
7. }
8. }
9. }
1 class WhileLoop
2{
3 public static void main(String args[])
4 {
5 int num = 5;
6 while (num > 0)
7 {
8 System.out.println("Num " + n);
9 n--;
10 }
11 }
12}
Num 4
Num 3
Num 2
Num 1
DO WHILE STATEMENT
do{
// body of loop
} while(condition);
1. class DoWhileDemo {
2. public static void main(String[] args){
3. int count = 1;
4. do {
5. System.out.println("Count is: " + count);
6. count++;
7. } while (count < 11);
8. }
9. }
1do
2{
3 // body of loop
4}
5while(condition);
do-while example
class DoWhileStatement
{
public static void main(String args
{
int num = 5;
1 class DoWhileStatement
2{
3 public static void main(String args[])
4 {
5 int num = 5;
6 do
7 {
8 System.out.println("num " + n);
9 n--;
10 } while (n > 0);
11 }
12}
Num 5
Num 4
Num 3
Num 2
Num 1
class DoWhileDemo
{
public static void main(String arg[])
{
int x = 1;
do
{
System.out.println("The x value is: " + x);
x++;
} while( x <= 3 ); // LINE A
}
}
OUTPUT
The x value is: 1
The x value is: 2
The x value is: 3
FOR STATEMENT
The program from previous example can be re-written using for loop as:
a = 1
b = 4
a = 2
b = 3
1. class ForDemo {
2. public static void main(String[] args){
3. for(int i=1; i<11; i++){
4. System.out.println("Count is: " + i);
5. }
6. }
7. }
8.
Demo: Iteration statement enhanced for
1. class EnhancedForDemo {
2. public static void main(String[] args){
3. int[] numbers = {1,2,3,4,5,6,7,8,9,10};
4. for (int item : numbers) {
5. System.out.println("Count is: " + item);
6. }
7. }
8. }
The syntax of the for loop statement is
For loop syntax
for(initialization; condition; iteration
{
// body
}
1class ForDemo
2{
3 public static void main(String args[])
4{
5 int num;
6 for (n = 5; n > 0; n--);
7 System.out.println("num " + n);
8 }
9}
Num 5
Num 4
Num 3
Num 2
Num 1
The general syntax can be defined as :-
for(<initial value>;<condition>;<increment>)
class
For
Loop
{
public static void main(String args[])
{
for(int i=1;i<=3;i++)
{
System.out.println(i);
}
}
}
CODE
Try it Online
class ForExample
{
public static void main(String arg[])
{
for(int x = 0; x < 5; x++)
{
System.out.println("x = " + x);
}
System.out.println("After for loop");
}
}
OUTPUT
x=0
x=1
x=2
x=3
x=4
After for loop
explain https://java.meritcampus.com/core-java-topics/for-loop-in-java
Syntax:
for (initialization condition; testing condition;
increment/decrement)
{
statement(s)
}
// Java program to illustrate for loop.
class forLoopDemo
{
public static void main(String args[])
{
// for loop begins when x=2
// and runs till x <=4
for (int x = 2; x <= 4; x++)
System.out.println("Value of x:" + x);
}
}
Output:
Value of x:2
Value of x:3
Value of x:4
NESTED LOOPS
// nested loops
class NestedLoop {
public static void main(String args[]) {
int i, j;
for (i = 0; i < 8; i++) {
for (j = i; j < 8; j++)
System.out.print(".");
System.out.println();
}
}
}
........
.......
......
.....
....
...
..
1 class NestedLoopDemo
2{
3 public static void main(String args[])
4 {
5 int a, b;
6 for (a = 0; a < 8; a++)
7 {
8 for (b = a; b < 8; b++)
9 System.out.print(".");
10 System.out.println();
11 }
12 }
13}
…….
……
…..
….
..
for(element: collection) {
// body
}
void cancelAll(Collection<TimerTask> c) {
for (TimerTask t : c)
t.cancel();
}
1for(element: collection)
2{
3 // body
4}
For-each example
class ForEachExample
{
public static void main(String args[
{
int arr[]={1,2,3,4};
1 class ForEachExample
2{
3 public static void main(String args[])
4 {
5 int arr[]={1,2,3,4};
6 for(int i:arr)
7 {
8 System.out.println(i);
9 }
10
11 }
12}
Output:1
2
3
4