Computer Programming
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
Logical (boolean)expression - an expression that
has a value of either true or false.
Ex.
Suppose i and j are integers.
i>j
Logical (boolean) values- the values true and false
44
Relational Operators and Primitive Data Types
You can use the relational operators with integral and floating-point primitive
data types.
44
Logical (Boolean) operators- enable to combine
logical expressions.
44
44
ORDER OF PRECEDENCE
44
ORDER OF PRECEDENCE
44
EXERCISE #1(LOGICAL OPERATORS)
Evaluate the following expressions:
EXPRESSION VALUE
boolean found = true; 1. !found
boolean flag = false; 2. x > 4.0
double x = 5.2;
3. !found && (x >= 0)
double y = 3.4;
int a = 5, b = 8; 4. !(found && (x >= 0))
int n = 20; 5. x + y <= 20.5
char ch = B; 6. (n >= 0) && (n <= 100)
7. ('A' <= ch && ch <= 'Z')
8. (a + 2 <= b) && !flag
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
In Java, a condition is represented
by a logical (boolean) expression;
conditions are either true or false.
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
pseudocode syntax Java syntax
if by itself: if by itself:
if <condition> if (<condition>)
<statement(s)> {
<statement(s)>
}
if STATEMENT
pseudocode syntax Java syntax
if, else: if, else:
if (<condition>)
if <condition>
{
<statement(s)>
<statement(s)>
else
}
<statement(s)> else
{
<statement(s)>
}
EXERCISE #2 import java.util.Scanner;
public class IfStatementOneway {
public static void main(String[]args)
(if Statement-One Way) {
Scanner stdIn=new Scanner(System.in);
Consider the following statement: int Score;
char grade;
if score >= 90 grade='A';
grade = 'A';
System.out.print("Enter your Score: ");
Score=stdIn.nextInt();
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;
if grade >= 75 System.out.print("Enter your Grade: ");
grade=stdIn.nextInt();
Display PASSED!
else if(grade>=75)
Display FAILED! {
System.out.println("You are PASSED!");
}
else
{
System.out.println("Your are FAILED!");
}
}
}
EXERCISE #4 (Nested if-Multiple Selection)
Consider the following statement:
if (FinalGrade >= 98)
System.out.println(“With Highest Honors");
else if (FinalGrade >= 95)
System.out.println(“With High Honors");
else if (Final Grade >= 90)
System.out.println(“With Honors");
else if (FinalGrade >= 90)
System.out.println(“With Honors");
else if (FinalGrade >= 80)
System.out.println(“Very Satisfactory");
else if (FinalGrade >= 75)
System.out.println(“Satisfactory");
else
System.out.println(“Needs Improvement!");
EXERCISE #4 (Nested if-Multiple Selection)
import java.util.Scanner; else if (FinalGrade>=95)
{
public class IfStatementThreeWay System.out.println("With High Honors");
{
}
public static void main(String[]args)
{
Scanner stdIn=new Scanner(System.in); else if (FinalGrade>=90)
int FinalGrade; {
System.out.println("With Honors");
System.out.print("Enter your Score: "); }
FinalGrade=stdIn.nextInt();
else if (FinalGrade>=80)
if(FinalGrade>=98)
{
{
System.out.println("With Highest Honors"); System.out.println("Very Satisfactory");
} }
EXERCISE #4 (Nested if-Multiple Selection)
else if (FinalGrade>=75)
{
System.out.println("Satisfactory");
}
else
{
System.out.println("Needs Improvement!");
}