Control Structures with Example

Explain the following control structures with example.

(a) if statement

Ans:- 

The if statement is of two types:

1. Simple if statement : The simple if statement in which only one statement is followed by that if statement.

Sysntax:- if(apply some condition)
                   Statement

Example: if(a>b)
                   System.out.println(“a is Biiiig!”);

2. Compound if statement : If there are more than one statement that can be executed when if condition is true. Then it is called compound if statement. All these executable statements are placed in curly brackets.

Syntax:- if(apply condiotion)
                   {
                           Ststement 1

                           Ststement 2
                           ………..
                    }

(b) if else statement

Ans:- 

The syntax for if….else statement will be:

              If(condition)
                           Statement 1
              Else
                           Statement 2

Here if the condition is true then statement 1 will execute otherwise statement 2 will execute.

Example:-

              if(a>b)
                           System.out.println(“a is big”);
              else
                           System.out.println(“b is big”);

If……else if statement
          The syntax of if…..else if statement is
           if(is condition true?)
                 Statement
           else if(another condition)
                 Statement
           else if(another condition)
                  Statement
           else
                  statement

Example:

           if(age==1)
                  System.out.println(“You are an infant”);
           else if(age==10)
                  System.out.println(“You are a kid”);
           else if(age==20)
                  System.out.println(“You are grown up now”);
           else
                  System.out.println(“You are an adult”);

(c) while statement

The while loop is Java’s most fundamental looping statement. It repeats a statement or block while its controlling expression is true. Here is its general form:

          while(condition) {
                   // body of loop
          }

The condition can be any Boolean expression. The body of the loop will be executed as long as the conditional expression is true. When condition becomes false, control passes to the next line of code immediately following the loop. The curly braces are unnecessary if only a single statement is being repeated.

Example:

         int count=1;
                    while(count<=5)
         {
                    System.out.println(“I am on line number : “+count);
                    count++;
          }

(d) do…while statement

This is similar to while statement but the only difference between the two is that in case of do..while statement the statements inside the do…while must be executed at least once. This means that the statement inside the do….while body gets executed first and then the while condit ion is checked for next execution of the statement, whereas in the while statement first of all the condition given in the while is checked first and then the statements inside the while body get executed when the condition is true.

Syntax:

         do
         {
                   Statement 1;
                   Statement 2;
                   …….
                   Statement n;
          }while(condition);

Example:

          int count 1;
         {
                   System.out.println(“I am on line number : “+count);
                   count++;
          }while(count<=5);

(e) switch case

The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression.As such, it often provides a better alternative than a large series of if else-if statements.Here is the general form of a switch statement: 

          switch (expression) {
          case value1:
             // statement sequence
             break;
          case value2:
             // statement sequence
            break;
            ...
          case valueN:
             // statement sequence
             break;
             default:
            // default statement sequence
          } 

The expression must be of type byte, short, int, or char; each of the values specified in the case statements must be of a type compatible with the expression.  Each case value must be a unique literal (that is, it must be a constant, not a variable). 

Duplicate case values are not allowed. The switch statement works like this: The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default  statement is optional.

If no case matches and no default is present, then no further action is taken. The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branches to the first line of code that follows the entire switch statement. This has the effect of “jumping out” of the switch.

Here is a simple example that uses a switch statement:

            class SampleSwitch
            {
                 public static void main(String args[])
                 {
                     for(int i=0; i<6; i++)
                          switch(i)
                     {
                          case 0:
                             System.out.println("i is zero.");
                             break;
                          case 1:
                             System.out.println("i is one.");
                             break;
                          case 2:
                             System.out.println("i is two.");
                             break;
                          case 3:
                             System.out.println("i is three.");
                             break;
                             default:
                             System.out.println("i is greater than 3.");
           }
                }
                     }

(f) for loop

Here is the general form of the for statement:

          for(initialization; condition; iteration) {
              // body
          }

If only one statement is being repeated, there is no need for the curly braces. The for loop operates as follows. When the loop first starts, the initialization portion of the loop is executed. Generally, this is an expression that sets the value of the loop
control variable, which acts as a counter that controls the loop. It is important to understand that the initialization expression is only executed once. Next, condition is evaluated. This must be a Boolean expression. It usually tests the loop control variable against a target value. If this expression is true, then the body of the loop is executed. If it is false, the loop terminates. Next, the iteration portion of the loop is executed. This is usually an expression that increments or decrements the loop control variable. The loop then iterates, first evaluating the conditional expression, then executing the body of the loop, and then executing the iteration expression with each pass. This process repeats until the controlling expression is false.

Example:

          class ForTick
          {
                public static void main(String args[])
               {
                    int n;
                    for(n=10; n>0; n--)
                    System.out.println("tick " + n);
         }
               }

Java Tips

See also

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.