Control Statements in JAVA

Control Statements in java


  • if Statements
  • switch Statement
  • while Loop
  • do..while Loop
  • for Loop

if Statements

Three Forms of the if Statement
  • “if”—use when you want to do one thing or nothing.
  • “if, else”—use when you want to do one thing or another thing.
  • “if, else if”—use when there are three or more possibilities.
The usual logical conditions from mathematics in java:

  • Less than: a < b
  • Greater than: a > b
  • Less than or equal to: a <= b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b
Syntax:

if 
if (condition) {
  // block of code to be executed if the condition is true
}


Example:
public class Pairosoft{
    public static void main(String[] args) {
            int a=10,b=20;
            if(a<b){
              System.out.println("b is grater then a");}
        }
}
Output:-
b is grater then a

if-else
if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}


Example:
public class Pairosoft{
    public static void main(String[] args){
            int a=10;
            if(a<1){
              System.out.println("if.");}
            else{
              System.out.println("else.");}
    }
}
 Output:
else.

if-else-if
if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}


Example:
public class Pairosoft{
    public static void main(String[] args) {
            int a=10;
            if(a<1){
              System.out.println("C++");}
            else if(a<2){
              System.out.println("PYTHON");}
            else{
            System.out.println("JAVA");}
    }
}
 Output:
JAVA


switch Statement

syntax:
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
The switch expression is evaluated once.The value of the expression is compared with the values of each case.If there is a match, the associated block of code is executed.The break and default keywords are optional.we seen one example.


Example:
public class Pairosoft{
    public static void main(String[] args) {
         int a=11,b=20;
         char ch='+';
            switch (ch) {
            case '+':
            System.out.println(a+b);
            break;
           
            case '-':
            System.out.println(a-b);
            break;
            }
    }
}
 Output:
31

while Loop

Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the while loop.
Example: i <= 10
Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value.
Example:  i++;

Syntax:
while (test_expression)
{
   // statements

  update_expression;
}


Example:
public class Pairosoft{
    public static void main(String[] args) {
        int i = 1;
        while (i <= 5) // test expression
        {
            System.out.println("java");
            i++; // update expression             }
        }
    }
}
 Output:
java
java
java
java
java

do..while Loop

Syntax:
do
{
    // loop body
   
    update_expression
}
while (test_expression);


Example:
public class Pairosoft{
    public static void main(String[] args){
        int i = 1;
        do { // Print the statement
            System.out.println("java");
            i++; // update expression
        }
        while (i <= 5); // test expression
    }
}
Output:
java
java
java
java
java

for Loop

Syntax:
for (statement 1; statement 2; statement 3) {
  // code block to be executed
 }
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.

Example:
public class Pairosoft{
    public static void main(String[] args){
       for(int i=1;i<=10;i++){
       System.out.println(i);}
    }
}
Output:
1
2
3
4
5
6
7
8
9
10


Common escape sequences

Escape Sequence
Description
\t
Move the cursor to the next tab stop
\n
Newline-go to first column in next line  
\r
Return to first column in current line
\”
Print a literal double quote
\’
Print a literal single quote
\\
Print a literal backslash







More will come idea if you watch this video:-





Post a Comment

0 Comments