Control Statements in Java
-
Upload
chpavanimca -
Category
Documents
-
view
8 -
download
0
description
Transcript of Control Statements in Java
![Page 1: Control Statements in Java](https://reader035.fdocuments.us/reader035/viewer/2022072009/55cf91e5550346f57b9182e1/html5/thumbnails/1.jpg)
Do…while 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;
![Page 2: Control Statements in Java](https://reader035.fdocuments.us/reader035/viewer/2022072009/55cf91e5550346f57b9182e1/html5/thumbnails/2.jpg)
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++)
![Page 3: Control Statements in Java](https://reader035.fdocuments.us/reader035/viewer/2022072009/55cf91e5550346f57b9182e1/html5/thumbnails/3.jpg)
{
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.
![Page 4: Control Statements in Java](https://reader035.fdocuments.us/reader035/viewer/2022072009/55cf91e5550346f57b9182e1/html5/thumbnails/4.jpg)
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”);
![Page 5: Control Statements in Java](https://reader035.fdocuments.us/reader035/viewer/2022072009/55cf91e5550346f57b9182e1/html5/thumbnails/5.jpg)
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”);
}
![Page 6: Control Statements in Java](https://reader035.fdocuments.us/reader035/viewer/2022072009/55cf91e5550346f57b9182e1/html5/thumbnails/6.jpg)
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(“..........”);
}
}
}
![Page 7: Control Statements in Java](https://reader035.fdocuments.us/reader035/viewer/2022072009/55cf91e5550346f57b9182e1/html5/thumbnails/7.jpg)
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;
}
}
![Page 8: Control Statements in Java](https://reader035.fdocuments.us/reader035/viewer/2022072009/55cf91e5550346f57b9182e1/html5/thumbnails/8.jpg)