Java if-else statement,loops,switch,break,continue and comment with easy explanation.

In this tutorial, we will learn about the control flow of the java such as if-else statement,loops,switch,break,continue and comment etc step by step.

What is control flow statement ?

Control flow statements are used to evaluate  or terminate the flow of execution of the program with the help of decision making statement,loops and branching statement in the particular blocks of code with the basis of condition.

Categories of control flow

Decision making statement :if,if-else,if-else if-else & switch

Looping statement :while,do–while,for

Branching statement :break,continue

If-else  statement

If statement is used to check the condition.It returns true or false.There are various types of if statement

A.If statement

B.If-else statement

C.If-else- if ladder

A.If statement

If statement is used to check the condition.It statement execute only when condition true.

Example:

class Test
{
Public static void main(String[ ] arg)
{
In a=10,b=5;
if(a>b) //Here condition is true so if block will execute
{
System.out.println(“Hello java if statement program!”);
}
}

Output:

Hello java if statement program!

B.if-else statement

If-else is also a conditional statement.if condition is true then if-block execute otherwise else-block.

Example:

Class Test
{
Public static void main(String[ ] args)
{
Int a=17;
if(a%2==0)//Here condition is false so else-block will execute
{
System.out.print(“This is an even number”);
}
else
{
System.out.print(“This is an odd number”);
}
}
}

Output:This is an odd number

C.if-else-if ladder

If-else-if ladder statement is  used to execute only one condition from the multiple statements.

Example:

class Test
{
Public static void main(String[ ] args)
{
Int marks=70;
if(marks<60)
{
System.out.println(“fail”);
}
else if(marks>65)
{
System.out.println(“First division”);
}
else
{
System.out.println(“Not valid marks”)
}
}
}

Output:

First division

Loops

Loops are used to execute the blocks of code repeatedly on the basis of condition.

There are three types of loop in java

A.while  loop

B.do…while loop

C.for loop

A.while loop

While loop executes the block of code many times until the condition is false.

Example:


Class Test
{
Public static void main(String[ ] args)
{
Int number=5;
while(number<10)
{
System.out.println(number);
number++;
}
}
}

Output:

5

6

7

8

9

B.do…while loop

do-while loop is used to execute the block of code any multiple times until the condition is false.

It executes at least once because condition checked after execution of code.

Example:

class Test
{
Public static void main(String[ ] args)
{
Int number=5;
Do
{
System.out.println(number);
number++;
}while(number<10);
}
} 

Output:

5

6

7

8

9

C.for loop

It is used to execute the blocks of code many times until condition is true.

It takes initialisation,condition and increment/decrements into a single line.

Example:

class Test
{
Public static void main(String[ ] args)
{
for(int i=1;i<=5;i++)
{
System.out.println(i);
}
}

Output:

1

2

3

4

5

Switch,break,continue and comment

Switch statement:

Switch statement is used to execute one statement from the multiple statement according to the condition.

Example:

class Test
{
Public static void main(String[ ] args)
{
Int x=3;
String days;
switch(x)
{
Case 1:
days=”Monday”;
Break;
Case 2:
days=”Tuesday”;
Break;
Case 3:
days=”Wednesday”;
Break;

Case 4:
days=”Thursday”;
Break;

Case 5:
days=”Friday”;
Break;

Case 6:
days=”Saturday”;
Break;

Case 7:
days=”Sunday”;
Break;

Default:”invalid day”;
Break;
}
}
}

Output

Wednesday

Break statement

Break statement is used to terminate the loop or blocks of code.

Example:

class Test
{
Public static void main(String[ ] args)
{
for(int i=1;i<=5;i++)
{
if(i==3)
{
break;
}
System.out.println(i);
}
}
}

Output:

1

2

Continue statement

It is used to break on iteration in the loop when condition after that it continue with the next  iteration.

Example

class Test
{
public static void main(String[ ] args)
{
for(int i=1;i<=5;i++)
{
if(i==3)
{
continue;
}
System.out.println(i);
}
}
}

Output:

1

2

4

5

Comments

Comments are the symbol which is used to provide the information about line of code.

Comments statement/information are ignored by the java compiler.

Example:

1.// This is my first program

2./*This is my

first program

*/

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *