if then else
How to resolvle ambiguity
dangling-else ambiguity
The dangling else ambiguity (cont.)
• Programming problem: shipping cost
• Country code for INDIA = 91
Cost to ship to a package to a destination in the INDIA = 5.00,
₹
except for Andaman & Nicobar and Lakshadweep islands which
cost 50.00
₹
The state code for Andaman & Nicobar and Lakshadweep = 33
• The shipping cost for a destination outside the INDIA is 200.00
₹
The dangling else ambiguity (cont.)
• Write a Java program than reads in:
and prints the shipping cost
• A country code (integer)
• A state code (integer)
The dangling else ambiguity (cont.)
• Algorithm:
The dangling else ambiguity (cont.)
• Note:
• There is no ambiguity in the algorithm given as a
structure diagram
• The ambiguity will be introduced when we write
the algorithm in the Java programming language !!!
cost = 5.0;
if ( country_code == 1 )
if ( state_code == 50 )
cost = 10.0; // Andaman & Nicobar and
Lakshadweep
else
cost = 20.0; // Outside INDIA
The dangling else ambiguity (cont.)
(The algorithm is unnecessarily confusing because I want
to show the ambiguous syntax....)
The dangling else ambiguity (cont.)
• There are 2 different yet syntactically correct ways to read
the if-statements:
The dangling else ambiguity (cont.)
• Explanation:
• The first way is to associate the keyword else with the
first keyword if
See the corresponding structure diagram
• The first if is an if-else-statement
• The second if is an if-statement that comprises
the then-part of the (first) if-else-statement
The dangling else ambiguity (cont.)
• The second way is to associate the keyword else with the
second keyword if
See the corresponding structure diagram
• The first if is an if-statement
• The second if is an if-else-statement that
comprises the then-part of the (first) if-
statement
The dangling else ambiguity (cont.)
• (This is the only ambiguous syntax in the Java
programming language which it had inherited from C)
The dangling else ambiguity (cont.)
• Resolving the dangling-else ambiguity:
• Java (and C, C++) imposes the following rule:
• The keyword else is associated to the nearest
keyword if that makes a syntactically correct
statement
The dangling else ambiguity (cont.)
• Example:
The dangling else ambiguity (cont.)
• Java program:
import java.util.Scanner;
public class DanglingElse01
{
public static void main(String[] args)
{
int country_code, state_code;
double cost;
Scanner in = new Scanner(System.in); // Construct Scanner object
System.out.print("Enter country code: ");
country_code = in.nextInt(); // Read in integer
System.out.print("Enter country code: ");
state_code = in.nextInt(); // Read in integer
The dangling else ambiguity (cont.)
cost = 5.0;
if ( country_code == 1 )
if ( state_code == 50 )
cost = 10.0; // Andaman & Nicobar and
Lakshadweep
else
cost = 20.0; // Outside INDIA
System.out.println("Shipping cost = " + cost);
}
}
The dangling else ambiguity (cont.)
• Sample execution:
Enter country code: 1 (code for INDIA)
Enter country code: 40 (not Andaman & Nicobar
and Lakshadweep)
Shipping cost = 20.0 (should be 5
₹ !)
The dangling else ambiguity (cont.)
• The reason is that the Java program is executed as follows:
The dangling else ambiguity (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/
DanglingElse01.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac DanglingElse01.java
• To run: java DanglingElse01

Ambiguous statements in a program.ppt

  • 1.
    if then else Howto resolvle ambiguity
  • 2.
  • 3.
    The dangling elseambiguity (cont.) • Programming problem: shipping cost • Country code for INDIA = 91 Cost to ship to a package to a destination in the INDIA = 5.00, ₹ except for Andaman & Nicobar and Lakshadweep islands which cost 50.00 ₹ The state code for Andaman & Nicobar and Lakshadweep = 33 • The shipping cost for a destination outside the INDIA is 200.00 ₹
  • 4.
    The dangling elseambiguity (cont.) • Write a Java program than reads in: and prints the shipping cost • A country code (integer) • A state code (integer)
  • 5.
    The dangling elseambiguity (cont.) • Algorithm:
  • 6.
    The dangling elseambiguity (cont.) • Note: • There is no ambiguity in the algorithm given as a structure diagram • The ambiguity will be introduced when we write the algorithm in the Java programming language !!! cost = 5.0; if ( country_code == 1 ) if ( state_code == 50 ) cost = 10.0; // Andaman & Nicobar and Lakshadweep else cost = 20.0; // Outside INDIA
  • 7.
    The dangling elseambiguity (cont.) (The algorithm is unnecessarily confusing because I want to show the ambiguous syntax....)
  • 8.
    The dangling elseambiguity (cont.) • There are 2 different yet syntactically correct ways to read the if-statements:
  • 9.
    The dangling elseambiguity (cont.) • Explanation: • The first way is to associate the keyword else with the first keyword if See the corresponding structure diagram • The first if is an if-else-statement • The second if is an if-statement that comprises the then-part of the (first) if-else-statement
  • 10.
    The dangling elseambiguity (cont.) • The second way is to associate the keyword else with the second keyword if See the corresponding structure diagram • The first if is an if-statement • The second if is an if-else-statement that comprises the then-part of the (first) if- statement
  • 11.
    The dangling elseambiguity (cont.) • (This is the only ambiguous syntax in the Java programming language which it had inherited from C)
  • 12.
    The dangling elseambiguity (cont.) • Resolving the dangling-else ambiguity: • Java (and C, C++) imposes the following rule: • The keyword else is associated to the nearest keyword if that makes a syntactically correct statement
  • 13.
    The dangling elseambiguity (cont.) • Example:
  • 14.
    The dangling elseambiguity (cont.) • Java program: import java.util.Scanner; public class DanglingElse01 { public static void main(String[] args) { int country_code, state_code; double cost; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter country code: "); country_code = in.nextInt(); // Read in integer System.out.print("Enter country code: "); state_code = in.nextInt(); // Read in integer
  • 15.
    The dangling elseambiguity (cont.) cost = 5.0; if ( country_code == 1 ) if ( state_code == 50 ) cost = 10.0; // Andaman & Nicobar and Lakshadweep else cost = 20.0; // Outside INDIA System.out.println("Shipping cost = " + cost); } }
  • 16.
    The dangling elseambiguity (cont.) • Sample execution: Enter country code: 1 (code for INDIA) Enter country code: 40 (not Andaman & Nicobar and Lakshadweep) Shipping cost = 20.0 (should be 5 ₹ !)
  • 17.
    The dangling elseambiguity (cont.) • The reason is that the Java program is executed as follows:
  • 18.
    The dangling elseambiguity (cont.) • Example Program: (Demo above code) – Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/ DanglingElse01.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac DanglingElse01.java • To run: java DanglingElse01