Sign in to follow this  
Nanook

blocks

Recommended Posts

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;
}


Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 [ s o u r c e ] (without spaces),

Use the following:

&#91;source&#93;&#91;/source&#93;

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.

Share this post


Link to post
Share on other sites
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!! :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this