Would you please give me your best examples of what the {Curley braces} are use for? and when to use them and not to use them.
I am trying to get a GOOD understanding about them.
It do not need to be a (long example) just your example.
{ means begin a block of code; some languages actually use things like keywords begin and end.
if(somthing == true)
dostuff();
otherstuff(); // this is NOT IN THE IF STATEMENT, IT ALWAYS HAPPENS
if(somthing == true)
{
dostuff();
otherstuff(); // this is also inside the if statement block.
}
most of the time, code blocks define blocks of code like this; the begin and end of a function, the begin and end of a user defined type, the begin and end of a conditional or loop or similar block, etc.
On occasion, someone will use them just to scope variables.
int x;
x = 3;
code
{
int x; //this isnt the same x as above.
x = 5; //different x!
}
//x is 3 here. the other x was destroyed when its block went away.
this is a terrible example; there are legitimate times to scope a variable this way to ensure a destructor is called or to otherwise explicitly control something, but I am not thinking of a useful example of that right now. Youll know it when you see it.
{} are also heavily used to initialize variables, either user defined types or built in containers etc.
type something {value, value, value}; //crude syntax example.
Curly braces have to be used with class, enum, and function definition blocks.
In addition to executing multiple things within a block, and scope behavior (a variable declared in a scope only exists in that scope):
This might be a bit advanced, but curly-brace initialization syntax also prevents the "Most vexing parse" syntax ambiguity resolution in C++. https://en.wikipedia.org/wiki/Most_vexing_parse
from the wiki page:
1 2 3
TimeKeeper time_keeper(Timer());
vs.
TimeKeeper time_keeper{Timer{}};