if statement
This statement checks the condition first. If the condition is true then the next statement is executed
otherwise it is ignored.
e.g.,
if(a>10)
c = a * a;
System.out.println(c);
In the example shown above, if the given condition (i.e., a is greater than 10) is true then it will calculate the square of the number 'a' otherwise, it will ignore this statement and will go to the next statement. In that case, it will print the default value of c as 0.
if-else statement
If you want to execute either of the two statements depending upon a given condition then 'if-else' statement is used.
e.g.,
if(a > b)
max = a;
else
max = b;
The example shown above illustrates that max is assigned either with the value 'a' or 'b' depending upon the condition (a>b) us true or false.
if-else-if statement
Sometimes, it may happen that the given condition is false and the user wants to check another condition for taking necessary action, under this situation 'if-else-if ' statement is used.
e.g.,
if(marks >= 75)
System.out.println("Distinction");
else if(marks >= 60)
System.out.println("First Division");
else if(marks >= 50)
System.out.println("Second Division");
else
System.out.println("Third Division");
if-else and nested if-else | Part 1
e.g.,
if(marks >= 75)
System.out.println("Distinction");
else if(marks >= 60)
System.out.println("First Division");
else if(marks >= 50)
System.out.println("Second Division");
else
System.out.println("Third Division");
if-else and nested if-else | Part 1
Nested if statement
When 'if' statement is used within another 'if' statement, the construct is said to be a 'Nested if ' statement. In this construct, if the given condition is true then it further checks another if condition and carries out the instructions accordingly.
e.g.,
if( a>= 10)
{
if(a < 100)
c = a * a;
}
else
c = a * a * a;
e.g.,
if( a>= 10)
{
if(a < 100)
c = a * a;
}
else
c = a * a * a;
switch-case statement
In a switch case statement, a number of blocks are created under different cases. All the cases are enclosed within curly braces { } under switch statement. A particular case is executed based on the given value (user's choice) of control variable passed as an argument to the switch () statement.
e.g.,
switch (a)
{
case 1:
System.out.println("Good");
break;
case 2:
System.out.println("Better");
break;
System.out.println("Best");
break;
default:
System.out.println("Invalid");
}
A particular case will be executed when matched with the value of control variable 'a'. The default case will be executed only when the value of 'a' doesn't match with any of the cases listed above.
e.g.,
switch (a)
{
case 1:
System.out.println("Good");
break;
case 2:
System.out.println("Better");
break;
System.out.println("Best");
break;
default:
System.out.println("Invalid");
}
A particular case will be executed when matched with the value of control variable 'a'. The default case will be executed only when the value of 'a' doesn't match with any of the cases listed above.
0 Comments:
Post a Comment