If you mean you cannot use a for loop, then use a while loop:
int i=0
while( i++ < 100 )
std::cout << i << " ";
std::cout << std::endl;
Or a do-while loop:
int i=0;
do
std::cout << ++i << " ";
while( i<100 );
std::cout << std::endl;
If these are not allowed either, use a procedural loop:
int i=0;
again:
std::cout << ++i << " ";
if( i<100 ) goto again;
std::cout << std::endl;
If even that is not allowed, then the only option is to hard-wire:
std::cout << 1 << " " << 2 << " " << [etc] << 99 << " " << 100 << std::endl;
It does seem a pointless exercise when a for loop exists specifically for counting iterations like this:
for( int i=1; i<=100; ++i )
std::cout << i << " ";
std::cout << std::endl;
how to print "square" using for loop
The best way to print the numbers 1 to 100 in PHP without using a loop is with the following code: echo implode("<br>", range(1,100)); You can replace the <br> with anything that you want to separate the numbers, such as dashes. I used a line-break in the example.
If you are using for loop for(;;); or you can also define condition and iterations but the loop has to close there itself without any statement inside it. In the similar way you can define while and do while loop without any statement.
while- It will wrok only if the condition is correct if not it wont excute the body of the loop do-while: In some cases we want that body of the loop and the condition may fails so the loop want to print so the do while loop first excute body of the loop and next it checks the condition it continues until the condition becomes false
int youArray[arraysize] = {...};...for (int i = 1; i
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x=fruits.valueOf(); alert(x);
int main (void) { puts ("unique"); }
The do..while() loop tests the condition at the end of the loop. Therefore the loop body executes at least once. The while() loop (without do) tests the condition before entering the loop and before each iteration of the loop. The for() loop conditional expression is optional but, when specified, is tested before entering the loop and before each iteration of the loop.
All loops available in Java (for, while, do-while) have a loop termination condition that would get executed during every iteration of the loop. Without checking the loop condition the loop cannot be terminated and hence avoiding the loop condition check during iteration is not logic and Java does not do it.
It may not be worthwhile to write a loop at all in such a simple case. On the other hand, you can make two loops, one for the ascending part, and one for the descending part. Finally, you can use a single loop, and a condition inside it: use a counter "i" = 1 to 5; if i < 4 print i, otherwise print 6 - i. This latter would be useful if, instead of simply printing the values indicated, you needed several commands within the loop; in this case, having a second loop is simply not practical.
foreach can simply replace for loop. for ex in perl, if you need to copy display an array, it will take only foreach $var(@arr). then print $var. no need of incrementing the index of array like for loop.
Do statement : Here the do loop exicutes once without cheching the condition and then the condition will be checked if condition becomes true then again loop begins from do and then exictes here if you are given value is n then loop exictues n+1 times.for statement : Here the for loop will be initilized first and the condition will be checked if the condition then the pointer enters the loop else not.And after completion of the loop the pointer again moves to the for statement and the increment/Decrement will be done as you provided in loop then the condition will be checked if satisfy then it enteres loop else not