Write a PL/SQL block to show the operator precedence and parentheses in several more complex expressions

  • pl/sql

Write a PL/SQL block to show the operator precedence and parentheses in several more complex expressions.

الأجوبة

DECLARE
  salary      NUMBER := 40000;
  commission  NUMBER := 0.15;
BEGIN
  -- Division has higher precedence than addition:
  
  DBMS_OUTPUT.PUT_LINE('8 + 20 / 4 = ' || (8 + 20 / 4));
  DBMS_OUTPUT.PUT_LINE('20 / 4 + 8 = ' || (20 / 4 + 8));
  
 -- Parentheses override default operator precedence:
 
  DBMS_OUTPUT.PUT_LINE('7 + 9 / 3 = ' || (7 + 9 / 3));
  DBMS_OUTPUT.PUT_LINE('(7 + 9) / 3 = ' || ((7 + 9) / 3));
 
  -- Most deeply nested operation is evaluated first:
 
  DBMS_OUTPUT.PUT_LINE('30 + (30 / 6 + (15 - 8)) = '
                      || (30 + (30 / 6 + (15 - 8))));
 
  -- Parentheses, even when unnecessary, improve readability:
 
  DBMS_OUTPUT.PUT_LINE('(salary * 0.08) + (commission * 0.12) = '
    || ((salary * 0.08) + (commission * 0.12))
  );
 
  DBMS_OUTPUT.PUT_LINE('salary * 0.08 + commission * 0.12 = '
    || (salary * 0.08 + commission * 0.12)
  );
END;
/

Sample Output:

8 + 20 / 4 = 13
20 / 4 + 8 = 13
7 + 9 / 3 = 10
(7 + 9) / 3 = 5.33333333333333333333333333333333333333
30 + (30 / 6 + (15 - 8)) = 42
(salary * 0.08) + (commission * 0.12) = 3200.018
salary * 0.08 + commission * 0.12 = 3200.018

Statement processed.

0.01 seconds
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...