编写一个从startNumber到lastNumber打印的for循环

时间:2018-10-25 00:15:16

标签: c++ for-loop

我不知道如何执行以-5的开始编号和6的结束编号的for循环。

int startNumber
int endNumber
int I 

startNumber = -5
endNumber = 6;

for( i = -5; i > endNumber; i++)
 cout << i << /n 

1 个答案:

答案 0 :(得分:0)

您在这里遗漏了几件事(假设这是您的完整代码,而不仅仅是代码段

#include <iostream> // allows you to use cout functions etc

int main() // function for your program to run
{
    int startNumber = -5; // initialise variables
    int endNumber = 6;

    // this loops from startNumber to endNumber going up by 1 each loop
    for(int i = startNumber; i <= endNumber; i++)
        std::cout << i << std::endl //cout prints the value i

    return 0;
}