[go: up one dir, main page]

0% found this document useful (0 votes)
17 views24 pages

Lecture - 07 Selections (SwitchCase)

asd

Uploaded by

sirnoobbotyes
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)
17 views24 pages

Lecture - 07 Selections (SwitchCase)

asd

Uploaded by

sirnoobbotyes
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/ 24

• Logical Operators and Combined Conditions

• Switch Statements and Conditional Expressions

• Operator Precedence & Debugging

• Scanner Class
SELECTIONS
• Math Class
LECTURE 07
• Random Number Generation

Reference : Chapter 3 Selections (75)


~ Adnan Ali
Logical Operators

The logical operators !, &&, ||, and ^ can be used to create a compound Boolean expression.
Operators ‘!’
Operators ‘&&’
Operators ‘||’
Operators ‘^’
Questions

Assuming that x is 1, show the result of the following Boolean expressions.


Questions

Suppose, when you run the following program, you enter the input 2 3 6 from the console. What is the output?
‘switch’ Statements

A switch statement executes statements based on the value of a variable or an expression.

The if statement we studied in previous slide, makes selections based on a single true or false condition. Let's
consider, there are four cases which depend on the value of status. To fully account for all the cases, nested if
statements could be used. Overuse of nested if statements makes a program difficult to read. Java provides a
switch statement to simplify coding for multiple conditions.
‘switch’ Statements

Syntax for the switch statement:


‘switch’ Statements

The switch statement observes the following rules:


1. The switch-expression must yield a value of char, byte, short, int, or String type and must always be enclosed in
parentheses. (The char and String types will be introduced in the next chapter.)
2. 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.
3. When the value in a case statement matches the value of the switch-expression, the statements starting from this
case are executed until either a break statement or the end of the switch statement is reached.
4. The default case, which is optional, can be used to perform actions when none of the specified cases matches the
switch-expression.
5. The keyword break is optional. The break statement immediately ends the switch statement.
‘switch’ Statements
‘switch’ Statements
Conditional Expressions

A conditional expression evaluates an expression based on a condition.

Conditional expressions are in a completely different style, with no explicit if in the statement. The syntax
is:

The result of this conditional expression is expression1 if boolean-expression is true; otherwise the result
is expression2.
Conditional Expressions

You might want to assign a value to a variable that is restricted by certain conditions. For example, the
following statement assigns 1 to y if x is greater than 0, and -1 to y if x is less than or equal to 0.
Operator Precedence and Associativity

Operator precedence and associativity determine the order in which operators are evaluated.
User Input and “Scanner” Class

Our programs so far have dealt with predetermined input data

• However, most programs needs some kind of user input


• The Scanner class: Allows us to read from a stream of text input, such as the keyboard
• To use it, first you must add this line to your .java file, above the class declaration:

import java.util.Scanner;
User Input and “Scanner” Class

Next, within your program, you will need to create the Scanner object:

Scanner scan = new Scanner(System.in);

Once created, the Scanner object can be used to invoke various input methods, such as:

String answer = scan.nextLine();

To use the Scanner object, you need to be aware of the different methods and how they work, as well as
how input works
How keyboard input works

The data will enter as a “stream”, held in a buffer

Made of "tokens", divided by spaces, and "lines", divided by newline characters

User types...
12 24.7 hello, goodbye, hey[Enter]
This is a line true 78 45.6[Enter]

Buffer contains...
Various “Scanner” Methods
“Math” Class

The Math class is like String (does not require import)

It is a holder for various methods performing mathematical functions, as well


as the mathematical constants pi and e:

Math.PI and Math.E

These methods are invoked using the class name, rather than creating an
object
“Math” Class Methods
“Random” Class

The Random class does require an import

import java.util.Random;

It exists for the purpose of generating random values of various types and
within ranges

To use the Random class, you do in fact have to create an object:

Random random = new Random();


“Random” Class Methods

You might also like