IDEONE Java Basics 104 – Operator Precedence
PREREQUISITE
This tutorial uses the online compiler IDEone (www.ideone.com ).
IDEone gives you few benefits:
1) Zero setup. You just need Internet Connection and a modern Web Browser.
2) Online storage. You can refer back to the codes at later time.
3) Public codes available. You can also see how others write codes and learn from them as well.
4) Online Learning. You can ask a companion at remote location to help edit the codes or review your codes for improvements or troubleshooting algorithms.
STEPS
1) Start with the basic template
Browse http://ideone.com/l3lzo6
Clone the code above. (Click the Fork button).
2) Edit the codes.
2-1) Change the variable data type to int and add a new variable third_number in the declaration statement.
int first_number, second_number, third_number, answer;
|
2-2) Change the assignment statements.
first_number = 100;
second_number = 75;
third_number = 25;
|
2-3) Change the operation statement.
answer = first_number - second_number + third_number;
|
2-4) Change the output statement.
System.out.println("Result = " + answer);
|
2-5) Run.
step.1: 100-75=25
step.2: 25+25=50
2-6) Change the operation statement.
answer = first_number + second_number - third_number;
|
2-7) Run
step.1: 100+75=175
step.2: 175-25=25
2-8) Change the operation statement.
answer = first_number + second_number * third_number;
|
2-9) Run
step.1: 75x25=1875
step.2: 100+1875=1975
2-10) Change the operation statement.
answer = (first_number + second_number) * third_number;
|
2-11) Run
step.1: 100+75=175
step.2: 175x25=4375
2-12) Find out whether the following operations result with the same outcome.
Operation A:
answer = first_number / second_number * third_number;
|
Operation B:
answer = first_number * second_number / third_number;
|
Lessons:
Operator Precedence Rule:
- Multiply and Divide - Treated equally, but have priority over Addition and Subtraction
- Add and Subtract - Treated equally but have a lower priority than multiplication and division
No comments:
Post a Comment