You are on page 1of 17

IF STATEMENT

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;

// Here if the condition is true, if block


// will consider only statement1 to be inside
// its block.

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");

// This statement will be executed


// as if considers one statement by default
System.out.println("I am Not in if");
}
}
Output:
I am Not in if

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
}

Here is more practical example.

// Demonstrate the while loop.


class While {
public static void main(String args[]) {
int n = 10;
while (n > 0) {
System.out.println("tick " + n);
n--;
}
}
}

The ouput of the program is:

tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1

Demo: Iteration statement while

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. }

While Loop Example


class WhileLoop
{
public static void main(String args
{
int num = 5;

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}

Output will be:


Num 5

Num 4
Num 3

Num 2

Num 1

DO WHILE STATEMENT

do{
// body of loop
} while(condition);

The program presented in previous while statement can be re-written using


do-while as:

// Demonstrate the do-while loop.


class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while (n > 0);
}
}

Demo: Iteration statement do ..while

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. }

Structure of do-while loop


do
{
// body of loop
}
while(condition);

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}

Output will be:

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

Here is the general form of the traditional for statement:

for(initialization; condition; iteration) {


// body
}

The program from previous example can be re-written using for loop as:

// Demonstrate the for loop.


class ForTick {
public static void main(String args[]) {
int n;
for (n = 10; n > 0; n--) System.out.println("tick " + n);
}
}

Here, we've presented the example to illustrate this.

// more than one statement using the comma.


class Comma {
public static void main(String args[]) {
int a, b;
for (a = 1, b = 4; a < b; a++, b--) {
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}

The ouput of the program is:

a = 1
b = 4
a = 2
b = 3

Demo :Iteration statement for

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
}

1for(initialization; condition; iteration)


2{
3 // body
4}
 Previous program of while loop can be written in for loop as:
for loop example
class ForDemo
{
public static void main(String args
{
int num;

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}

Output will be:

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();
}
}
}

The output of the program is:

........
.......
......
.....
....
...
..

Nested Loop example


class NestedLoopDemo
{
public static void main(String args
{
int a, b;

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}

The output of the program is:


……..

…….

……
…..

….

..

FOR EACH LOOP

Here is how the example looks with the for-each construct:

for(element: collection) {
// body
}

Example: (from Oracle)

void cancelAll(Collection<TimerTask> c) {
for (TimerTask t : c)
t.cancel();
}

The loop above reads as “for each TimerTask t in c.”


Syntax of for-each loop
for(element: collection)
{
// body
}

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

You might also like