Conditional Statements

advertisement
Conditional Statements
Working with the Scanner class.
package file_name;
// right below the package statment
import java.util.Scanner;
This statement imports in your scanner class. This statement should be
outside your main class and ideally right after your “package”
statement.
How to declare a Scanner
void main()
{
Scanner type = new Scanner(System.in);
}
Name of class/ variable name/ reserved word/ Name of class again.
You can name it any name you want.
Capturing Scanner Statements
int k = type.nextInt();
Variable type / variable name / name of scanner / scanner method
Now write the scanner inputs for a double and a String
If Statement
if( these conditions are true)
{
then execute this body of code
}
What are conditions?
What are conditions?
Conditions are logical statements that check if something is equal, or
greater than or less than or some combination.
For example:
if(a < 100) // checks if variable a is less than 100
if(a > 100) // checks if variable a is greater than 100
if(a == 100) // checks if variable a is equal to 100
More Conditions
You can check multiple conditions in one statement with the OR and
AND operations.
In an OR operation, designated by ||, if either side is true, then the
statement evaluates to true.
In an AND operation, designated by &&, both sides need to be true for
the whole statement to evaluate to true.
Examples
if(a > 50 && a < 500)
This statement checks if a is between 50 and 500
if(a == 50 && a == b)
This statement checks if a is equal to 50 and equal to b
if(a > 50 || a < 500)
This statement checks if a is between 50 and 500
if(a == 50 || a == b)
This statement checks if a is equal to 50 or b
Number 8 and 9
8. if(X > 10 && X < 100)
9. If(X < 10 || X > 100)
The else statement
The else statement is the default statement and executes when none of
the if statements evaluate to true.
else
{
// body of code
}
Notice that an else statement does not receive any conditionals, that’s
because it is the default statement.
Typical Code
if(conditions)
{
// body of code
}
else // this else respects the if statement
{
// body of code
}
Typical Code
if(conditions)
{
// body of code
}
else if(conditions) // this else if respects the if statement
{
// body of code
}
else
// this else respects the if statement
{
// body of code
}
Exercise 1
Ask for an integer input. If this number is greater than 100, display
“This number is quite large.”
Otherwise, display “This number is small.”
Exercise 2
Ask for an input of two doubles. Check to see if the they are both
equal first. If they are, display “They are equal.”
Then check if they are both below 100. If they are, display “Both
numbers are small.”
If neither condition hits, display “These are strange number.”
Exercise 3
Ask the user to input three integers.
First, check to see if they are all equal. If they are, display “These
numbers are all equal.”
Second, check to see if any two are equal. If they are, display “Two of
these numbers are equal.”
If one are equal, display “All are different.”
Download