answersLogoWhite

0


Best Answer

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++;

}


User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus for while loop example code?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Math & Arithmetic

What is 1 plus 2 plus 3 plus 4 plus 5 plus 6 plus 7 plus 8 plus 9 plus 10 equals 55 how is it possible using while loop?

Example in Java: int total = 0; int i = 1 while (i &lt;= 10) { total += i; i++; } System.out.println("The sum of the first ten numbers is " + total);


Program to find the sum of the series as 1 plus x plus x2 plus x3 plus?

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


How you written your mobile no in international forms?

Plus, country code, area code, number For example: +1 555 1234567


What is different between while and for loop in c plus plus?

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


What is the need for indentation in while writing a c plus plus program?

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.

Related questions

Example of flowchart of while loop in c plus plus?

kk


Explain the difference between for and while loop and give the suitable examples in c plus plus?

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.


Is for loop is faster than while loop in turbo c plus plus compiler?

No, why did you think so?


How does C plus plus endeavor to represent the loop paradigm?

Iterative loops in C/C++ are represented by for(), while() and do...while() code blocks. Recursive loops are represented by functions calling themselves.


What is CPU usage small programm c plus plus?

Example: int main (void) { LOOP: goto LOOP; }


What is a Procedural models in C plus plus?

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++&lt;10) std::cout&lt;&lt;x&lt;&lt;std::endl; // Procedural loop: int x=0; again: if(x++&lt;10) { std::cout&lt;&lt;x&lt;&lt;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.


What is 1 plus 2 plus 3 plus 4 plus 5 plus 6 plus 7 plus 8 plus 9 plus 10 equals 55 how is it possible using while loop?

Example in Java: int total = 0; int i = 1 while (i &lt;= 10) { total += i; i++; } System.out.println("The sum of the first ten numbers is " + total);


What is an example of a do-while loop in C plus plus?

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 &lt;&lt; i &lt;&lt; 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 &lt;&lt; i &lt;&lt; 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 &lt;&lt; i &lt;&lt; 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.


Do while in c plus plus?

The do while loop is useful for the special loops that they must circulate at least one time. #include &lt;iostream&gt; using namespace std; int main() { int num,digit; cout&lt;&lt;"Enter a #\n"; cin&gt;&gt;num; cout&lt;&lt;"Invers is : "; do { digit=num/10; cout&lt;&lt;digit; num/=10; } while(num != 0); return 0; }


How to complete this C plus plus End Of File controlled while loop program?

Add the missing parts.


What is an infinite loop in c plus plus?

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'


What are some examples of loops in C plus plus?

The following example demonstrates all 4 loop structures in C++. #include&lt;iostream&gt; int main() { int i; std::cout&lt;&lt;"For loop...\n"&lt;&lt;std::endl; for(i=0; i&lt;10; ++i) std::cout&lt;&lt;i; std::cout&lt;&lt;'\n'&lt;&lt;std::endl; std::cout&lt;&lt;"While loop...\n"&lt;&lt;std::endl; i=0; while(i&lt;10) std::cout&lt;&lt;i++; std::cout&lt;&lt;'\n'&lt;&lt;std::endl; std::cout&lt;&lt;"Do-while loop...\n"&lt;&lt;std::endl; i=0; do { std::cout&lt;&lt;i; }while( ++i&lt;10 ); std::cout&lt;&lt;'\n'&lt;&lt;std::endl; std::cout&lt;&lt;"Goto loop...\n"&lt;&lt;std::endl; i=0; again: std::cout&lt;&lt;i; if(++i&lt;10) goto again; std::cout&lt;&lt;'\n'&lt;&lt;std::endl; } Output: For loop... 0123456789 While loop... 0123456789 Do-while loop... 0123456789 Goto loop... 0123456789