Iteration Loops

Total Page:16

File Type:pdf, Size:1020Kb

Iteration Loops

Orange Coast College Business, Computing, & Career Education Division CS/CIS Department CS A170 (Java Programming ) Lab 11 Instructor: Martha Malaty CS 170 – Lab 11 Iteration – Loops

Introduction The purpose of this homework is to introduce you to the fundamental concepts of loops using the While, Do-While, and For statement

Notes  For all programs the header from the template should be used.  Attach a Word file including the “Pseudocode” of the algorithm, as well as screen shots for running the programs.  Add reasonable comments through all the program.  All lab and homework classes should be grouped in one project.  Make sure to follow the naming convention for the project name.

I. Stars  Use nested for loops to write a program named “Stars“ that asks for the number of stars to be displayed and then generates the output pattern of stars shown below: o Note that there are no spaces in between stars. Sample Output:

Page 1 of 3 Orange Coast College Business, Computing, & Career Education Division CS/CIS Department CS A170 (Java Programming ) Lab 11 Instructor: Martha Malaty

II. Tracing Note: You can add as many rows in the table as needed a) What is the output of the following code segment? 1.int x = 4, y = 5, z = y + 6; 2. while(((z - x) % 4) != 0) { 3. System.out.println(z + " "); 4. z += 7; } Line x y z Condition Output

b) What is the output of the following code segment? 1. int num = 31; 2. int sum = 2;

Page 2 of 3 Orange Coast College Business, Computing, & Career Education Division CS/CIS Department CS A170 (Java Programming ) Lab 11 Instructor: Martha Malaty 3. while ( num < 40 ) { 4. sum = (num % 3); 5. num += sum; 6. System.out.println(num + " " + sum + " "); }

Line num sum Condition Output

c) What is the output of the following code segment?

1. int num = 31; 2. int sum = 2; do { 3. sum = (num % 5); 4. num += sum; 5. System.out.println(num + " " + sum + " "); }6. while ( num <= 40);

Line num sum Condition Output

Page 3 of 3

Recommended publications