Lesson 6 - Relational Operators - Control Statement
Lesson 6 - Relational Operators - Control Statement
R E L AT I O N A L O P E R AT O R S
& CONTROL
S TAT E M E N T S
44
RELATIONAL OPERATOR
An operator that allows you to make comparisons in a program.
44
ORDER OF PRECEDENCE
44
ORDER OF PRECEDENCE
44
ASSIGNMENT #1
EXPRESSION VALUE EXPLANATION
1. !('A' > 'B')
2. !(6 <= 7)
3. (14 >= 5) && ('A' < 'B')
4. (24 >= 35) && ('A' < 'B')
5. (14 >= 5) || ('A' > 'B')
6. (24 >= 35) || ('A' > 'B')
7. ('A' <= 'a') || (7 != 7)
8. (age >= 21) || (x == 5) where age=25
9. (7 >= 8 || 'A' < 'B') && 5 * 4 == 20
44
CONTROL STATEMENTS
Conditions
if Statement
&& Logical Operator
|| Logical Operator
! Logical Operator
switch Statement
while Loop
do Loop
for Loop
Loop Comparison
Nested Loops
Boolean Variables
Input Validation
Boolean Logic
Expression Evaluation Practice
CONDITIONS
Throughout this quarter, you’ll see if statements and loop statements
where conditions appear within a pair of parentheses, like this:
if (<condition>)
{
...
}
while (<condition>)
{
...
}
CONDITIONS
Each condition involves some type of comparison and the
comparisons use comparison/relational operators.
Relational Operators (==, !=, <, >, <=, >=)
Each comparison operator evaluates to either true or false.
==
Tests two operands for equality.
3 == 3 evaluates to true
3 == 4 evaluates to false
Note that == uses two equal signs, not one!
!=
Tests two operands for inequality.
The != operator is pronounced “not equal.”
The <, >, <=, and >= operators work as expected.
if STATEMENT
Use an if statement if you need to ask a question in order to
determine what to do next.
There are three forms for an if statement:
if by itself
Use for problems where you want to do something or nothing.
if, else
Use for problems where you want to do one thing or another
thing.
if, else if
Use for problems where you want to do one thing out of three
or more choices.
if STATEMENT
if by itself: if by itself:
if <condition> if (<condition>)
<statement(s)> {
<statement(s)>
}
if STATEMENT
pseudocode syntax Java syntax
if(Score>=90)
{
System.out.println("Your grade is: " +
grade);
}
}
}
EXERCISE #3 import java.util.Scanner;
public class IfStatementTwoWay
{
(if Statement-Two Way) public static void main(String[]args)
{
Scanner stdIn=new Scanner(System.in);
Consider the following statement: int grade;