While loop in C + + and for Loop statement Learning Tutorials _c language

Source: Internet
Author: User

C + + while loop

The general form of the while statement is as follows:

  while (expression) statement

The action is to execute the inline statement in the while statement when the specified condition is true (the expression is not 0). The diagram below is shown in the flowchart.

It is characterized by: first, the expression, followed by the execution of the statement. A while loop is called a type loop.

Example: Seek 1+2+3+...+100.

#include <iostream>
using namespace std;
int main ()
{
  int i=1,sum=0;
  while (i<=100)
  {
   sum=sum+i;
   i++;
  }
  cout<< "sum=" <<sum<<endl;
}

Run result is

sum=5050

Need to note:
The loop body, if it contains more than one statement, should be enclosed in curly braces to form a compound statement. If no curly braces are in place, the while statement ranges only to the first semicolon after the while.
There should be statements in the loop body that make loops tend to end.

C + + for Loop statement
for statements in C + + are most widely and flexibly used, not only in cases where the number of loops has been determined, but also in cases where the number of loops is indeterminate and only the end condition of the loop is given, which can be completely substituted for the while statement.

The general format for the for statement is:

  for (expression 1; expression 2; expression 3) statement

The For statement execution process is as follows:
Solve the expression 1 first.
Solution Expression 2, if the value is true (the value is not 0), executes the inline statement specified in the FOR statement, and then executes the following step (3). If False (a value of 0), end the loop and go to step (5).
Solution Expression 3.
Revert to the above step (2) to continue.
Loop ends, executes a statement below the for statement.

You can use the following figure to represent the execution of a for statement.

The simplest form of the For statement is also the easiest to understand in the following format:

  For (cyclic variable assignment initial value; cyclic condition; cyclic variable increment) statement

For example:

  for (i=1;i<=100;i++) sum=sum+i;

It corresponds to the following statement:

I=1;
while (i<=100)
{
  sum=sum+i;
  i++;
 }

Obviously, the for statement is simple and convenient.

The use of a for statement has many tricks, and if you master and apply a For statement skillfully, you can refine the program.

A few notes about the For statement:
The "Expression 1" in the general format of the for statement can be omitted, and the loop variable should be assigned an initial value before the for statement.
If the expression 2 omitted, that is, do not judge the cycle conditions, the cycle is not terminated. That is, the expression 2 is always true.
Expression 3 can also be omitted, but at this point the program designer should also try to ensure that the loop ends normally.
You can omit expression 1 and expression 3, and only the expression 2, which gives only the loop condition.
All 3 expressions can be omitted.
Expression 1 can be an assignment expression that sets the initial value of a loop variable, or it can be another expression unrelated to a loop variable.
Expressions are generally relational expressions (such as i<=100) or logical expressions (such as A<b && x<y), but can also be numeric or character expressions that execute the loop body as long as the value is Non-zero.

A For statement in C + + is more powerful than a circular statement in other languages. The loop body and some operations unrelated to the loop control can also appear as expression 1 or expression 3, so that the program can be short and concise. But using this feature too much makes a for statement messy and less readable, and it is recommended that you do not put loop-control-independent content into a for statement.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.