Decision control statements
Conditional Statesments
if statement
  • It is a statement used to execute a piece of code based on a given condition.
  • The statement is executed if the Boolean-expression evaluates to true.

          Syntax:
                   
if (expression)
                    statement;

  • If the Boolean-expression evaluates to false then the statement will be skipped and another statement (if exists) will be executed.

Example:

Output:

if-else statement
  • If all the if block statements are false, then else block statement is executed, i.e., statements present inside the else block is executed, if the Boolean-expression of the if statement is not satisfied.

          Syntax:
                  if (expression)
                  statement;
                  else
                  statement;

                  Remember, only one statement can appear directly, i.e., either if or the else

Example:

Output:

if else if statement / if-else-if ladder
  • if-else-if ladder is a programming construct that is based on a sequence of nested ifs.

          Syntax:
                  if(expression)
                  {
                                   statement;
                  }
                    else if(expression)
                  {
                                   statement;
                  }
                   else if(expression)
                  {
                                   statement;
                  }
                   else
                  {
                                   statement;
                   }

  • The if statements are executed from the top-down. If one of the conditions of the if statement is satisfied, then the statement associated with that particular if is executed, the rest of the ladder is bypassed.
  • Among all the conditions, if none of the conditions is true, then the else statement will be executed at the end.
  • The final else statement acts as a default condition.
  • If the final else statement is not present at the end and all other conditions are not satisfied, then no action will be taken.

Example:

Output:

Nested if statements
  • nested if statement uses if statements inside if statements. i.e., if statements present inside another if statement is known as Nested if. 
  • It is used when we have multiple if conditions.
  • In Nested if, one if statement targets another if statement. 

          Syntax:
                    if(expression)
                    {
                              statement;
                              if(expression)
                              {
                                        statement;
                              }
                    }

Example:

Output:

for loop
  • Loops (also called iteration statements) allow a collection of instructions to execute repeatedly until a certain condition is reached.

                  Syntax:
                         
for (initialization; condition; increment)
                          {
                                  statement
                          }

There are many variations of for loop, but its most common form works like this:

  • The initialization is used to set the loop control variable, and it assigns an initial value to it.
  • The condition of the for loop is a relational expression that defines the exit of the loop.
  • The increment defines the variation in the loop control variable at each time when the loop is iterated. 
  • These three sections are separated with semicolons.
  • The for loop executes continuously until the assigned condition remains true. Once the given condition becomes false, control is shifted to the statement following the for.

Example:

Output:

While Loop
  • The while loop is Java’s most common looping statement. 
  • The statements inside the while loop will keep executing as long as the Boolean expression evaluates to true.
  • Each execution is called an iteration.
  • When the Boolean expression does not satisfy, control passes to the next line of code immediately following the loop.
  • If the condition never becomes false, i.e., the condition is always true, then the while loop keeps getting executed. Such type of loop is known as an infinite loop.

Example

Output:

do while loop
  • do-while loop won’t test the loop condition at the top of the loop; it checks the condition of the Boolean expression at the bottom of the loop to execute the statements inside the block at least once.
  • This indicates that the do-while loop always executes at least once.
  • After executing once, statements inside the do-while loop will keep executing as long as the Boolean expression evaluates to true.
              Syntax:
                      
    do {
                                      statement;
                           }
                      while(condition);
  • Although the curly braces are not essential when only one statement is present, they are usually used to avoid confusion (to you, not the compiler) with the while.
  • The do-while loop iterates continuously until the condition becomes false.

Example:

Output:  

Unconditional Statements
break statement

   In Java language, the break statement holds three uses. 

(1) As we have already seen, it terminates a statement sequence in a switch statement. 

(2) It is used to exit from a loop. 

(3) it is used as a “civilized” form of a goto statement. 

Let’s see the last two uses.

Using a break statement to exit from a loop:

  • A break statement can be used to make immediate termination of a loop bypassing the normal loop conditional test.
  • The loop is immediately terminated when the break statement is present inside the loop, and program control is transferred to the statement following it.

Example:

Output:

Using break as a form of Goto:

  • The break statement in Java can also be used to give a “civilized” form of the goto statement.
  • Java language does not have the goto statement.
  • The goto statement can be useful when we are exiting from a deeply nested set of loops. To handle such a situation, Java provides an expanded form of the break statement.
  • By using this form of break, we can break out of one or more blocks of the code. 
  • These blocks need not be present in a loop or a switch. They can be any block.
  • This kind of break works with a label. As we will see, break provides us with the benefits of a goto without its problems.

          General form:

                  break label;

Example:

Output:

Switch Statement
  • The switch statement is a multiple-choice decision-making statement. 
  • It is used to execute different cases based on equality.
  • It provides a better alternative than a series of if-else-if statements. 
  • It tests the value of an expression successively from a character constant or a list of integers.
  •  When a match is found, then the statements associated with that constant are executed. 

          Syntax:
                  switch (expression)
                  {
                           case constant1:
                           statement
                           break;
                           case constant2
                           statement 
                           break; 
                           case constant3: 
                           statement 
                           break;
                           .
                           .
                           .
                          default
                          statement 
                  }

  • The expression can be a character or an integer value, although floating-point expressions are not allowed.
  • The value of an expression is tested with the constant values, one after the other, specified   within the case statements. 
  • When a match is found, then the statement sequence associated with that particular case is executed, and it continues till the end of the switch, or a break statement is reached.
  • The default statement is executed at the end if no matches are detected.
  • The break statement is used under the switch statement. When a break statement is found, then the execution branches to the initial line of code that follows the whole switch statement.
  •  The break statement is used to “jump out” of the switch.

Example:

Output:

Continue statement
  • Sometimes it is helpful to force an early iteration of a loop.
  • Using a continue statement in a while and do-while loop makes the control be shifted immediately to the conditional expression that controls the loop.
  • The continue statement works somewhat similar to the break statement, but the continue statement forces the next iteration of the loop to take place instead of forcing termination.
  • It is generally used when we want to skip a particular part of code for some condition. 

          Syntax:
                  //statements of the loop  
                  continue;  
                  //some particular lines of the code which we want to skip  

Example:

Output:

Who is the course for?

Prerequisites before starting this course:

Questions 🙵 Answers.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

More Courses

You might also be interested in these courses