Ch5-Loops - Java Software Solutions Chapter 5 Conditionals...

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

View Full Document Right Arrow Icon
1 Java Software Solutions Chapter 5 – Conditionals and Loops 1 Miguel A. Labrador Department of Computer Science & Engineering [email protected] http://www.csee.usf.edu/~labrador Dr. Miguel A. Labrador Topics Comparing Data The while Statement Other Repetition Statements 2 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 2 Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of certain data types Let's examine some key situations: Comparing strings 3 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 3 – Comparing characters
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Comparing Strings Remember that in Java a character string is an object The equals method can be called with strings to determine if two strings contain exactly the same characters in the same order 4 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 4 The equals method returns a boolean result if (name1.equals(name2)) System.out.println ("Same name"); Comparing Characters Unicode establishes a particular numeric value for each character, and therefore an ordering We can use relational operators on character data based on this ordering 5 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 5 For example, the character '+' is less than the character ' J' because it comes before it in the Unicode character set Comparing Characters In Unicode, the digit characters (0-9) are contiguous and in order Likewise, the uppercase letters (A-Z) and lowercase letters (a-z) are contiguous and in order 6 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 6 Characters Unicode Values 0 – 9 48 through 57 A – Z 65 through 90 a – z 97 through 122
Background image of page 2
3 Repetition Statements: The while Statement 7 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Java has three kinds of repetition statements 8 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 8 Java has three kinds of repetition statements: – the while loop – the do loop – the for loop The programmer should choose the right kind of loop for the situation The while Statement •A while statement has the following syntax: while ( condition ) statement ; 9 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 9 If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Logic of a while Loop condition evaluated 10 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 10 statement true false The while Statement An example of a while statement: int count = 1; while (count <= 5) { System.out.println (count); 11 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 11 If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

Ch5-Loops - Java Software Solutions Chapter 5 Conditionals...

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

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