blocks

Started by
9 comments, last by Driv3MeFar 17 years, 8 months ago
Can someone pleas explain why both of this is valid? its an exercise in a book and it asks me to explain why or why not this is valid.. and I've compiled both of them with no errors..

#include <iostream>
#include <string>

int main()
{
 { const std::string s = "a string";
   std::cout << s << std::endl; 
 { const std::string s = "another string";
   std::cout << s << std::endl; }}
 return 0;
}



#include <iostream>
#include <string>

int main()
{
 { const std::string s = "a string";
   std::cout << s << std::endl; 
 { const std::string s = "another string";
   std::cout << s << std::endl; };}
 return 0;
}


Advertisement
hmm.. whats thoes code brackets for the forum again? :)
<br>[ / s o u r c e ]<br><br>without the spaces.
gives you:

void foo(){}


and [ source] [ /source] gives you:

void foo(){}


Dave
Gracias! :) can anyone explain it?
The {} indicate scope. I can't see anything wrong with. If there is something, it will most be a real icky technicality.
hmm.. nope theres nothing wrong.. it compiles both.. but I dont get why? the first one is ok.. I thoght maybe the ; thats added to the last one would screw things up..? still confused..
Brackets {} indicate a new scope, so when you do this:
int main(){ { //this bracket is pretty useless    const std::string s = "a string";   std::cout << s << std::endl;  //should print "a string"   { //this creates a new scope      const std::string s = "another string"; //s already exists, but in a different scope, so this is legal      std::cout << s << std::endl; //should print "another string"   } } return 0;}


The second example just adds an additional semi-colon. You can really have as many of these as you want, if you have a line with nothing but a semi-colon on it thats just a NOP, so while it may be bad style, its perfectly legal.

int main(){ {    const std::string s = "a string";   std::cout << s << std::endl;    {     const std::string s = "another string";     std::cout << s << std::endl;    }   ; //this is a NOP, it won't cause any harm, but it doesn't do any good either } return 0;}



From your original question, it sounds like the book was asking you if either were legal. Were you expecting one to be invalid?
As stated, brackets indicate scope. As for the semicolon, they basically represent the end of a statement. Outside of preprocessor directives (basically anything that starts with #), you can have almost an infiniite amount of ;'s, and the compiler won't care.

IE:

#include <iostream>#include <string>int main(){	{		const std::string s = "a string";		std::cout << s << std::endl; 		{			const std::string s = "another string";			std::cout << s << std::endl;			{{{std::cout << "I work also" << std::endl;;;;;;;;;;;;;;;;}}}		}		;;;;;;;;;;;;;;	}	return 0;}


And a tip for you who want to help by showing people how to type [source][/source] instead of (without spaces),<br><br>Use the following:<br><br>&#38;#91;source&#38;#93;&#38;#91;/source&#38;#93;<br><br>This way they can copy and paste it. Just somewhat helpful, and requires less of an explanation for people like me who might not understand everything.
ok.. I messed around abit with the code and I get it now.. didnt realise that it was legal.. What does NOP stands for?

the book asked if any of them where legal and I thought maybe the one with the extra ; was not.. but now I know! thanks alot!! :)

This topic is closed to new replies.

Advertisement