{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Lesson Set 7 - CS 1136 Lab 7 Continuation of loops...

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 1136 Lab 7 Continuation of loops Carefully examine the program below and try to work out what occurs in the nested for loops. What do you predict the output will be? import java.util.Scanner; public class Lab7_Loops { public static void main(String args) { Scanner keyboard = new Scanner(System.in); int x, y; System.out.print(“Enter an integer in the range “ + “ 1- 20: “ ); x = keyboard.nextInt(); System.out.print(“Enter another integer in the range “ + “ 1- 20: “ ); y = keyboard.nextInt(); for (int i = 1; i <= y; i++) { for ( int j = 1; j <= x; j++) { System.out.print( „@‟ ); } System.out.println(); } System.exit(0); } } Now enter the program above into your compiler, compile, and run it. Was your prediction correct? If not, you need to practice more with nested for loops; you will find that this type of programming construct is very common in all programming languages. If you can grasp the logic above, you can apply that understanding to programs written in just about any programming language. Now make the following changes to the variable names: Replace: x‟s with num_of_columns y‟s with num_of_rows i‟s with row j‟s with column After making these changes examine the code again. It should be much easier to understand now. This example illustrates why you should always use descriptive names for your variables in your programs. Exercise 1: Write a program that reads five integers from the keyboard. Each integer is between 1 and 10 ( you will need to validate the user ’s input; if you reject the user’s input, you must re-read the input until a valid quantity is entered...
View Full Document

{[ snackBarMessage ]}

### Page1 / 5

Lesson Set 7 - CS 1136 Lab 7 Continuation of loops...

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

View Full Document
Ask a homework question - tutors are online