java statement

What is Control Statement ?

Control statements are the statements which are used to control the flow of execution of the instructions written in a program. Instructions are executed sequentially means one after another .
java does not accept numbers as conditional operators. It only considers boolean expressions as conditions which returns TRUE or FALSE. E.g if(1==1) is accepted as it is a boolean expression and will return true.
sum=a+b; is a instruction.
a=5;
b=10;
sum=a+b;
it is a set of instruction.
each instruction is terminated by semicolon ;
computer will execute the first line a=5 and then b=10 so on…
example of control statements

if (a=5){
print a equal to 5;
}
else{
print a not equal to 5;
}
Types of statements:

    Decision control structure

      if else
      nested if else
      else if ladder
      case control structure
      switch statement

    if contition is true then if statement block will be executed otherwise else block will be executed.

if statement
if(condition 1){
this statement block will be executed;
}
else{
else statement block will be executed;
}

if statement program
class StateMent{
public static void main(String[] args){
a=5;
if (a=5){
System.out.println(“value of a is 5”);
}
else{
System.out.println(“value of a is not 5”);
}
}
}
Output:: value of a is 5

if else statement program

If Else If Statement
if (1==2)
System.out.println(“Hello! This will not get printed”);
else if(1==1)
System.out.println(“Hello! This will get printed”);
else
System.out.println(“This will get executed when Above conditio is FALSE”);
switch statement
switch(day){
case 1:
System.out.println(“monday”);
break;
case 2:
System.out.println(“tuesday”);
break;
case 3:
System.out.println(“wednesday”);
break;
default:
System.out.println(“sunday”);
break;
}

day is variable if the value of the day variable is 1 ,then it will print monday ,if 2 then tuesday so on…..

BACK

NEXT