Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Dowhile Loop

Syntax:1
do{
statements;
}while(condition):
Program:1 write a program to display numbers from 1 to 10.
Class Demo
{
Public static void main(String[] args)
{
Int X;
X=1;
do{
System.out.println(X);
X++;
}while(X<=10);
}
}

While Loop
Syntax:2
While(condition)
{
Statements;
}
Program: 2 To display 1 to 10 numbers
Class Demo
{
Public static void main(String[] args)
{
Int X;
X=1;

While(X<=10)
{
System.out.println(X);
X++;
}
}
}

For Loop
Syntax: 3
For(expression1; expression2; expression3 )
{
Statements;
}

Program:To display 1 to 10 numbers


Class Demo
{
Public static void main(String[] args)
{
for(int x=1;x<=10;x++)
{
System.out.println(x);
}
}
}

Nested for Loops


Syntax:4
for(int i=1;i<=3;i++)
{
Statements1;

for(int j=1;j<=4;j++)
{
Statements2;
}
}
Program:To display stars in triangular form-nested for loops
Class Stars
{
Public static void main(String args[])
{
Int r=5;
for(int i=1;i<=r;i++)
{
for(int st=1;st<=i;st++)
{
System.out.println( * );
}
System.out.println();
}
}
}

For-each Loop
Syntax:
for(var : collection)
{
Statements;
}
Program: Retrieve the elements one by one form an array and display it.
Class Demo
{
Public static void main(String args[])

{
Int arr[]={200,19,-56,44,99};
for(int i : arr)
{
System.out.println(i);
}
}
}

Switch Statement
Syntax:
Switch(variable)
Case value1:statements1;
Case value2:statements2;
Case value3:statements3;
.
.
Case valuen:statementsn;
}
Program:Write a program to come out of switch block
Class Demo
{
Public static void main(String args[])
{
Char color=g;
Switch(color)
{
Case r:System.out.println(Red);
break;
case g:System.out.println(Green);
break;
case b:System.out.println(Blue);

break;
case w:System.out.println(White);
break;
default:System.out.println(No color);
}
}
}

Break Statement
Syntax:
break label;
program:
class Demo
{
Public static void main(String args[])
{
boolean x=true;
bl1:{
bl2:{
bl3:{
System.out.println(Block3);
If(X) break bl2;
}
System.out.println(Block2);
}
System.out.println(Block1);
}
System.out.println(Out of all blocks);
}
}

Continue Statement

Syntax:
continue;
Program: write a program for using continue statements
Class Demo
{
public static void main(String args[])
{
Int i=1,j;
lp1:while(i<3)
{
System.out.print(i);
Ip2:for(j=1;j<=5;j++)
{
System.out.println(\t+j);
If(j==3)
{
I++;
Continue lp1;
}
}
I++;
System.out.println(..........);
}
}
}

Return statement
Syntax:
return 1;
return x;
return (x+y);
return -5;

Program:write a program to return a value from a method


class Demo
{
Public static void main(String args[])
{
Int res=Demo.myMethod(10);
System.out.println(Result=+res);
}
Static int myMethod(int num)
{
return num*num;
}
}

You might also like