CONDITIONAL STATEMENTS :
- IF STATEMENT :
if (condition)
{
------ ;
}
The IF loop lies in the curly braces . if the condition defined in brackets will be satisfied only then the statements in the curly braces will be executed else the compiler will jump those statements.
e.g.
if (a>b)
{
System.out.println("a is greater than b");
}
ON the screen a is greater than will be printed only if a id greater than b .
- IF - ELSE STATEMENT :
if (condition)
{
-----;
}
else
{
----;
}
IN this kind of statement , if the condition is satisfied statements under if loop will be executed and in case the condition is not met it will jump the if statements and will execute the else part.
MULTIPLE IF ELSE STATEMENTS ARE POSSIBLE :
SYNTAX :
if (condition_1)
{
}
else
{
if(condition_2)
{
}
else
{
if(condition_3)
{
}
else
{
}
}
}
- SWITCH STATEMENTS :
int n ;
switch(n)
{
case 1: {-----};
break;
case 2 : {-----};
break;
-
-
case n : {----};
break;
default : {};
}
In the above syntax , we consider any integer n . For n to be 1 , case 1 will be executed , for n to be 2 , vase 2 will be executed .In the same way for n case n will be executed . If the value or the integer is greater than n , then default case will be executed.
NOTE :
- Instead of passing an integer value , character can also pe passed . In JAVA Version 7 and above we can even pass a string .
- For character :
char 'n';
switch('n')
{
}
- Use of break statement is IMPORTANT . if the value of n is 1 and break statement is not used . All the cases following case 1 will be executed .
LOOPING :
- FOR LOOP:
for(initialize any variable;condition;increment/decrement)
{
----- ;
}
In this loop a variable is first initialized i.e. initial value to the variable is assigned for which the loop will be executed . When the compiler reaches the end of the loop , it increments/decrements the initialized variable as per the command given in third section and then after incrementing condition is checked . If the condition given in second section met even after the increment/decrement , the loop is continued .
In this way loop continues till the condition is satisfied . For any unwanted value of the variable , loop is broken .
In this way loop continues till the condition is satisfied . For any unwanted value of the variable , loop is broken .
for (int i = 0 ; i<5 ; i++)
{
System.out.println("THE VALUE IS "+i);
}
The loop will execute till the condition is satisfied and the output of the above program will be :
THE VALUE IS 0
THE VALUE IS 1
THE VALUE IS 2
THE VALUE IS 3
THE VALUE IS 4
initalization ;
while(condition)
{
---- ;
increment/decrement ;
}
While loop and for loop are almost the same . For loops are used when the number of recursions are known and while loop is used when number of recursions are not known .
Example : The same above program can be implemented using while loop in the following way .
int i ;
while(i<5)
{
System.out.println("THE VALUE IS "+i);
i++;
}
initialization ;
do
{
------ ;
increment/decrement ;
}
while(condition);
In do while loop condition is checked after the one time execution of loop . It works in the same sequence as it appears . The loop is one time executed for the initial value of variable and after the execution at the end it is incremented or decremented as per the requirements of the user . After this when the control comes out of the loop , if checks the condition for the current value of variable , if it holds the loop is again executed else control skips the loop.
THE VALUE IS 0
THE VALUE IS 1
THE VALUE IS 2
THE VALUE IS 3
THE VALUE IS 4
- WHILE LOOP
initalization ;
while(condition)
{
---- ;
increment/decrement ;
}
While loop and for loop are almost the same . For loops are used when the number of recursions are known and while loop is used when number of recursions are not known .
Example : The same above program can be implemented using while loop in the following way .
int i ;
while(i<5)
{
System.out.println("THE VALUE IS "+i);
i++;
}
- DO WHILE LOOP :
initialization ;
do
{
------ ;
increment/decrement ;
}
while(condition);
In do while loop condition is checked after the one time execution of loop . It works in the same sequence as it appears . The loop is one time executed for the initial value of variable and after the execution at the end it is incremented or decremented as per the requirements of the user . After this when the control comes out of the loop , if checks the condition for the current value of variable , if it holds the loop is again executed else control skips the loop.
BRANCHING STATEMENTS :
- BREAK STATEMENT :
If multiple loops are used then the control comes out of the inner loop .
This is explained with the example below :
- CONTINUE STATEMENTS :
The output to the above program will appear for 0,1,2,3,4,6,7,8,9.
IN JAVA THERE ARE NO GO TO STATEMENTS .
0 comments:
Post a Comment