kwseeker

学编程:找代码,读代码,改代码,写代码;少看书。但少看不是不看,看一本书要限制在一天内。任务是通读书中主要的概念,明白大致是干什么用的,形成一个大概的框架;然后一周内干掉书中全部代码,代码不理解再浏览书中相关章节,注意是浏览。或者刷博客。代码实在刷不懂,可以先刷后面,待日后重新刷;因为书中代码有些部分可能出自后面的章节。代码总是在多次刷过之后,拨开乌云见日月的。。。

导航

C++@语句块

#include <iostream>
using namespace std;

int main()
{
	{
		int x=1;
		cout << x << endl;
		{
			cout << x << endl;
			int x=2;
			cout << x <<endl;
			{
				cout << x <<endl;
				int x=3;
				cout << x <<endl;
			}
			cout << x <<endl;
		}
		cout << x << endl;
	}
	return 0;
}

输出结果 1 1 2 2 3 2 1

#include <iostream>
using namespace std;

int main()
{
	{
		int x1=1;
		cout << x1 << endl;
		{
			cout << x1 << endl;
			int x2=2;
			cout << x2 <<endl;
			{
				cout << x2 <<endl;
				int x3=3;
				cout << x3 <<endl;
			}
			cout << x2 <<endl;
		}
		cout << x1 << endl;
	}
	return 0;
}

输出结果 1 1 2 2 3 2 1 

/*
* 在第一例每个语句块中,虽然变量名都一样,但是int x= ;之后x就变成另外一个变量了
* 且生存周期仅限于当前的语句块中
*/

posted on 2015-05-30 18:34  kwseeker  阅读(571)  评论(0编辑  收藏  举报