Control Statements Flashcards

1
Q

Write a program that reads characters from the keyboard until a period is received. Have the program count the number of spaces. Report the total at the end of the program.

A

// Count spaces.
class Spaces {
public static void main(String[] args)
throws java.io.IOException {

char ch;
int spaces = 0;

System.out.println("Enter a period to stop.");

do {
ch = (char) System.in.read();
if(ch == ‘ ‘) spaces++;
} while(ch != ‘.’);

System.out.println("Spaces: " + spaces);   } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Show the general form of the if-else-if ladder.

A

if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
.
else
statement;

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

Given

if(x < 10)
if(y > 100) {
if(!done) x = z;
else y = z;
}
else System.out.println(“error”); // what if?

to what if does the last else associate?

A

The last else associates with if(y > 100).

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

Show the for statement for a loop that counts from 1000 to 0 by –2.

A

for(int i = 1000; i >= 0; i -= 2) // …

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

Is the following fragment valid?

for(int i = 0; i < num; i++)
sum += i;

count = i;

A

No; i is not known outside of the for loop in which it is declared.

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

Explain what break does. Be sure to explain both of its forms.

A

A break without a label causes termination of its immediately enclosing loop or switch statement.

A break with a label causes control to transfer to the end of the labeled block.

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

In the following fragment, after the break statement executes, what is displayed?

for(i = 0; i < 10; i++) {
while(running) {
if(x<y) break;
// …
}
System.out.println(“after while”);
}
System.out.println(“After for”);

A

After break executes, “after while” is displayed.

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

What does the following fragment print?

for(int i = 0; i<10; i++) {
System.out.print(i + “ “);
if((i%2) == 0) continue;
System.out.println();
}

A

Here is the answer:
0 1
2 3
4 5
6 7
8 9

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

The iteration expression in a for loop need not always alter the loop control variable by a fixed amount. Instead, the loop control variable can change in any arbitrary way.

Using this concept, write a program that uses a for loop to generate and display the progression 1, 2, 4, 8, 16, 32, and so on.

A

/* Use a for loop to generate the progression

1 2 4 8 16, …
*/
class Progress {
public static void main(String[] args) {

for(int i = 1; i < 100; i += i)
  System.out.print(i + " ");

}
}

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

The ASCII lowercase letters are separated from the uppercase letters by 32. Thus, to convert a lowercase letter to uppercase, subtract 32 from it. Use this information to write a program that reads characters from the keyboard. Have it convert all lowercase letters to uppercase, and all uppercase letters to lowercase, displaying the result. Make no changes to any other character. Have the program stop when the user enters a period. At the end, have the program display the number of case changes that have taken place.

A

// Change case.
class CaseChg {
public static void main(String[] args)
throws java.io.IOException {
char ch;
int changes = 0;

System.out.println("Enter period to stop.");

do {
  ch = (char) System.in.read();
  if(ch >= 'a' & ch <= 'z') {
    ch -= 32;
    changes++;
    System.out.println(ch);
  }
  else if(ch >= 'A' & ch <= 'Z') {
    ch += 32;
    changes++;
    System.out.println(ch);
  }
} while(ch != '.');
System.out.println("Case changes: " + changes);   } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an infinite loop?

A

An infinite loop is a loop that runs indefinitely

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

When using break with a label, must the label be on a block that contains the break?

A

Yes.

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