{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lesson Set 5 - CS 1136 Lab 5 Boolean and Relational...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 1136 Lab 5 Boolean and Relational Operators, Chain Logic, and Conditional Operators continued Nested Logic Nested if statements allow for greater control over the flow of a program‟s execution. Here is an example of how a nested if statement works: Scanner input = new Scanner(System.in); int grade; System.out.print( ”What grade did you receive? “ ); grade = input.nextInt(); if (grade >= 70) { if (grade >= 90) { System.out.println( ”You have mastered the “ + “course.” ); } else { System.out.println( ”You have passed the “ + “course.” ); } } else { System.out.println( ”You have failed the course.” ); } In this example if the grade is below 70 in the first if statement then it skips down to the “ You have failed the course. ” message. If the grade is above 70 then another if statement is entered. The second if statement follows a similar format but executes only if the first if statement is true. These nested if statements can be used to create multi-level menus or to design any sort of branching logic. It is very important to use curly braces ( {} ) when implementing nested if statements. The use of curly braces makes the code much easier to follow and see which else statements match up with which if statements. Comparing Strings Programs cannot use the logical operators to compare correctly the contents of Strings. Strings are objects and the String variable holds the address of where the String information is stored rather than storing the information. Consequently, using logical operators on Strings compares the addresses of where the data is stored rather than comparing the data. The String class has two convenient methods that
Image of page 1

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

View Full Document Right Arrow Icon
allow comparing the contents of String variables correctly.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ 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