loop Flashcards

1
Q

identify what would ‘i’ be at the end of the code in:

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

A

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

correct the following:

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

A

for ( int i = 1 ; i <= 3 ; i++ ) // the for command comes without the ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

print the output of the following:
for ( int i = 1 ; i <= 3 ; i++ )
System.out.println(“Hello”);

System.out.println(“end.”);

A

hello
hello
hello
end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

print the output of the following:
for ( int i = 1 ; i <= 3 ; i++ )
{
System.out.println(“Hello”);

System.out.println(“end.”);
}

A

hello
end.
hello
end.
hello
end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

print the output of the following:
for ( int i = 1 ; i <= 3 ; i++ ) ;
{
System.out.println(“Hello”);
System.out.println(“end.”);

A

hello
end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

print the output of the following:
for ( int i = 1 ; ; i++ ) ;
{
System.out.println(“Hello”);

A

infinite loop.
it will go on forever since there is no condition statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

print the output of the following:

for (int i = 1 ; i <= 9 ; i += 2)

System.out.println(i + “”)

A

1
3
5
7
9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

print the output of the following:
for ( int i = 1 ; i <= 10 ; i– )
System.out.print(“*”);

A
  • infinite
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

convert the following to a while code:
// read 3 grads from user then find sum and average :
102
103 int sum = 0 ;
104
105 int num ;
106
107 for(int i = 1 ; i <= 3 ; i++)
108 {
109 System.out.println(“Enter number : “);
110 num = input.nextInt() ;
111 sum = sum + num ;
112
113 }
114
115 System.out.println(“sum = “ +sum );
116 System.out.println(“Average = “ + ( sum / 3 ));

A

120 int sum = 0 ;
121
122 int num ;
123 int i = 1;
124
125 while( i <= 3 )
126 {
127 System.out.println(“Enter number : “);
128 num = input.nextInt()
129 sum = sum + num ;
130 i++ ;
131 }
132
133 System.out.println(sum );
134 System.out.println(“Average = “ + ( sum / 3 ));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do you use when the question asks you to read a cont. set of numbers but asks to stop at a certain num/char/etc.

A

it is an essential loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

explain why we added num = input.nextInt() twice in:
System.out.println(“Enter number, to stop enter -1”);
num = input.nextInt();

while ( num != -1)
{
sum = sum + num ;
System.out.println(“Enter number, to stop enter -1”);
num = input.nextInt();
}

A

we added a second num input in the while loop so that the user can keep entering a number
whereas if we only had the first one then the user wouldve only been able to enter a num once

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

if you were using a do-while code then you must first identify.. before the “do”

A

the variable that’s within the while condition statement before the do
ex:
char ch;

do(

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

correct the following:

do(
char ch = ‘a’;

double book = 0;
int i;

…….
)while ( int => 0 )

A

int i;

do(
char ch = ‘a’;

double book = 0;

…….
)while ( int => 0 )

\ you must identify the var used in the while condition statement before the “do”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

T/F: a ‘break’ could make you exit an if statement.

A

F, it makes you exit a for or while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what does the break do?

A

it immediately takes you out of the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

print the output:

for (int i = 1 ; i <= 5 ; i++)
{
System.out.print(“*”);
if (i == 3)
break;

System.out.println(i);

A

*1
*2
*

17
Q

if the question asks us to “ignore” instead of “stop” we use (break/continue)

A

continue

18
Q

what is the most appropriate loop type to use when you are asked to read a GIVEN CERTAIN amount of numbers?
(this is a tip)

A

for loop

19
Q

print the output of the following:
for (int i = 1 ; i <= 5 ; i++)
{
System.out.print(“*”);
if (i == 3)
continue;

System.out.println(i);

}

A

*1
*2
**4
*5

20
Q

what’s the difference between a break and continue loop

A

a break loop immediately takes you out the loop and doesn’t continue it.
a continue loop, essentially only skips/ignores the one condition set before it in the if statement but continues the loop until the end.

21
Q

what’s the difference between a break and continue loop

A

a break loop immediately takes you out the loop and doesn’t continue it.
a continue loop, essentially only skips/ignores the one condition set before it in the if statement but continues the loop until it ends.

22
Q

When drawing in loop, there will be two “for” statements: the first is for the number of ________, and the second “for” statement is for the number of __________.

A

the number of lines, the number of whatever symbol we’re printing

23
Q

what is the code for drawing a triangle:

A

for( int i = 1 ; i <= 4 ; i++)
{
for( int j = 1 ; j <= i ; j++)
System.out.print(“*”);

System.out.println();
}

24
Q

what is a flag loop/what the code?

A

boolean [variable name] = [identify it as true or false (ex: true)]

while ( [boolean variable] )
{

for loop
[variable name] = [the opposite of what we set it as (ex: false)]

}

25
Q

What is included/we use in a menu code?

A

do-while code with switch

26
Q

what does this code to a sentence?
for(int i = str.length() - 1 ; i >= 0 ; i– )
System.out.print(str.charAt(i));

A

reverse it

27
Q

what is the code for printing a number in reverse order?

A

while ( num != 0 )
{
System.out.print( num % 10);
num = num / 10;
}

System.out.println();

28
Q

what is the code for printing the factorial of a num?

A

System.out.println(“Enter number to print it’s factorial:”);
int num = read.nextInt();

int fact = 1 ;

for (int i = 1; i <= num ; i++)
fact = fact * i;

System.out.println(“factorial of” + num + “=” + fact) ;

29
Q

loop example that includes strings:
for loop will start with int 0 or 1?

A

it will start with 0.
for (int i = 0 ; ; )
because we usually have to find the index.