1.9 part 2

advertisement
Nested loop
• A nested loop is a loop that is inside another
loop
• When break is used in a nested loop, it only
breaks out of the immediate loop that it is
in. It will not break out of all loops.
• Demo: NestedLoop
Escape sequence
• Certain characters, preceded by a backslash ( \ ),
are known as escape sequences
• They are used to display certain characters, or as
display shortcuts
• What if you want to display these? “ ‘ \
• The following code would cause errors:
System.out.println(“ ” ”)
System.out.println(“ ’ ”)
System.out.println(“ \ ”)
• These errors are avoided by using an escape
sequence. For example:
System.out.println(“\’”) // this displays ‘
System.out.println(“\””) // this displays “
System.out.println(“\\”) // this displays \
• There are some other uses for escape sequences:
System.out.println(“\t”) // this tabs 5 spaces
System.out.println(“\n”) // this goes to the next
// line
• P. 183-185 (see pics)
# 6, 7, 13
***for #6: assume it is a 3-digit #; “read from the keyboard” means
the user enters the #; no Strings allowed.
***for #7, hint -- use a nested for-loop (a for-loop inside a for-loop).
Only 2 variables are needed.
***for #13: ignore the mention of the Stars program. Use nested
for-loops to print the four patterns; you do NOT need to write a
separate program for each one. You must use nested for-loops.
YOU MUST.
Download