Java Programming
Conditional Statements IF-ELSE-ELSE-IF
Objectives
 Conditional Statements
 Relational Operators
 IF Statements
 IF - ELSE Statements
 IF - ELSE IF – ELSE Statements
 Nested Conditional Statements
 Equals Function
 Logical Operators
 Grade Average Program
Conditional Statements
Allows a program to take action based
on the given condition. It makes our
program smarter.
Conditional Statements
Typically, It compares two values so
that our program can decide what
action should be taken.
Relational Operators
LABEL SYMBOL SYNTAX
Equals == X == y
NOT Equals != X != y
Less Than < x < y
Less Than or Equals <= X <= y
Greater Than > X > y
Greater Than or
Equals
>= X >= y
IF Statement
Handles 1 Conditional Expression, It
either does SOMETHING or NOTHING.
IF Statement
If(condition) {
//Do Everything you want here
}
IF Statement
If(age >= 18) {
System.out.println(“You Have Access!”);
}
IF – ELSE Statement
Handles 2 Conditional Expressions, It either
does the first Code Block or the second Code
Block.
IF – ELSE Statement
If(condition) {
//Do Everything you want here
}else{
//Do Everything you want Here
}
IF – ELSE Statement
If(age >= 18) {
System.out.println(“You Have Access!”);
}else{
System.out.println(“Access Denied!”);
}
IF – ELSE IF – ELSE Statement
Handles 3 or More Conditional Expressions,
The possibility of this Statements are limitless it will
run a certain code block based on the condition
IF – ELSE IF – ELSE Statement
If(condition) {
//Do Everything you want Here
}else if(condition) {
//Do Everything you want Here
}else {
//Do Everything you want Here
}
IF – ELSE IF – ELSE Statement
If(age >= 18) {
System.out.println(“You Have Access!”);
}else if(age >= 13) {
System.out.println(“You Need Parent
Consent!”);
}else {
System.out.println(“Access Denied!”);
}
NESTED Conditional Statement
A Conditional Statement within a Conditional
Statement.
PS: The Nested Conditional Statement can be
any Type of Conditional Statement.
NESTED Conditional Statement
if(condition) {
if(condition) {
//Do Anything You Want Here
}
}
NESTED Conditional Statement
If(age >= 18) {
if(isVerified) {
System.out.println(“Qualified”);
}
}
Equals Function
To compare Strings more efficiently we
need to make use of the Equals Function
because the Equal relational operators
compares the memory address not the
Content.
Equals Function
String x = “Hello”;
If(x.equals(“Hello”)) {
System.out.println(“Hi”);
}
Logical Operators
Label Symbol Syntax Description
AND &&
condition &&
condition
Both Condition
Needs To Be
True
OR ||
Condition ||
condition
Either
Conditions
needs To Be
True
NOT ! !condition
Inverts the
Current
Condition

Conditional Statements.pptx

  • 1.
  • 2.
    Objectives  Conditional Statements Relational Operators  IF Statements  IF - ELSE Statements  IF - ELSE IF – ELSE Statements  Nested Conditional Statements  Equals Function  Logical Operators  Grade Average Program
  • 3.
    Conditional Statements Allows aprogram to take action based on the given condition. It makes our program smarter.
  • 4.
    Conditional Statements Typically, Itcompares two values so that our program can decide what action should be taken.
  • 5.
    Relational Operators LABEL SYMBOLSYNTAX Equals == X == y NOT Equals != X != y Less Than < x < y Less Than or Equals <= X <= y Greater Than > X > y Greater Than or Equals >= X >= y
  • 6.
    IF Statement Handles 1Conditional Expression, It either does SOMETHING or NOTHING.
  • 7.
    IF Statement If(condition) { //DoEverything you want here }
  • 8.
    IF Statement If(age >=18) { System.out.println(“You Have Access!”); }
  • 9.
    IF – ELSEStatement Handles 2 Conditional Expressions, It either does the first Code Block or the second Code Block.
  • 10.
    IF – ELSEStatement If(condition) { //Do Everything you want here }else{ //Do Everything you want Here }
  • 11.
    IF – ELSEStatement If(age >= 18) { System.out.println(“You Have Access!”); }else{ System.out.println(“Access Denied!”); }
  • 12.
    IF – ELSEIF – ELSE Statement Handles 3 or More Conditional Expressions, The possibility of this Statements are limitless it will run a certain code block based on the condition
  • 13.
    IF – ELSEIF – ELSE Statement If(condition) { //Do Everything you want Here }else if(condition) { //Do Everything you want Here }else { //Do Everything you want Here }
  • 14.
    IF – ELSEIF – ELSE Statement If(age >= 18) { System.out.println(“You Have Access!”); }else if(age >= 13) { System.out.println(“You Need Parent Consent!”); }else { System.out.println(“Access Denied!”); }
  • 15.
    NESTED Conditional Statement AConditional Statement within a Conditional Statement. PS: The Nested Conditional Statement can be any Type of Conditional Statement.
  • 16.
    NESTED Conditional Statement if(condition){ if(condition) { //Do Anything You Want Here } }
  • 17.
    NESTED Conditional Statement If(age>= 18) { if(isVerified) { System.out.println(“Qualified”); } }
  • 18.
    Equals Function To compareStrings more efficiently we need to make use of the Equals Function because the Equal relational operators compares the memory address not the Content.
  • 19.
    Equals Function String x= “Hello”; If(x.equals(“Hello”)) { System.out.println(“Hi”); }
  • 20.
    Logical Operators Label SymbolSyntax Description AND && condition && condition Both Condition Needs To Be True OR || Condition || condition Either Conditions needs To Be True NOT ! !condition Inverts the Current Condition