Control Statements in Java

← Back to Java Language Chapter

The conditional control statement controls the order in which a java program is executed.

Types of Conditional control statement:

  • If statement

  • Switch statement

Syntax :

    i) if(condition){ 
         s1; 
        }  
    
    ii) if(condition){
           s1; 
        }
        else{ 
           s2; 
        }
    
    iii) if (condition) {
           s1;
         } else 
         if (condition) {
           s2; 
         } else { 
           s3;
         }

Question : Write a program to pass three numbers from command lines and find:
i) minimum
ii) maximum

Syntax :

    switch(expr) { 
        case No1: s1;
            break; 
        case Non: sn;
            break; 
      default: s;
    }

Example:


NOTE: The default statement can be written anywhere in the switch statement. But they have a break. In the last statement, it does not have break, but it is also possible.

Looping statements execute statements over and over again, in a loop. It continues until the condition result is true.

Types of Looping control statement:

  • For statement

  • While statement

  • Do while statement

The for statement lets a code execute in a loop continuously.

Syntax :

    for (initialization; condition; increment/decrement) {
            s1; s2;
          }
    for(	;	;	) {   // possible empty 
            s1; s2;
          }
    

Question. Write a programme to read a number from the command line and display the numbers which divisible by five, from one to t h e given number in both order.


Question. Write a programme to generate prime numbers from 1 to 100.


Question. Write a programme to read a two numbers from command lines and print all the prime numbers between given range.

The while statement executes a single line continuously until the condition is met.

Syntax :

    initialization;
    while(condition) { 
      s1;
      s2;
    Increment/decrement
    }

Example :


Question : Write a programme to read the number from the command line and display the sum of individual digits of the given number.

The do while statement executes a particular chunk of the code until a Boolean expression is true.

Syntax :

    Initialization; 
    do {
      //processing s2;
      Increment/decrement
    } while ( condition );

Example:


Enumerate the difference between While and Do While statement.

While Do While
In the case of While, the first condition will be verified and then the statement will be executed, Inside the while block. In the case of Do-While first statement, inside do block is executed and the condition is be verified.
If the condition is false the first time, then the block of statement will be executed zero times. If the condition is false the first Time, then the block of statement will be executed once.

Types of Unconditional control statements:

  • Break

  • Continue

  • Goto


Break

Break statement transfers the control unconditionally to the end of the block.


Continue

The Continue statement transfers the control unconditionally to the beginning of the block.


Example:


Example:



← Back to Java Language Chapter