cs401 Lecture4
cs401 Lecture4
CS 0401
Intermediate Programming
(with Java)
By
John C. Ramirez
Department of Computer Science
University of Pittsburgh
• These notes are intended for use by students in CS0401
at the University of Pittsburgh and no one else
• These notes are provided free of charge and may not be
sold in any shape or form
• Material from these notes is obtained from various
sources, including, but not limited to, the following:
4 Starting Out with Java, From Control Structures through Data
Structures, Fourth Edition, by Gaddis and Muganda
4 Starting Out with Java, From Control Structures through Objects,
Third to Seventh Editions by Gaddis
4 Java Software Solutions, Fourth and Fifth Editions by Lewis and
Loftus
4 Java By Dissection by Pohl and McDowell
4 The Java Tutorial (click for link)
4 The Java home page and its many sub-links:
http://www.oracle.com/technetwork/java/index.html
2
Lecture 4: if statement
• Nested ifs
4 Since both <true option> and <false option>
can be any Java statement, they can certainly
be if statements
4 This allows us to create nested if statements
• We can nest on <true option>, on <false option> or
both
• Enables us to test multiple conditions and to have a
different result for each possibility
• Let's look at an example
3
Lecture 4: if statement
double score = inScan.nextDouble();
String grade;
if (score >= 90)
grade = "A";
else if (score >= 80) Statement for first 'else' is all remaining code
grade = "B";
else if (score >= 70) Statement for second 'else' is all remaining code
grade = "C";
else if (score >= 60) Statement for third 'else' is all remaining code
grade = "D";
else Statement for last 'else' is assignment
grade = "F";
• Note that by using nesting on the 'else' clauses we are
making the conditions exclusive
– Only one grade will ever be chosen per score
• Also note that each 'else' is nested one level deeper
than the previous
– Even though they are all typed at the same level
4
Lecture 4: if statement
4 Dangling else
• The structure of a Java if statement allows for an
interesting special case:
if (score >= 95) // condition1
if (extraCredit) // condition2
grade = "A+";
else
grade = "??";
• Question: is the <else> for condition1 or condition2?
– Note how it is drawn
> Does this matter?
– As shown above the else will ALWAYS be for condition2
– Rule is that an else will always be associated with the
“closest” unassociated, non-terminated if
5
Lecture 4: if statement
7
Lecture 4: Programming Errors
• RUN-TIME ERROR
4 Error during program execution
4 Ex: InputMismatchException,
ArrayIndexOutOfBoundsException
• We will see many more of these
4 Detected by the interpreter (JRE)
• Also quite helpful identifying occurrence and location
of the error
• May not always detect underlying cause of the error
– Why did we go past the end of the array?
– Programmer may have to figure this out
8
Lecture 4: Programming Errors
• LOGIC ERROR
4 Error in the logic or meaning of the code
4 Ex: Programmer meant Z = X + Y;
• But actually typed Z = X * Y;
• Note that both statements are legal and will compile
and run, but they will give different answers
4 Ex: Dangling else
• Programmer thought else was associated with outer if
but it was really associated with inner if
4 Note that since these programs will compile and
(usually) run, detecting logic errors is often
difficult
9
Lecture 4: Programming Errors
12
Lecture 4: Example
13
Lecture 4: Counting Loop
15
Lecture 4: Sentinel-controlled Loop
16
Lecture 4: Sentinel-controlled Loops
17
Lecture 4: for loop
• init_expr
– Any legal Java statement expression
– Evaluated one time, when the loop is FIRST executed
• go_expr
– Java Boolean expression
– Evaluated PRIOR to each execution of the for loop body
> If true, body is executed
> If false, loop terminates
• inc_expr
– Any legal Java statement expression
– Evaluated AFTER each execution of the for loop body
4 These expressions make the for loop extremely
flexible
19
Lecture 4: for loop
20
Lecture 4: for loop
21
Lecture 4: for loop
22