NMK20703 Lab Module 3 - Java Codes_Command Statements
NMK20703 Lab Module 3 - Java Codes_Command Statements
LAB MODULE 3
Learning Outcome:
Introduction
When you write a computer program, there are times when you need the computer to be more
selective about what it does. For example, if you have written a program to balance your
checkbook, you might want the computer to display a warning message if your account is
overdrawn. The computer should display this message only if your account is overdrawn. If it isn’t,
the message would be inaccurate and emotionally upsetting. The way to accomplish this task in a
Java program is to use a conditional, a statement that causes something to happen in a program
only if a specific condition is met.
When a Java program makes a decision, it does so by employing a conditional statement. During
this hour, you check the condition of things in your Java programs using the conditional keywords
if, else, switch, case, and break. You also use the conditional operators ==, !=, <, >, <=, >=, and ?,
along with Boolean variables.
Decision making structures have one or more conditions to be evaluated or tested by the program,
along with a statement or statements that are 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.
Following is the general form of a typical decision making structure found in most of the
programming languages:
2
NMK20703 Object-oriented Programming: Lab Module 3
Statement Description
if statement An if statement consists of a Boolean
expression followed by one or more
statements.
3
NMK20703 Object-oriented Programming: Lab Module 3
4
NMK20703 Object-oriented Programming: Lab Module 3
Loops
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general form of a loop statement in most of the programming languages:
5
NMK20703 Object-oriented Programming: Lab Module 3
Java programming language provides the following types of loops to handle looping requirements.
Click the following links to check their detail.
6
NMK20703 Object-oriented Programming: Lab Module 3
7
NMK20703 Object-oriented Programming: Lab Module 3
Loop control statements change execution from its normal sequence. When execution leaves a
scope, all automatic objects that were created in that scope are destroyed.
Java supports the following control statements. Click the following links to check their detail.
8
NMK20703 Object-oriented Programming: Lab Module 3
Syntax
for(declaration : expression)
{
//Statements
}
9
NMK20703 Object-oriented Programming: Lab Module 3
• Declaration: The newly declared block variable, is of a type compatible with the
elements of the array you are accessing. The variable will be available within the for
block and its value would be the same as the current array element.
• Expression: This evaluates to the array you need to loop through. The expression
can be an array variable or method call that returns an array.
Example
Answered Question
A. I declared a variable inside a block statement for an if. When the if was done, the definition
of that variable vanished. Where did it go?
Answer:
In technical terms, block statements form a new lexical scope. This means that if you
declare a variable inside a block, it’s visible and usable only inside that block. When the block
finishes executing, all the variables you declared go away.
It’s a good idea to declare most of your variables in the outermost block in which they’ll be
needed—usually at the top of a block statement. The exception might be simple variables, such
as index counters in for loops, where declaring them in the first line of the for loop is an easy
shortcut.
B. Why can’t I use switch with strings?
10
NMK20703 Object-oriented Programming: Lab Module 3
Answer:
You can. If it isn’t working in NetBeans, you must make sure that you have a current
version of Java installed and your development environment has been set up to use it. In
NetBeans, to see whether the current project is set up for Java 8, choose File, Project Properties
to open the properties dialog. Choose Librariesvin the Categories pane; then set Java Platform to
JDK 8 if it isn’t already.
Exercises:
1- What kind of loop is used to execute the statements in the loop at least once before the
conditional expression is evaluated?
A. do-while
B. for
C. while
3- Which instance variable of an array is used to find out how big it is?
A. size
B. length
C. MAX_VALUE
4- Given the code in (Figure Ex 4) bellow, What will be the value of x when it is displayed?
A. 9.0
B. 11.0
C. 15.0
D. The program will not compile.
11
NMK20703 Object-oriented Programming: Lab Module 3
Figure Exercise 4.
5- Create a class that takes words for the first 10 numbers (“one” to “ten”) and converts them
into a single long integer. Use a switch statement for the conversion and command-line
arguments for the words.
12