public static void main(String[] args) { int num = 50, total = 0; while (num <= 100) { total += num; num++; } System.out.println("total=" + total); int val = 0; for (int i = 50; i <= 100; i++) { val += i; } System.out.println("val=" + val); }
java里这个for和while的结果是相同的,但是在C++里,结果就不一样了!
#include <iostream> using namespace std; int main() { cout << "this is demo" << endl; int num = 50, total; while (num <= 100) { total += num; ++num; } cout << "50到100相加的和是:" << total << endl; int val; for (int i = 50; i <= 100; ++i) { val += i; } cout << "again..." << val << endl; return 0; }C++用的是eclipse的CDT,MinGW写的,这是为什么呢???