对于C ++中的循环增加变量的区别

时间:2014-11-13 09:37:42

标签: c++ loops

以下两个for循环有什么区别?

for(int i = 0; i < 5; i++)
{

}

for(int i = 0; i < 5;)
{
//End of whatever code to do.
 i++;
}

根据http://www.cplusplus.com/doc/tutorial/control/,不应该有区别。 然而,当我运行我的代码(下面的代码)时,取决于iter ++的位置,存在差异。

在这个程序中,我有一个单独的线程运行来获取输入。当我将iter ++移到底层时会发生什么,当一个单独的客户端连接到服务器时,我必须在响应之前在cin流中输入一些内容。

当iter ++位于for循环中的顶部时,不会发生此问题。

我希望我的iter ++位于底部的原因是,当我收到断开连接时,我可以删除地图中的会话。

    for (iter = network->sessions.begin(); iter != network->sessions.end(); iter++)
{
    //bool Deleted = false;

    int data_length = network->receiveData(iter->first, network_data);

    if (data_length < 0) 
    {
        //no data recieved
        continue;
    }
    if (data_length == 0)
    {
        printf("Data closed GRACEFULLY LOL \n");
        continue;
    }

    int i = 0;
    while (i < (unsigned int)data_length) 
    {
        packet.deserialize(&(network_data[i]));
        i += sizeof(Packet);

        switch (packet.packet_type) {

            case INIT_CONNECTION:

                printf("server received init packet from client\n");

                char Buffer[100];
                //Buffer to hold char values of client id

                _itoa_s(client_id - 1, Buffer, 10);
                sendActionPackets(client_id - 1, Buffer);

                break;

            case ACTION_EVENT:

                printf("server received action event packet from client\n");

                break;


            case TALK:
                ProcessTalkLine(packet.Message, sizeof(packet.Message), iter->first);
                //sendTalkPackets(packet.Message,sizeof(packet.Message), iter->first);

                break;

            case DISCONNECTING:
                printf("I HAVE RECEIVED DC CONNECT /n");
                char theMessage[MAX_MESSAGE_SIZE];
                sprintf_s(theMessage, "%s has disconnected.", Usernames.find(iter->first)->second.c_str());
                Usernames.erase(iter->first);
                //network->sessions.erase(iter++);

                break;
            default:

                printf("error in packet types\n");

                break;
        }
    }
}
编辑:感谢@Matt McNabb指出继续将......继续。我也在那里放入了我的iter ++,但是在我输入内容之前它不会收到消息的问题依然存在。如果我把iter ++留在for循环中,那么这个问题就不存在了。

4 个答案:

答案 0 :(得分:6)

当你continue执行for中的第三个语句时。在第一种情况下,这会增加i,而在第二种情况下,它不会增加continue

如果您不使用goto(或{{1}}),则循环仅相同。

答案 1 :(得分:2)

这两个不一样:

for(int i = 0; i < 5; i++)
{
    if (some_condition)
        continue;
}

for(int i = 0; i < 5;)
{
    if (some_condition)
        continue;

    //End of whatever code to do.
    i++;
}

答案 2 :(得分:1)

for(int i = 0; i < 5; i++)
{
    if (condition)
        continue;
    //Your Code
}

在上面的for循环中,在条件为真的情况下,循环将继续而不遍历下面的行。但是i值肯定会增加。

for(int i = 0; i < 5;)
{
    if (condition)
        continue;

    //Your Code
    i++;
}

在第二个for循环中,行为与上一个相同,但在继续时,i的值不会递增。

在你的情况下,如果你确定要将itr ++放在底部,那么写下如下,

for(int i = 0; i < 5;)
{
    if (condition)
        goto incrementPoint; //Use goto instead of continue.

    //Your Code
    incrementPoint: i++;
}

答案 3 :(得分:0)

要真正看到差异,请考虑编译器通常为for循环生成的代码:for(i= initial_value; i<max value; i=i+increment)

注意:这是伪代码并忽略所有编译器优化

**stat_for_loop**:
      execute instructions
      i=i+increment
      if i<max_value
         goto **stat_for_loop**

在for循环中添加continue语句时,通常如下所示:

**stat_for_loop**:
      execute instructions
      if(cond) 
         goto **next_iteration**
      execute instructions   
**next_iteration**:
      i=i+increment

      if i<max_value
           goto **stat_for_loop**

你可以清楚地看到,如果忽略for循环中的迭代器增量并且你决定在for块中手动递增它(就像你在while循环中那样),这取决于你何时添加一个continue语句,生成的代码将不同,因此执行路径将是不同的

相关问题