Loop Syntax
Loop Syntax
Research and discuss the following:
• Is the following code syntactically correct? How did you determine your answer?
If the following code is not syntactically correct, what is missing? If it is syntactically correct, what is the expected result?
for ( ; ; ) {
System.out.println(“In the body of the loop”);
}Many programmers use variables named i, j, or k to increment a loop counter, as shown below. However, variables can be named anything a programmer wants to name them, as long as they follow Java™’s syntactical rules for variable names. What are the benefits and drawbacks of using variable names such as i to increment or decrement loop counters vs. other variable names, such as counter or myCounter or hour?
for (int i = 1; i <= 24; i++) { // using i as counter
if (i < 12) { // morning hours
System.out.println(i + ” a.m.”);
}else if (i == 12) { // noon is a special case
System.out.println(i + ” p.m.”);
}else // afternoon hours {
System.out.println(i-12 + ” p.m.”);
}}Supporting Activity: Loops
Our focus this week is loops.
Select and complete one of the following activities:
Convert the following program from for loop to while loop.
class Arithmetic Progression
{ public static void main (String [] args)
{ int sum = 0;
while (int I != -1)
{ sum = sum + i;
System.out.println(Integer.toString(sum));
} }}Supporting Activity: Conditions
Consider the following conditions
CONDITION 1: while ( !(cChoice == ‘Q’ || cChoice == ‘q’) )
CONDITION 2: while ( cChoice != ‘Q’ || cChoice != ‘q’ )
Do Conditions 1 and 2 give the same result? What tool or technique would you use to explain the result?
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.