Object Oriented Programming: Unit I: Introduction to OOP and Java

Control Statements

with Example Java Programs

Programmers can take decisions in their program with the help of control statements. Various control statements that can be used in java are - 1. if statement, 2. if else statement, 3. while statement, 4. do...while statement, 5. switch case statement, 6. for loop

Control Statements

Programmers can take decisions in their program with the help of control statements. Various control statements that can be used in java are -

1. if statement

2. if else statement

3. while statement

4. do...while statement

5. switch case statement

6. for loop

Let us discuss each of the control statement in details -

1. if statement

The if statement is of two types

Introduction to OOP and Java

i) Simple if statement : The if statement in which only one statement is followed by that is

Syntax

statement.

if(apply some condition)

statement

For example

if(a>b)

System.out.println("a is Biiiiig!");

ii) 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 some condition)

{

statement 1

statement 2

.

.

.

statement n

}

2. if...else statement

The syntax for if...else statement will be -

if(condition)

statement

else

statement

For example

if(a>b)

System.out.println("a is big")

else

System.out.println("b :big brother")

The if...else statement can be of compound type even. For example

if(raining==true)

System.out.println("I won't go out");

System.out.println("I will watch T.V. Serial");

System.out.println("Also will enjoy coffee");

else

{

System.out.println("I will go out");

System.out.println("And will meet my friend");

System.out.println("we will go for a movie");

System.out.println("Any how I will enjoy my life");

}

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

For 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");

Let us now see Java programs using this type of control statement-

Java Program [ifelsedemo.java]

/*

This program illustrates the use of

if else statement

*/

class ifelsedemo

public static void main(String[] args)

{

int x=111,y=120,z=30;

{

if(x>y)

{

if(x>z)

System.out.println("The x is greatest");

else

System.out.println("The z is greatest");

}

else

{

if(y >z)

System.out.println("The y is greatest");

else

System.out.println("The z is greatest");

}

}

}

Output

The y is greatest

Java Program [ifdemo.java]

/*This program illustrates the use of

if...else if statement

*/

class ifdemo

{

public static void main(String args[])

{

int basic=20000;

double gross,bonus;

if(basic<10000)

{

bonus 0.75*basic;

gross basic+bonus;

System.out.println("Your Salary is = "+gross);

}

else if(basic>10000&&basic<=20000)

{

bonus=0.50*basic;

gross-basic+ bonus;

System.out.println("Your Salary is= "+gross);

}

else if(basic>20000&&basic<=50000)

{

bonus 0.25*basic;

gross-basic+bonus;

System.out.println("Your Salary is = "+gross);

}

else

{

bonus=0.0;

gross basic+ bonus;

System.out.println("Your Salary is = "+gross);

}

}

}

Output

Your Salary is = 30000.0

3. while statement

This is another form of while statement which is used to have iteration of the statement for the any number of times. The syntax is

while(condition)

{

statement 1;

statement 2;

statement 3;

...

statement n;

}

For example

int count=1;

while(count<=5)

{

System.out.println("I am on line number "+count);

count++;

}

Let us see a simple Java program which makes the use of while construct.

Java Program [whiledemo.java]

/*

This is java program which illustrates

while statement

*/

class whiledemo

{

public static void main(String args[])

{

int count=1,i=0;

while(count<=5)

{

i=i+1;

System.out.println("The value of i= "+i);

count++;

}

}

}

Output

The value of i= 1 The value of i= 2

The value of i= 3

The value of i= 4

The value of i= 5

4. 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 condition 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

statement 1;

statement 2;

……..

statement n;

} while(condition);

For example

int count=1;

do

{

System.out.println("I am on the first line of do-while");

System.out.println("I am on the second line of do-while");

System.out.println("I am on the third line of do-while");

System.out.println("I am on the forth line of do-while");

System.out.println("I am on the fifth line of do-while");

count++;

} while(count<=5);

Java Program [dowhiledemo.java]

/*

This is java program which illustrates

do...while statement

*/

class dowhiledemo

{

public static void main(String args[])

{

int count=1,i=0;

do

{

i=i+1;

System.out.println("The value of i= "+i);

count++;

} while(count<=5);

}

}

Output

The value of i= 1

The value of i= 2

The value of i= 3

The value of i= 4

The value of i= 5

5. switch statement

You can compare the switch case statement with a Menu-Card in the hotel. You have to select the menu then only the order will be served to you.

Here is a sample program which makes use of switch case statement -

Java Program [switchcasedemo.java]

/*

This is a sample java program for explaining

Use of switch case

*/

class switchcasedemo

{

public static void main(String args[])

throws java.io.IOException

{

char choice;

System.out.println("\tProgram for switch case demo");

System.out.println("Main Menu");

System.out.println("1. A");

System.out.println("2. B");

System.out.println("3. C");

System.out.println("4. None of the above");

System.out.println("Enter your choice");

choice (char)System.in.read();

switch(choice)

{

case '1':System.out.println("You have selected A");

break;

case '2':System.out.println("You have selected B");

break;

case '3':System.out.println("You have selected C");

break;

default:System.out.println("None of the above choices made");

}

}

}

Output

Program for switch case demo

Main Menu

1. A

2. B

3. C

4. None of the above

Enter your choice

2

You have selected B

Note that in above program we have written main() in somewhat different manner as public static void main(String args[])

throws java.io.IOException

This is IOException which must be thrown for the statement System.in.read(). Just be patient, we will discuss the concept of Exception shortly! The System.in.read() is required for reading the input from the console[note that it is parallel to scanf statement in C]. Thus using System.in.read() user can enter his choice. Also note that whenever System.in.read() is used it is necessary to write the main() with IOException in order to handle the input/output error.

6. for loop

for is a keyword used to apply loops in the program. Like other control statements for loop

can be categorized in simple for loop and compound for loop.

Simple for loop :

for (statement 1;statement 2;statement 3)

execute this statement;

Compound for loop :

for(statement 1;statement 2; statement 3)

{

execute this statement;

execute this statement;

execute this statement; that's all;

}

Here

Statement 1 is always for initialization of conditional variables,

Statement 2 is always for terminating condition of the for loop,

Statement 3 is for representing the stepping for the next condition.

For example:

for(int i=1;i<=5;i++)

{

System.out.println("Java is an interesting language");

System.out.println("Java is a wonderful language");

System.out.println("And simplicity is its beauty");

}

Let us see a simple Java program which makes use of for loop.

Java Program [forloop.java]

/*

This program shows the use of for loop

*/

class forloop

{

public static void main(String args[])

{

for(int i=0;i<=5;i++)

System.out.println("The value of i: "+i);

}

}

Output

The value of i: 0 

The value of i: 1

The value of i: 2

The value of i: 3

The value of i: 4

The value of i: 5

Object Oriented Programming: Unit I: Introduction to OOP and Java : Tag: : with Example Java Programs - Control Statements