Oh well. C++ to me is either really great once you can get to understanding it or a pain with all of its ultra precison in complicated synthax. I.E
cout <<
cin >>
I learned the signals by heart imagining a racecars going outside a tunnel or inside with those two. Not hard but when you add..
pow(x,y) where x and y can be heavilly modified and can have extra parenthesis.
Nesting If's has also been troublesome because I was given the logic to 'connect the if's like you would blocks' instead of stuffing them in together.
Previous believed this:
Block of if 1
--------------------separated but connected.
Block of if 2.
Instead of:
Block 1 (
Block 2()
)
I'm trying to think of a good way to explain C syntax, but you're right, it gets tricky. Statements can be blocks of statements, expressions can contain sub-expressions, and so forth, even before getting into operator overloading tricks, class definitions, templates, and macros.
But to start with the basics, your function body is a list of statements, which the computer executes one by one. Each statement can be a variable declaration, an expression, an if statement, a while statement, a block of statements, or a few other things that could be expressed in those terms. (Okay, so variable declarations are a bit wiggly as far as when they get executed. Ignore that for now.) For example:
- int a = 3;
- f(a);
- if (a > 4) g();
- while (a < 10) a = h(a);
- { f(a); g(); }
That last item, the block surrounded by braces, acts much like the function body itself; it contains a list of statements, executed in order, one by one. Notably, the statements in a block can be any of the above statement types, even other blocks.
Expressions consist of operators applied to names and/or other expressions, much like mathematical notation. Granted, C has special meanings for certain operators, sometimes in particular places; for example, a single equal sign (
=) indicates assignment, not comparison. From your example, "
cout << f" applies the << operator to cout and f, which the cout object takes to mean that it should print f to the screen.
The
if statement is interesting, because it requires an expression and one or two other statements:
if (expression) true_statement else false_statement
When the computer gets to this statement, it evaluates
expression. If (and only if) the result is true (non-zero), then
true_statement is executed. Otherwise,
false_statement is executed. (The "else false_statement" part can be omitted, in which case nothing is executed if the expression is false.) In either case, the computer goes on to execute the next statement in the surrounding block.
Frequently, the expressions for the if statement are blocks. Also common is a second if statement as the false_statement.
if (a > 10) {
if (b > a) f();
if (b < 15) g();
} else if (a > 5) {
h();
} else {
q();
if (b < a) {
r();
}
}
if (b > 10) {
s();
}
t();
This snippet has examples of if statements nested inside each other, and examples of if statements after each other. Which functions will be called for various values of
a and
b? As a particularly interesting example, what will be called for
a = 14, b = 12?
But perhaps this will all be easier to understand once you learn about recursion...
And yes, C++ is either really great or a massive pain. When I want the level of control it requires me to exert, I prefer straight C. When I want the power of objects and classes, I prefer Python. There are good reasons to use C++, but if it's not obvious, something else is probably better.