这两个程序有什么不同
What is the difference between these two programs?
这两个程序之间有什么区别?第一个程序的输出为9,第二个程序的输入为10。
#include <iostream>
using namespace std; // So we can see cout and endl
int main()
{
int x = 0; // Don't forget to declare variables
while ( x < 10 ) { // While x is less than 10
cout << x << endl;
x++; // Update x so the condition can be met eventually
}
cin.get();
}
#include <iostream>
using namespace std; // So we can see cout and endl
int main()
{
int x = 0; // Don't forget to declare variables
while ( x < 10 ) { // While x is less than 10
x++; // Update x so the condition can be met eventually
cout << x << endl;
}
cin.get();
}
在第一个代码块中,您将输出x,然后将其相加,这样它将输出0-9。在第二个代码块中,在输出之前将1添加到x,这样它将为您提供1-10的输出。它基于x++
相对于输出语句
第一个的输出为0 1 2 3 4 5 6 7 8 9。第二个输出是1 2 3 4 5 6 7 8 9 10。
在第一个例子中,您写出数字,然后增加变量,而在第二个例子中您首先增加值。
这显然是由于订单的原因。在第一个while循环中,当x
为9时,它会打印它,增加它,不通过条件,也不会再次进入循环。
在第二种情况下,当x
为9时,它将其增加到10,然后打印它,然后离开循环。这只是常识。因此,第二个循环跳过数字0,并打印到10。
第一个循环:0,1,2,3,4,5,6,7,8,9
第二个循环:1,2,3,4,5,6,7,8,9,10
第一个片段打印变量的值,然后将其递增。这意味着不会打印上次递增后的变量值。
第二个代码段递增变量,然后打印它。这意味着它不会打印0。它被初始化为.
相关文章:
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 正在解码MSVC 32位版本的程序集(作业).没有手术做什么
- 是否总是可以将使用递归编写的程序重写为不使用递归的程序C++,性能观点是什么?
- C++std::atomic在程序员级别保证了什么
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- 什么是"undetectable means",它们如何更改 C/C++ 程序的对象?
- C++程序什么都不做,但瓦尔格林德显示内存分配
- 使用动态链接加载程序 <dlfcn.h> 而不是直接函数调用的目的是什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我的堆栈和库存清单程序的结构有什么问题?
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 没有信号处理程序的POSIX定时器的目的是什么?
- 谁能告诉我,程序中的错误是什么?该程序仅用于获取文件扩展名
- 指针和程序性能之间有什么关系吗?
- 我的C++级数计算程序出了什么问题?被困了好几个星期
- 是什么原因导致我的SDL2程序中出现杂散像素
- 我的C++程序的工作目录是什么
- 程序什么时候会创建多个堆
- 我可以编写什么程序来冻结/挂起我的程序来测试看门狗计时器
- Windows如何知道用什么程序打开文件?