OOP Lab Manual 3

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH

OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH

Name: _____________________________ Roll No: __________________________________

Signature of Lab Tutor: ____________________ Date: ________________________________

LAB 3: THE USE OF CONTROL STATEMENTS IN JAVA

OBJECTIVES

 To practice the use of different control statements (for, while, do-while, if, if-else, switch)
in Java

Java Control Statements


A programming language uses control statements to cause the flow of execution to advance and
branch based on changes to the state of a program. Java’s program control statements can be put
into the following categories: selection, iteration, and jump.

Java’s Selection Statements


if Conditionals

The if conditional, which enables you to execute different bits of code based on a simple test in
Java. if conditionals contain the keyword if, followed by a boolean test, followed by a statement
(often a block statement) to execute if the test is true. Here is the general form of the if statement:
if (condition) statement1;
else statement2;
Here, each statement may be a single statement or a compound statement enclosed in curly
braces (that is, a block). The condition is any expression that returns a boolean value.

Nested ifs

A nested if is an if statement that is in the body of another if or else. When you nest ifs, the main
thing to remember is that an else statement always refers to the nearest if statement that is within
the same block as the else and that is not already associated with an else. Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)
As the comments indicate, the final else is not associated with if(j<20), because it is not in the
same block. Rather, the final else is associated with if(i==10). The inner else refers to if(k>100),
because it is the closest if within the same block.
The if-else-if Ladder
1
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
A common programming construct that is based upon a sequence of nested ifs is the if-else-if
ladder. It looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
The if statements are executed from the top down. As soon as one of the conditions controlling
the if is true, the statement associated with that if is executed, and the rest of the ladder is
bypassed. If none of the conditions is true, then the final else statement will be executed. The
final else acts as a default condition; that is, if all other conditional tests fail, then the last else
statement is performed. If there is no final else and all other conditions are false, then no action
will take place.
Switch Conditionals
A common practice in programming in any language is to test a variable against some value, and
if it doesn’t match that value, to test it again against a different value, and if it doesn’t match that
one to make yet another test, and so on.
A common shorthand mechanism for nested ifs that you can use in some cases allows you tests
and actions together in a single statement. This is the switch or case statement:
switch (test)
{
case valueOne:
resultOne;
break;
case valueTwo:
resultTwo;
break;
case valueThree:
resultThree;
break;
...
default: defaultresult;
}

In summary, there are three important features of the switch statement to note:
■ The switch differs from the if in that switch can only test for equality, whereas if can evaluate
any type of Boolean expression. That is, the switch looks only for a match between the value of
the expression and one of its case constants.
■ No two case constants in the same switch can have identical values. Of course, a switch
statement enclosed by an outer switch can have case constants in common.
■ A switch statement is usually more efficient than a set of nested ifs.

2
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
Iteration Statements

Java’s iteration statements are for, while, and do-while. These statements create what we
commonly call loops. As you probably know, a loop repeatedly executes the same set of
instructions until a termination condition is met.

For Loops
The for loop, repeats a statement or block of statements some number of times until a condition
is matched. for loops are frequently used for simple iteration in which you repeat a block of
statements a certain number of times and then stop, but you can use for loops for just about any
kind of loop. The for loop in Java looks like this:
for (initialization; test; increment)
{
statements;
}

The start of the for loop has three parts:


1. Initialization is an expression that initializes the start of the loop. If you have a loop index, this
expression might declare and initialize it, for example, int i = 0. Variables that you declare in this
part of the for loop are local to the loop itself; they cease existing after the loop is finished
executing.
2. Test is the test that occurs after each pass of the loop. The test must be a Boolean expression
or function that returns a boolean value, for example, i < 10. If the test is true, the loop executes.
Once the test is false, the loop stops executing.
3. Increment is any expression. Commonly, the increment is used to change the value of the loop
index to bring the state of the loop closer to returning false and completing.
The statement part of the for loop is the statement that is executed each time the loop iterates.

Nested Loops
Java supports loops to be nested. That is, one loop may be inside another. For example, here is a
program that nests for loops:
// Demonstrating the use of Nested for loop
public class NestedLoop
{
public static void main(String[] args)
{
long limit=20;
long factorial=1;
for(int i=1;i<=limit;i++)
{
factorial=1;
for(int j=2;j<=i;j++)
factorial*=j;
System.out.println("Factorial of " + i + " = " + i + "! = " + factorial);
}
}

3
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
}

While Loops
The while loop is used to repeat a statement or block of statements as long as a particular
condition is true. When condition becomes false, control passes to the next line of code
immediately following the loop. The curly braces are unnecessary if only a single statement is
being repeated.while loops look like this:
while (condition) {
bodyOfLoop;
}
The condition is a boolean expression. If it returns true, the while loop executes the statements in
bodyOfLoop and then tests the condition again, repeating until the condition is false. Note that if
the condition is initially false the first time it is tested (for example, if the first element in that
first array is 0), the body of the while loop will never be executed.

Do...while Loops

The do loop is just like a while loop, except that do executes a given statement or block until a
condition is false. The main difference is that while loops test the condition before looping,
making it possible that the body of the loop will never execute if the condition is false the first
time it’s tested. do loops run the body of the loop at least once before testing the condition. Do
loops look like this:
do {
bodyOfLoop;
}
while (condition);

Here, the bodyOfLoop part is the statements that are executed with each iteration. The do-while
loop always executes its body at least once, because its conditional expression is at the bottom of
the loop. Each iteration of the do-while loop first executes the body of the loop and then
evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise,
the loop terminates. As with all of Java’s loops, condition must be a Boolean expression.
Jump Statements
Java supports three jump statements: break, continue, and return. These statements transfer
control to another part of your program.
Using break
In Java, the break statement has three uses. First it terminates a statement sequence in a switch
statement. Second, it can be used to exit a loop. Third, it can be used as a “civilized” form of
goto. The last two uses are explained here.
Using continue
Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue
running the loop, but stop processing the remainder of the code in its body for this particular
iteration. The continue statement performs such an action. In while and do-while loops, a
continue statement causes control to be transferred directly to the conditional expression that
controls the loop. In a for loop, control goes first to the iteration portion of the for statement and
then to the conditional expression. For all three loops, any intermediate code is bypassed.
4
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
Task 1: Write a java program using loops to generate the following output:

0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Task 2: Write a java code that prompts user to enter upper and lower limit of a pyramid. (for
example if 2 & 5 is entered than program should display following pyramid)

22
333
4444
55555

5
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Task 3: Write a java program to display sum of digit of a number entered by user (at least 4 digit
number).

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
6
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH

Task 4: Write a java program to arrange the members of an array in ascending order

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Task 5: Using switch statement, write a Java program that takes a number form user and convert
digits of that number into corresponding English words. (e.g. 2640 = two six four zero)

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Task 6: Create different versions of a program that finds all the primes below 100. Create
one version that only uses the for(;;) loop (i.e., no while or do-while). Create
another version that only uses the while loop.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

7
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Task 8: Write a Java program that takes month (1-12) as the input from the user and using switch
statement displays the number of days in that month.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
8

You might also like