+ 1
help understanding loops
Could you please help me with these questions? what is a condition? what is an initialized variable? do we have to assign a value in the initialized variable or could we have user-input? why do we assign the value of 0? What is the difference between when we assign a 0 or a 1? how many unassigned values (if any) can we have in a loop? differences between for, while, and do..while loops? when would you want to use a…. a. for loop? b. while loop? c. do…while loop? are the for and while loop the same thing in different formats?
2 Antworten
+ 1
What is a condition?
A condition is an expression that can be evaluated to either true or false.
What is an initialized variable?
It is a variable that you not only declare but you also give it an initial value, say 0 as in int num = 0; or as in String name = "";
Do we have to assign a value in the initialized variable or could we have user-input?
You can use user input.
Why do we assign the value of 0?
Is an starting point in the total number of iterations or repetitions that the loop will go through. Zero is usually useful when going through structures that use zero based indexing, that is the first element index is 0; example is an array structure.
What is the difference between when we assign a 0 or a 1?
Lets say you want to loop 10 times if you want to use it in a zero based indexed structure you can start at zero and finish at 9 or i < 10. If you want just to count stuff that does not use zero indexing then start at 1 and end at 10, example for ( int i=1; i <= 10; i++){}
How many unassigned values (if any) can we have in a loop?
Not sure what you mean.
Differences between for, while, and do..while loops?
+For loops are better if you know how many repetitions you need.
+While loops for when you do not now how many repetitions
+Do while loops are for when you need at least 1 action to happen before evaluating the following ones if they pass the test.
when would you want to use a….
a. for loop? see last questio
b. while loop?
c. do…while loop?
Are the for and while loop the same thing in different formats?
In some cases they can achieve the same in different formats.
+ 1
Thank you Israel, I really appreciate your thorough explanation. I am getting clarity about this