For循环语句的使用

一、For循环语句

说明:For循环用于循环次数已经确定的情况下。 

格式:for(循环变量赋初值; 循环条件; 循环变量增值)

      {

            ·····语句

     }

举例:求1到100相加的和

代码:

#include <vcl.h>
#include <iostream>
#pragma hdrstop
#include <tchar.h>
//---------------------------------------------------------------------------

#pragma argsused

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	int i; //自增变量
	int sum=0; //总和变量
	for (i = 0; i <= 100; i++)
	{
		sum = sum+i; 

	}
	cout << "从1加到100的和为" << sum << endl;
	cin.get();
	return 0;
}

 图解:

                    

原文地址:https://www.cnblogs.com/hkleak/p/4949509.html