In this lab, you will learn more about selection statements. You will get familiar to

JAVA PROGRAMMING I LAB ENG. GHADIR S. AL JARO Objective: In this lab, you will learn more about selection statements. You will get familiar to neste...
Author: Adelia Anderson
1 downloads 0 Views 376KB Size
JAVA PROGRAMMING I LAB

ENG. GHADIR S. AL JARO

Objective: In this lab, you will learn more about selection statements. You will get familiar to nested if and switch statements.

Nested if Statements: When you use if or if...else statement, you can write any statements to be executed in true or false cases including another if / if…else statement. The inner if is called nested if. Check the following code to see how it works.

The output:

1

JAVA PROGRAMMING I LAB

ENG. GHADIR S. AL JARO

Note that the else clause matches the most recent unmatched if clause in the same block. You can use braces to make your own match. See next code.

The output:

Here nothing is printed because the second if statement has nothing to be executed in the false case.

Switch Statement Switch is a shorthand for nested if…else statements. It is used to replace a stack of if statements that all relate to the same quantity. Here is the full syntax for the switch statement: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; ... 2

JAVA PROGRAMMING I LAB

ENG. GHADIR S. AL JARO

case valueN: statement(s)N; break; default:

statement(s)-for-default;

}

When using switch statement, observes the following rules: 

The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses.



The value1, and valueN must have the same data type as the value of the switch-expression. Note that value1, and valueN are constant expressions, meaning that they cannot contain variables, such as 1 + x.



When the value in a case statement matches the value of the switchexpression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached.



The keyword break is optional. The break statement immediately ends the switch statement.



The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.



The case statements are checked in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end.

3

JAVA PROGRAMMING I LAB

ENG. GHADIR S. AL JARO

Caution: Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This is referred to as fallthrough behavior. For example, the following code prints character a three times if ch is 'a': switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); }

Exercise: Write a program that reads a character from the user then prints a word beginning with this character. To simplify work, test for a, b, c characters. Otherwise print invalid character.

Homework: 

Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2000, the program should display that February 2000 has 29 days. If the user entered month 3 and year 2005, the program should display that March 2005 has 31 days. 4

JAVA PROGRAMMING I LAB 

ENG. GHADIR S. AL JARO

(Game: scissor, rock, paper) Write a program that plays the popular scissorrock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs:

Hint: for generating a random number you can use Math.random() method. (int)(Math.random() *10) returns a random single-digit integer (i.e., a number between 0 and 9).

5