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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
allow comparing the contents of String variables correctly. These methods are
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course CS 1136 taught by Professor N/a during the Fall '08 term at University of Texas at Dallas, Richardson.

Page1 / 3

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

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

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