7.2 for循环(while,continue,break,do..while,switch)

Posted: March 25, 2011 in C++

/*介绍for循环之前, 先简要介绍其他几种简要循环:

while循环:
while(condition)statement;
continue用于跳转到循环的开头; break用于跳出循环继续运行.

do..while循环:
do statement while(condition); 循环在测试条件前执行循环体,从而确保循环至少被执行一次

switch循环;
swich(expression)
{
             case value1: statement; break;
             case value2: statement; break;
             case value3: statement; break;
             default: statement;
}

.
for循环:
for (initialization;test;action) statement;
*/

//这里着重介绍 for 循环的3个使用技巧

#include <iostream>

using namespace std;

void main()
{
             for(int i=0,j=0;i<3;i++,j++) //多个变量进行初始化和递增
                          cout<<i<<“\t”<<j<<endl;
             cout<<endl;

             int a=4;
             for(;a>0;)//空语句(缺少条件会使循环难以理解)
             {
                          a–;
                          cout<<a<<endl;
             }
             cout<<endl;

             for(int b=0;b<5;cout<<“b:”<<b++<<endl);//空for循环(不需要循环体的循环,但不易理解)
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s