The syntax for a for loop is:
for (initialization; condition; increase) {
statement;
}
for example:
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
cout << arr[i] << endl;
// you can do alot more in a for loop than just print to console
}
while loop syntax
while (condition) {
// statements
}
example:
int i = 0;
// the loop will end when i = 5
// unlike for loop you must increment variables yourself or
// use the break command to exit the loop
while (i != 5) {
cout << i << endl;
i++;
}
Example in Java: int total = 0; int i = 1 while (i <= 10) { total += i; i++; } System.out.println("The sum of the first ten numbers is " + total);
The idea is to use a loop. To reduce the additional effort (and innacuracy) of power calculations, you can do repeated multiplication, as part of the loop. For example, in Java:double sum = 1;double xpower = 1.0;for (int i = 1; i
Plus, country code, area code, number For example: +1 555 1234567
Although both can achieve very similar loops, the one you use for a specific loop usually comes down to whichever seems more intuitive to you. In terms of performance, one method may be slightly more efficient than the other, however there are no general rules that can be applied; you must test each case by comparing the machine code or by physically timing the loops with a high performance event timer (HPET). In my experience, the performance difference is so negligible it is not worth the effort. Background activity has a far greater impact on performance.Describing the differences between every possible loop you might create would take me a lifetime, but there are a couple of subtle differences that are worth pointing out.Infinite Loopsfor(;;){} // infinite for loops do not require a condition.while( true ){} // infinite while loops MUST have a condition.To break out of an infinite loop you must test for one or more conditions within the loop itself, and call break to exit the loop when a condition is met. You can also call continue to start the next iteration before reaching the end of the current iteration.Stepping LoopsWhen stepping through a series of values, the for() loop is usually the better choice, especially if the step must occur AFTER each iteration. Consider the following similar loops:for( int x=0; x
Indenting is a good habit to inculcate while writing any type of code Although indentation is not mandatory and will never affect the working of your programme in C++ it makes the code more easy to read and debug especially for larger programmes. Most IDE's eg eclipse automatically indent the code as you type.
kk
There is no actual difference; a for loop is just syntactic sugar for a while loop. Which you use depends largely upon which makes the most sense within the context of your source code. The for loop is clearly more flexible, but you will generally use a for loop whenever the number of iterations is known in advance, such as when counting iterations, whereas while loops are generally used whenever the number of iterations is unknown or infinite. Regardless, this has no effect on the efficiency of your code (the machine code maps almost directly, 1-to-1, with a while loop), it's just a question of which makes your code easier to read. One useful property of a for loop is that you can declare and initialise a control variable in the initial expression. This renders the control variable local to the loop, which is something you cannot achieve with a while loop. This has no effect on the resultant machine code, but by scoping variables within a for loop you automatically enlist the help of the compiler to eliminate bugs. It should be noted that the do-while loop is similar to a while loop, except that a do-while loop always executes its statements at least once, because the conditional expression is evaluated at the end of each iteration, rather than before each iteration as it is with a while loop. Again, a for loop can be used to achieve a do-while loop, however the do-while loop maps closely with the resultant machine code, and is generally much easier to read.
No, why did you think so?
Iterative loops in C/C++ are represented by for(), while() and do...while() code blocks. Recursive loops are represented by functions calling themselves.
Example: int main (void) { LOOP: goto LOOP; }
Procedural code in C++ makes use of the goto keyword rather than using structured code such as for/while loops and function calls. Although procedural code is permitted in C++, its usage should be kept to a minimum and should only used when there are no better alternatives available. In other words, it must have purpose and add value to the code. Prevalent use of goto results in "spaghetti code" that is difficult to both read and maintain. By way of an example, the following while loop can be written procedurally: // Structured loop: int x=0; while(x++<10) std::cout<<x<<std::endl; // Procedural loop: int x=0; again: if(x++<10) { std::cout<<x<<std::endl; goto again; } Clearly the structured while loop is easier to read. Although the procedural loop works just as well, it is hard to justify its usage in this case as it adds no value and readers will simply question why it was used -- thus hindering the readability of the code.
Example in Java: int total = 0; int i = 1 while (i <= 10) { total += i; i++; } System.out.println("The sum of the first ten numbers is " + total);
A do-while loop is best suited to loops that must execute at least once: srand(( unsigned)time(NULL)); int i; do { // pick a random number between 1 and 10 i = 1 + (int) (10.0*(rand()/(RAND_MAX+1.0))); std::cout << i << std::endl; } while( i!=10 ); In this example, the loop executes at least once and prints a random number. While the random number is less than 10, the loop reiterates, selecting another random number until the number 10 is selected. The exact same effect can be achieved with for and while loops but the code is much less readable as a result: srand(( unsigned)time(NULL)); int i=0; while( i!=10 ) { // pick a random number between 1 and 10 i = 1 + (int) (10.0*(rand()/(RAND_MAX+1.0))); std::cout << i << std::endl; } In the above example, we must initialise i before entering the loop for the first time. srand(( unsigned)time(NULL)); for(;;) { // pick a random number between 1 and 10 int i = 1 + (int) (10.0*(rand()/(RAND_MAX+1.0))); std::cout << i << std::endl; if( i==10 ) break; } In the above example we use an infinite loop and instantiate i within the loop. We can also replace for(;;) with while(1) to achieve the same result. Ultimately the do-while loop makes the most sense in this case. The infinite loop versions would only make sense if you wanted i to fall from scope at the end of the loop (which can't be done with a do-while loop since the conditional expression is outside of the loop). But the while loop makes the least sense because i must be initialised before entering the loop for the first time.
The do while loop is useful for the special loops that they must circulate at least one time. #include <iostream> using namespace std; int main() { int num,digit; cout<<"Enter a #\n"; cin>>num; cout<<"Invers is : "; do { digit=num/10; cout<<digit; num/=10; } while(num != 0); return 0; }
Add the missing parts.
An infinite loop is one sequence of commands that just repeats over and over again forever. When it comes to creating an infinite loop you can use the: for do while and do statements. using the keywords 'true'
The following example demonstrates all 4 loop structures in C++. #include<iostream> int main() { int i; std::cout<<"For loop...\n"<<std::endl; for(i=0; i<10; ++i) std::cout<<i; std::cout<<'\n'<<std::endl; std::cout<<"While loop...\n"<<std::endl; i=0; while(i<10) std::cout<<i++; std::cout<<'\n'<<std::endl; std::cout<<"Do-while loop...\n"<<std::endl; i=0; do { std::cout<<i; }while( ++i<10 ); std::cout<<'\n'<<std::endl; std::cout<<"Goto loop...\n"<<std::endl; i=0; again: std::cout<<i; if(++i<10) goto again; std::cout<<'\n'<<std::endl; } Output: For loop... 0123456789 While loop... 0123456789 Do-while loop... 0123456789 Goto loop... 0123456789