0% found this document useful (0 votes)
49 views7 pages

Object Oriented Programming Lab # 02

The document discusses different types of decision making statements in Java including if, if-else, nested if, switch, and nested switch statements. It provides the syntax and description of each statement. The document also includes examples of programs using each type of statement to demonstrate their usage.

Uploaded by

Ahmed Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views7 pages

Object Oriented Programming Lab # 02

The document discusses different types of decision making statements in Java including if, if-else, nested if, switch, and nested switch statements. It provides the syntax and description of each statement. The document also includes examples of programs using each type of statement to demonstrate their usage.

Uploaded by

Ahmed Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Object Oriented Programming Lab # 02

Lab # 2
Aim:
Understanding the decision making statements in Java.
Theory:
Decision making structures requires the programmer to specify one or more conditions to
be evaluated or tested by the program, along with a statement or statements to be executed
if the condition is determined to be true, and optionally, other statements to be executed if
the condition is determined to be false. Java provides following types of decision making
statements.

Statement Description

if statement An if statement consists of a boolean expression followed by one or more


statements.
Syntax:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}

if...else statement An if statement can be followed by an optional else statement, which


executes when the boolean expression is false.
Syntax:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}
If the boolean expression evaluates to true, then the if block of code is
executed, otherwise else block of code is executed.

nested if You can use one if or else if statement inside another if or else
statements if statement(s).
Syntax:
if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
Object Oriented Programming Lab # 02

}
}

switch statement A switch statement allows a variable to be tested for equality against a list
of values.
Syntax:
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */

/* you can have any number of case statements */


default : /* Optional */
statement(s);
}

nested switch You can use one switch statement inside another switch statement(s).
statements Syntax:
switch(ch1)
{
case 'A':
printf("This A is part of outer switch" );
switch(ch2)
{
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* inner B case code */
}
break;
case 'B': /* outer B case code */
}

Java Programming Lab Exercises:


i. if-else Statement

Program:

package com.mycompany.lab2;

public class Program {


Object Oriented Programming Lab # 02

public static void main(String[] args)

/* local variable definition */

int a = 100;

/* check the boolean condition */

if (a < 20)

/* if condition is true than print the following */

System.out.println("a is less than 20");

else

/* if condition is false than print the following */

System.out.println("a is not less than 20");

System.out.println("value of a is : " +a);

Output:
Object Oriented Programming Lab # 02

ii. if...else if...else Statement


Program:

package com.mycompany.lab2;

public class Program {

public static void main(String[] args)


{
/* local variable definition */
int a = 100;

/* check the boolean condition */


if (a == 10)
{
/* if condition is true then print the following */
System.out.println("Value of a is 10");
}
else if (a == 20)
{
/* if else if condition is true */
System.out.println("Value of a is 20");
}
else if (a == 30)
{
/* if else if condition is true */
System.out.println("Value of a is 30");
}
else
{
Object Oriented Programming Lab # 02

/* if none of the conditions is true */


System.out.println("None of the values is matching");
}
System.out.println("Exact value of a is: " +a);
}
}

Output:

iii. switch Statement

Program:

package com.mycompany.lab2;

public class Program {

public static void main(String[] args)


{
/* local variable definition */
char grade = 'B';

switch (grade)
{
case 'A':
System.out.println("Excellent!");
break;
case 'B':
case 'C':
System.out.println("Well done");
break;
case 'D':
System.out.println("You passed");
break;
case 'F':
System.out.println("Better try again");
Object Oriented Programming Lab # 02

break;
default:
System.out.println("Invalid grade");
break;
}
System.out.println("Your grade is " +grade);

}
}

Output:

iv. Nested switch Statement

Program:

package com.mycompany.lab2;

public class Program {

public static void main(String[] args)


{
int a = 100;
int b = 200;

switch (a)
{
case 100:
System.out.println("This is part of outer switch ");
switch (b)
{
case 200:
System.out.println("This is part of inner switch ");
break;
}
Object Oriented Programming Lab # 02

break;
}
System.out.println("Exact value of a is : " +a );
System.out.println("Exact value of b is : " +b);

}
}

Output:

Lab Task:

a. Write a program that determines that number entered by user is even or odd.

b. Write a program which takes user’s input for age and on basis of that input gives
the following output:
If Age is greater than 45 then prints the message “You are old stay at home and wait for
call”
If Age is less than 30 then prints “Hey! Man enjoy your life with your kids”
Use Switch statement in this Task.

c. Write a Java program that takes an integer as amount of money entered to be


drawn. If the amount is less than or equal to current balance, then print message
“Amount is available.” And if the amount entered by the user is greater than current
balance, then print the message “Amount is not available.”

You might also like