Revision: 598
http://assorted.svn.sourceforge.net/assorted/?rev=598&view=rev
Author: yangzhang
Date: 2008-03-03 20:11:53 -0800 (Mon, 03 Mar 2008)
Log Message:
-----------
added scoping demo
Added Paths:
-----------
sandbox/trunk/src/cc/scopes.cc
Added: sandbox/trunk/src/cc/scopes.cc
===================================================================
--- sandbox/trunk/src/cc/scopes.cc (rev 0)
+++ sandbox/trunk/src/cc/scopes.cc 2008-03-04 04:11:53 UTC (rev 598)
@@ -0,0 +1,24 @@
+// Demonstrate the scoping of control structures. Before, I thought that only
+// for loops had this kind of scoping.
+
+#include <iostream>
+using namespace std;
+
+int
+main()
+{
+ if (int x = 1) {
+ cout << "true " << x << endl; // This is printed, as expected.
+ } else {
+ cout << "false " << x << endl;
+ }
+ // The following doesn't work; x is out of scope.
+ // cout << x << endl;
+
+ // Here, the initialization to 1 occurs on each iteration.
+ while (int x = 1) {
+ cout << x-- << endl;
+ }
+
+ return 0;
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|