Convert the following for loop to a while loop forint x 50l
Solution
2) converting the for loop to a while loop.
for(int x=50; x > 0; x--)
{
System.out.println(x+\"second to go.\");
}
Answer:-
Converting for lopp to while loop :-
int x = 50;
while (x > 0)
{
if0 % x == 0)
cout<< x<<\" \";
x--;
}
4) what is the differece between a while loop and do-while loop.
Answer:-
\'for\' loop is generally used when you know the number of iterations beforehand. e.g. to traverse an array of 10 elements you can use for loop and increment the counter from 0 to 9(or 1 to 10 in 1 based indexing).
On the other hand \'while\' is used when you have an idea about the range of values on which to iterate, but don\'t know the exact number of iterations taking place.
Example in a BFS :
while(!queue_q.empty())
{
// remove element(s)
// add element(s)
}
Here we don\'t know exactly how many times the loop will run.
In some specific areas like multiprocessing(e.g. in OpenMP), it is necessary to specify the number of iterations. Hence only \'for\' loop in used.
However many a times both are inter-convertible.
5) what is the difference between a void method and a value returning method.
Answer:-
Void method :
The \"void\" return type means that this method doesn\'t have a return type. It actually doesn\'t need one because you \"print\" your String onto the System\'s output stream. In an application, this approach may be used to print runtime specific messages on the console for example.
public void printString(String str) {
System.out.println(str);
}
Returning method :
public String stringMethod(String str) {
return str;
}
This other method on the other hand, returns a String. This means that you can use the returned value in your code for further processing. I guess good examples of such methods are \"getters\". These methods return field values of an object.

