Loops
Algorithms for Numbers
Algorithms for Strings
Others
Tracing Code
100

The way to end this loop:

while (x != -1) { 

System.out.print("Enter -1 to stop"); x = scan.nextLine(); 

}

User Input

100

What is the first and last numbers

int count = 4; 

while (count <= 7) {
  count++;  

System.out.print(count + " "); 

}

5 and 8
100

This code do this for strings. 


for(int i = 0; i < str.length(); i++) { System.out.print(str.substring(i,i+1)); 

}

iterate through the characters

100

Loop within a loop

Nested Loops


100

This code does this for strings:

int count = 0; 

for(int i = 0; i < str.length(); i++){ if(str.substring(i,i+1).equals("e")){ 

count++; 

}

Count the number of times the letter "e" appears in a string

200

The three parts that composes the for loop?

i = 0; 

initializes loop control variable i < 10; 

tells loop when to stop i++; tells the loop what to count by

200

int num = 49; while (num > 0)
{
  if (num % 2 == 0)
  {     num++;
  }   else
  {     num--;
  } 

}

Infinite Loop

200

The following code does what?

String str = "AP-CSA";

for (int i = 0; i < str.length(); i++)

{

if (str.substring(i, i + 1).equals("A"))

{

System.out.print(i + " ");

}

}

Find the index of A
200

This can only be access within the body which it was declare.

Local scoping

200

This code does this to a string:

String str = "loop"; 

String newStr = ""; 

for(int i = str.length(); i > 0; i--){ 

newStr += str.substring(i-1, i); 

}

Looping backwards through a string.

300

This code does what?


int a = 100;


while (a > 1)

{

System.out.println("$");

a /= 2;

}

Prints the number of $

300

What is the result of the following code:

for (int k = 30; k > 0; k = k - 3) {

 if (k % 5 == 0) { 

System.out.print(k + " "); 

}

30 15

300

Is the purpose of this code.


String str = "loop";
String newStr = "";
for (int i = str.length(); i > 0; i--)
{
  newStr += str.substring(i - 1, i);
}
System.out.println(newStr);

Concatenating strings

300

This can be access everywhere

Global Scoping


300

Is intended to print the sum of all the odd integers from 0 up to and including 101. 

int r = 0;

int sum = 0;

/* missing loop header */

{

if (r % 2 == 1)

{

sum += r;

}

r++;

}

System.out.println(sum);

while (r <= 101)

400

What is the wrong?

Code Segment I

for (int i = 0; i < 10; i++)

{

System.out.print( "*" );

}

Code Segment II

for (int i = 1; i <= 10; i++)

{

System.out.print( "*" );

}

The output of the code segments is the same because the loops in both code segments iterate 10 times.

400

purpose of this code.


public int numDigits(int num)

{

int count = 0;

while (num != 0)

{

count++;

num = num / 10;

}

return count;

}

print the number of digits in the parameter num.

400

This code does what?

String str = "Mississippi";
String newStr = "";
for (int i = 0; i < str.length(); i++)
{
  if (!str.substring(i, i + 1).equals("i"))
  {
    newStr += str.substring(i, i + 1);
  }
}
str = newStr;

Removing letter "i"

400

What is the purpose of this code?

For (int i = 0; i < str.length; i++) {System.out.println(str.substring(i, i+1);}

String traversal
400

The result is this for the code segement:

int a = 1;

String result = "";

while (a < 20)

{

result += a;

a += 5;

}

System.out.println(result);

161116

500

When would it be more beneficial to use a while loop instead of a for loop?

When you need your loop to be controlled by user input.


 

500

This code does what?

int sum = 0;

for (int i = 1; i <= n; i++) {

            sum += (i * i);

}

System.out.println(sum);

Sum of squares 

500

The code does this.

for (int i=0; i<n; i++)

        {

            for (int j=n-i; j>1; j--)

            {

                // printing spaces

                System.out.print(" ");

            }

            for (int j=0; j<=i; j++ )

            {

                // printing stars

                System.out.print("* ");

            }

            System.out.println();

        }

Print a pyramid 

500

Is the way we measure code efficiency.

Big O notation, counting variable

500

for (int j = 4; j > 0; j--) { 

for (int k = 1; k <= j; k++) { 

System.out.print(k + " "); 

System.out.println(); 

}

1 2 3 4 

1 2 3 

1 2 

1