PQ01. For what type of tasks are \"while\", \"for\", and \"do-while\" loops best
ID: 3582946 • Letter: P
Question
PQ01. For what type of tasks are "while", "for", and "do-while"
loops best suited? I.e., why is do-while good for checking
values entered by the user ("input validation").
PQ02. How does a flag-controlled loop work? Sentinel-controlled
loop? Counting loop?
PQ03. Practice *tracing* a couple "for" loops. Pay attention to
the order in which the parts of a "for" are executed.
PQ04. How do the "break" and "continue" statements alter loops?
When can "continue" cause problems?
PQ05. Trace sample code in which we *nested* a loop inside another.
In which we used an "if" inside a loop.
PQ06. What is the difference between a primitive and reference type?
List some examples. What are the *default values* for each?
PQ07. How do you use a method of *wrapper class* Character to
test whether a char is a letter?
PQ08. Use String methods indexOf() and substring() to extract
the first name from a string formatted as: "Last,First".
PQ09. Differentiate between the *contents* of a file read using
a counter vs. sentinel.
PQ10. How are classes Scanner, File, and PrintWriter used to read
from/write to a file.
Explanation / Answer
1) while, do, and for loops are used to repeat the execution of one or more statements a certain number of times.
While statement tests the boolean expression for truth before entering in the loop's body. On the contrary, do loop tests boolean expression for truth when exiting from the loop. If it returns false then control does not execute loop's body again else it will do. hence it is good to check value entered by the user.
2) flag controlled loop works as counter controlled loop. it works as a bool variable being defined and initialized to serve as a flag. The while loop continues until the flag variable value gives toggled value.
4) When a break statement appears, it terminates the above function and gets the control out of the switch or loop causing the innermost enclosing loop or switch to be exited immediately.
When a continue statement appears, it gets the control to the next iteration of the loop causing the loop nested within a switch to execute the next loop iteration.
continue can cause problem when the loop increases and it forms infinite loop.
5) class heap
{
public static void main(String args[])
{
for(int i = 1; i <= 25; i++)
{
if(i * i == 36) break; // terminate loop if i is 6
System.out.print(i + " ");
}
System.out.println(); // blank lin
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 5; j++)
{
if(j > i) break; //nested loop
System.out.print(j + " ");
}
System.out.println();
}
}
}
6) primitives are data types that stores actual value eg. long, float,short, double, integers etc. while references are pointers, which do not hold their values but point to their values and are used on/with objects i.e. it points the object e.g. string, thread, file etc. another difference is When you assign a value to primitive data types, the primitive value is copied, but when you assign an object to reference type, the handle is copied.
default value of premitive data types is 0 and reference is null.
9) in sentinel, unknown number of execution occurs whereas in counter previously known number of execution occurs. in sentinel, the limitation for the condition variable is strict but the value of the variable varies whereas in counter both are strict.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.