Control Statements in java

Control Statements in java
There are three control statements in java:
  1. Selection  statements
  2. Iteration statements or looping statements
  3. Jump statements
  4. Break statement
1.Selection statements:
  • if-else statements
  • switch case statements
Which path to choose:your choice


1.if-else statements:
With if only:
Syntax:if(condition)
{
statement1;
statement2;
statement3;
statement4;
..........
}

With if-else statements:
Syntax:if(condition)
{
statement1;
statement2;
statement3;
statement4;
..........
}
else
{
statement1;
statement2;
..........
Here condition is a conditional statement containing the condition operators only such as <,>,>=,<=,==,!=.
Example:if(age>=18)
{//if the age is greater than 18 then print this
System.out.println("You are allowed to vote");
}
else
{
System.out.println("Grow up!!wait for some years..");
}
Take care of the curly brackets that represents the scope of the condition.
There can be nesting of if-else statements.Such as:
if(condition)
{
    if(condition)
    {
     .....and so on if necessary
    }
    else
    {
    ....
    }
}
else
{
...
}

2.Switch case statements:
You might think what is the need for switch-case if we can do with if-else statements.Reason is simplicity and easiness.Suppose if there is a program to print the name of the day according to the number provided.With the help of if-else the program will be complex and hard to read,that's where swich-case is required.
Here's the syntax:
switch(which variable to switch)
{
case variable_value1://code;break;
case variable_value2://code;break;
case variable_value3://code;break;
........and so on...default ://code

break statement is need for breaking further statement execution of code,if we remove the break,the further statements will be executed which we don't want,right.
For number to day program,here's the code:


switch(number)

{
case 1:System.out.println("This is Monday");break;
case 2:System.out.println("This is Tuesday");break;
case 3:System.out.println("This is Wednesday");break;
case 4:System.out.println("This is Thursday");break;
case 5:System.out.println("This is Friday");break;
case 6:System.out.println("This is Saturday");break;
case 7:System.out.println("This is Sunday");break;
default :System.out.println("What!!there is no day with this number...");break;


2.Iteration statements or looping statements




There are needs when some statements are required to be executed more than once,for this purpose iteration or looping statements are used.
Basically there are 3 looping statements:

  1. while loop
  2. do while loop
  3. for loop
1.while loop:Entry controlled loop,i.e. it checks the condition before executing.
Syntax:
while(condition)
{
//do this
}
Example:
int i=0;
while(i<=5)
{
System.out.println("i is"+i);
i=i+1;//or you can write i++ too
}
Output:012345 
Because until the value of variable i is 5,value is printed.

2.do while loop:It's an exit controlled loop that means the statement/s are executed at-least once even if they fail to meet the conditions.
Syntax:
do
{
//do this
}while(condition);
Example:
int i=4;
do
{
System.out.println("Executed once only");
}while(i>4);//which returns false
Output:Executed once only

Another example:
Example:
int i=4;
do
{
System.out.println("Executed five times...");
i=i-1;
}while(i<=0);
Output:Executed five times...
Executed five times...
Executed five times...
Executed five times...
Executed five times...
3.for loop:Syntax:for(initialization;condition;iteration)
{
//statements to be looped
}
Example :for(int i=1;i<=10;i++)
{
System.out.println("i is "+i);
}
Output:1
2
3
4.....upto 10

Comments

Popular posts from this blog

Four building blocks of Object oriented programming language

Exceptions and exceptional handling

Lists in java