Ads

Conditional Statements

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

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;

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.

Terms related to switch statement


Control Variable

The variable passed as an argument to the switch statement that decides which case is to be executed is known as a control variable.

break statement

This is the statement used at the end of each case which acts as the case terminator. As soon as break is encountered, the control is forced to move out of the switch block.

default statement

If no case is matched in the switch block for a given value of control variable, default case is executed implicitly.

Fall Through

In case a break statement is not used at the end of a case, the control enters into the next case for execution. This condition is said to be Fall Through.
SHARE

Author

Hi, Its me Hafeez. A webdesigner, blogspot developer and UI/UX Designer. I am a certified Themeforest top Author and Front-End Developer. I'am business speaker, marketer, Blogger and Javascript Programmer.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 Comments:

Post a Comment