05_Conditionals_and_Loops

The compareto method compares strings based on the

This preview shows page 23 - 32 out of 37 pages.

The compareTo method compares Strings based on the value of its characters. What does the following code print? String food1 = "Apple" , food2 = "Banana" ; if (food1.compareTo(food2) < 0) { System.out.println(food1 + " before " + food2); } else { System.out.println(food2 + " before " + food1); }
Image of page 23

Subscribe to view the full document.

COMP 1210 – Fundamentals of Computing I Slide 5 - 24 Comparing Data Remember that any upper case value will have a lower value than any lower case value String food3 = "apple" , food4 = "Carrot" ; if (food3.compareTo(food4) < 0) { System.out.println(food3 + " before " + food4); } else { System.out.println(food4 + " before " + food3); } The String class has the equalsIgnoreCase and compareToIgnoreCase methods
Image of page 24
COMP 1210 – Fundamentals of Computing I Slide 5 - 25 Indentation Revisited Remember that indentation in the Java language is for the human reader, and is ignored by the computer if (total > MAX) System.out.println ("Error!!"); errorCount++; Despite what is implied by the indentation, the increment will occur whether the condition is true or not
Image of page 25

Subscribe to view the full document.

COMP 1210 – Fundamentals of Computing I Slide 5 - 26 Block Statements Several statements can be grouped together into a block statement delimited by braces A block statement can be used wherever a statement is called for in the Java syntax rules Our coding standard (supported by Checkstyle) requires blocks in if statements if (total > MAX) { System.out.println ("Error!!"); errorCount++; }
Image of page 26
COMP 1210 – Fundamentals of Computing I Slide 5 - 27 Nested if Statements The statement executed as a result of an if statement or else clause could be another if statement These are called nested if statements • An else clause is matched to the last unmatched if (no matter what the indentation implies) Braces can be used to specify the if statement to which an else clause belongs See Taxes.java
Image of page 27

Subscribe to view the full document.

COMP 1210 – Fundamentals of Computing I Slide 5 - 28 Infinite Loops A statement in the body of a while loop eventually must make the loop condition false Otherwise, we have an infinite loop , which will execute until the program is interrupted from outside the loop (usually by the user) Common logical error Double check the logic of a program to ensure that your loops will terminate normally
Image of page 28
COMP 1210 – Fundamentals of Computing I Slide 5 - 29 Infinite Loops An example of an infinite loop: int count = 1; while (count <= 25) { System.out.println (count); count = count - 1; } This loop will continue executing until interrupted (“Control-C” in DOS window or “End” on jGRASP Run I/O tab) or until an underflow error occurs CountInfinite.java
Image of page 29

Subscribe to view the full document.

COMP 1210 – Fundamentals of Computing I Slide 5 - 30 Nested Loops Similar to nested if statements, loops can be nested as well That is, the body of a loop can contain another loop For each iteration of the outer loop, the inner loop iterates completely
Image of page 30
COMP 1210 – Fundamentals of Computing I Slide 5 - 31 Nested Loops How many times will the string "Here" be printed?
Image of page 31

Subscribe to view the full document.

Image of page 32
You've reached the end of this preview.
  • Fall '07
  • CROSS
  • Logic, Boolean Algebra, Control flow

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern