Control Statements:
The control
statements are used to get the control over flow of executing at a java
program. The Control Statements are classified into
1. Braching:
2.looping:
If Statement
While loop
If-else Statement
Do-while loop
Nested-If Statement For loop
Else-If ladder Statement For each(jdk
1.5)
Switch
1.
Braching:
i).
If Statement:
Syntax:
if(condition)
{
Stmts
______
}
class IfDemo {
public static void main(String[] args) {
int d=20;
if(d>=10) {
System.out.println("am in
if stmt");
System.out.println(d);
}
}
}
ii)If-Else
Statement:
Syntax: if(condition)
{
Stmts
______
}
else
{
Stmts
______
}
class IfelseDemo {
public static void main(String[] args) {
int age=15;
if((age>=13)&&(age<=19))
System.out.println ("now
is teenage");
else
System.out.println ("now is not
teenage");
}
}
iii)Nested-If
Statement:
Syntax: if(condition)
{
Stmts
______
if(condition)
{
Stmts
______
if(condition)
{
Stmts
______
}
}
}
class NestedIfDemo {
public static void main(String[] args) {
char gender='m';
if(gender=='m') {
System.out.println
("male's block");
int age=23;
if(age>18){
System.out.println("u
r age's block");
int vid=3444;
if((vid>=3000)&&(vid<=4000)){
System.out.println
("u r eligible for vote");
System.out.println
("elect any one");
}
else
System.out.println ("u r not
eligible for vote");
}
}
}
}
iv)Else-If
ladder:
Syntax: if(condition 1)
{
Stmts
______
}
Else if(condition 2)
{
Stmts
______
}
.
.
.
Else
{
Stmts
______
}
Class ElseifladderDemo {
Public static void main(String[] args) {
char ch='d';
if(ch=='a') {
System.out.println
(ch+"ASCII value :"+( int) ch);
}
else if(ch=='c') {
System.out.println
(ch+"ASCII value :"+( int) ch);
}
else if(ch=='d') {
System.out.println
(ch+"ASCII value :"+( int) ch);
}
else
System.out.println
("invalid character rang");
}
In the above statement Else-If
ladder where the condition is satisfied that blocks only executed that reaming
blocks are skipping.
It takes more
amount memory, and resource to execute the statements so it is successes to use
“Switch statement” in the place of “Else-If ladder”.
v)Switch
Statement:
Syntax: Switch (var)
{
Case <case value1>:
Stmts
_____
Break:
Case <case
value2>:
Stmts
_____
Break:
.
.
Case <case value n >:
Stmts
_____
Break:
Default:
}
class SwitchDemo {
public static void main(String[] args) {
Char ch='c';
Switch (ch){
case 'a':
case 'A':
System.out.println ("am
in case a");break;
case 'b':
case 'B':
System.out.println ("am
in case b");break;
case 'c':
case 'C':
System.out.println ("am
in case c");break;
case 'd':
case 'D':
System.out.println ("am
in case d");break;
default:
System.out.println ("not
in range");
}}}
2.
Looping:
i)
While loop:
Syntax: while (condition)
{
Stmts;
_____
}
The above while loop can also called as
entry control loop i.e when the condition is satisfies then only it allows the
enter into while loop.
class WhileDemo {
public static void main (String [] args) {
int x=1;
While(x!=10) {
System.out.println
("x:"+x);
x++;
}
}
}
ii)Do-While
loop:
Syntax: do
{
Stmts;
_____
} while (condition);
It is also know as exit control loop.
class DowhileDemo {
public static void main(String[] args) {
int x=10;
do{
System.out.println(x);
x--;
} while (x>=1);
System.out.println ("main
end");
}
}
iii)For
loop:
Syntax: for (initialization; condition; inc/dec)
{
Stmts
}
class ForcharDemo {
public static void main(String[] args) {
for(char
ch='a';ch<='z';ch++){
System.out.println (ch);
if(ch=='g'){ break; }
}
}
}
iv)For-Each
loop:
This
for-each loop inducting jdk 1.5 versions. For-each loop is used only
“1D-Array”.
Syntax: for(declaration
: array name )
{
Stmts;
}
class ForeachDemo {
public static void main (String[] args) {
Char ch []={'b','r','s','t'};
for (char i:ch) {
System.out.println (i);
}
}
}
Arrays in Java:
Array is a collection of homo genius elements.i.e array can able to store same data values (or) elements.
The array elements are stored continues memory locations.
The array‘s will increases the program performances and saving the resource will performing the operations.
The array’s classified into
(i) 1Dimentional-arrays
(ii) 2 Dimentional –arrays
(iii) 3 Dimentional -arrays (or) multi Dimentional arrays
i) 1D-Arrays:
In 1D-array all the elements are arranged in only one row.
Syntax: <data type> <array name>[]=new <data type> [size];
Ex: int a[]=new int[5];
Note : “new” is a keyword in java which is used to allocate the memory dynamically in the programming. Array index always starts from “zero”. All Array elements are instilled zero at declaration time.
Note: - length: length is a property of array which is used to calculate the length of an array dynamically.
We can also declare and initialized the Array’s as represented blow program.
class OneDarryDemo {
public static void main (String[] args) {
Int num []={1,2, 3,4,2},key=2,c=0;
for (int a=0;a<num.length;a++){
if(num[a]==key){
c++;
}
}
If(c>0)
System.out.println ("search key found “ + c + “ time(s) “);
Else
System.out.println ("search key not found “);
} }
We can also perform Arithmetic Operation on array elements
ii)2D-Arrays:
Syntax: <data type> <array num> [][]=new <data type> [rows][cols];
Ex: int ab [][]=new int [3][3];
The 2D-Array is a combination of 1D-Array’s in rows and columns format
We can also declare and initialize an 2D-Array as below example.
class TwoDarrayDemo {
public static void main(String[] args) {
int n[][]={{1,2,3},{4,5,6}};
for(int i=0;i<n.length;i++){
for(int j=0;j<n[i].length;j++){
System.out.print (n[i][j]+ " ");
}
System.out.println ();
} } }
iii)3D-Array or multi Array:
Syntax: <data type> <array num> [][][]=new <data type> [dim1][dim2][dim3];
class ThreeDarryDemo{
public static void main(String[] args) {
int a[][][]={{{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}};
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
for(int k=0;k<a[i][j].length;k++){
System.out.print (a[i][j][k] + " ");
}
System.out.println ();
}
}
}
}
No comments:
Post a Comment