Intro to Java Web-Notes_Part32

Intro to Java Web-Notes_Part32 - eectl Home About Us...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: eectl Home - About Us lf-Else Examples Each business has its own rules on how to report business related expenses, such as lunches and dinners. Often lunches and dinners are automatically approved for reimbursement if they are below a certain amount, but require special approval if that amount is exceeded. The following code shows a simple program to help with the reimbursement process. In this example the employee is allowed 20 dollars for lunch and 50 dollars for dinner: int lunch : ?; int dinner = ?; if (lunch > 20) i int excessAmount : lunch — 20; System.out.print1n("Fi11 out form 2311 to request the extra $" + excessAmount + " of lunch reimbursement.")' I else System.out.print1n("Approvedz $" + lunch + " for lunch"); if (dinner > 50) i int excessAmount : dinner — 50; System.out.print1n("Fi11 out form 2312 to request the extra $" + excessAmount + " of dinner reimbursement."); else System.out.println("Approved: $" + lunch + " for dinner"); The code above uses two seperate if-else statements. It is sometimes difficult for the beginning programmer to recognize where one if-else stops and the next one starts. In the above structure, the two if-else structures had the following bounds: int lunch = ?; int dinner = ?; int exceeeAmcunt = lunch — ED; System.cut.println["Fill cut fcrm 2311 tc request the extra 3” + j: exceeeAmcunt + " cf lunch reimbursement."j; " System.cut.println["Fill cut fcrm 2312 tc request the extra 3” exceeeAmcunt + " cf dinner reimbursement."j; The next example intended as and exercise in identifying seperate if and if—else structures: int x : ?; int y : ?; if (X > 100) Sys:em.ot'.prii:li("One"); ( else Sys:em.OL’.prid:lq("Two"); ( Sys:em.ot:.prii:li("Three"); Sys:em.OL’.priq:lq("Four"); ( ( Sys:em.ot'.prii:li("Five"); ( Sys:em.OL’.prid:lq("Six"); Can you identify the seperate if and if—else statements in the above example? What output is generated by the code above ifx is 35 and y is 16? Or ifx is 123 and y is 200? What is the output ifx is 3000 and y is 14? Group Statements with { }'s if Necessary An i f—el se statement can only have a one statement (simple or compound) between the if (and its boolean condition) and the keyword else. This code will not compile because the compiler cannot find the if that goes with the else (the i f is too far away -- i.e., there is more than one statement between the i f and the el se): if (x > 100) System.out.println("Large number."); System.out.println("More than one hundred."); else ...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

Intro to Java Web-Notes_Part32 - eectl Home About Us...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online