Learning a programming language is important if you want to be a coder but no matter how many programming languages you know if you do not get the logic of it it'll always be difficult to solve problems.
You can rely on standard libraries as most programming languages have tons of available methods for sorting, searching, dealing with lists of items, network use, hardware drivers and even numerical methods. All available at your fingerprints on most IDEs like Borland's C++ Builder and Visual Studio. But, if you do not understand what's going on behind the icons you'll never be able to calculate the costs of it and you'll probably end up with an extremely inefficient code and sometimes wrong code that randomly fails.
code.
At the end it's all about data and on the machine all data is stored on the binary format. It is that way part because it's simpler and cheaper and part because Turing's machine is based on binary data and is proven to solve most types of problems.
This format is also called base and that's because binary numbers have a base of two numbers, 0 and 1. There are other bases commonly used in computers and they are: octal or base of eight numbers (0 to 7), decimal or base of ten numbers (0 to 9) and hexadecimal or base of 16 numbers (0 to 9, A to F).
Independent of it's base, all numbers are stored in the binary base and all operations are done as binary numbers.
The logic of programming languages, all of them, are based on how the machine works. Even if you cannot see clearly how both things are related, they are, after all software only runs because a hardware exists. It's like your brain and your thoughts, you only think because you have a brain and you'll never be able to think in a way that your brain isn't supposed to go. We're attached to our hardware (at least our brain is a hell of a hardware).
The binary logic operates on binary numerals, the base in which the computer operates. All other bases (octal, decimal, hexadecimal) are translated from binary. To understand better how binary logic works please refer to the machine architecture text available here.
But binary logic is not only used to binary numbers, we can apply it to any base as long as it's consistent with the binary base results. One way of doing that is to convert all base to binary, execute the logic and convert the result to the original base, and that's exactly what the computer does.
In order to understand the computer logic we must first see what are the available operations.
The basic logic operations are AND, OR and NOT. The AND operation takes two arguments (binary function) and returns true only if both are true. The OR operation also takes two arguments and returns true if at least one of them is true. At last, the NOT operator takes only one argument (unary function) and inverts the value, so it returns true for a false argument and false for a true one.
Based on these three operations you can construct several thers like NOR (NOT + OR), NAND (NOT + AND) and XOR (Xclusive OR), which returns true if the values differ and false if they're the same (ie. 1 XOR 1 = false). In fact, all operations in a computer are constructed using those basic three operations, including arithmetic, memory read and write, peripheral handling, etc.
Those operations can be extended to all other values, either directly converted or based on evaluation. A binary opertation on a multi-bit value is done bit by bit, no matter how many bits that value have, so if you operate:
10 AND 7 = 2 because: 1010 AND 0111 = 0010 because: 1 AND 0 = 0 0 AND 1 = 0 1 AND 1 = 1 0 AND 1 = 0
You can use the same concept within the evaluation of other operations, and that's what gives the programming languages most of it's power. You can guide your program through logical operations and assert the results of your computations using only those operations.
For instance, if you want to test two assertions at the same time when both have to be true you can use the AND operator between them, or if just one have to be true, use OR. It means that, no matter how your assertions work, the logic will treat values as true and false when they are being asserted, thus they're converted to binary values and thus any binary logic works for them.
There are several kinds of operators on programming languages and most of them are the same on all of them. Operator is a symbol that takes one or more arguments and do operate, returning or not a value.
The most basic operators are the arithmetic ones.
+ adition - subtraction * multiplication / division % remainder (rest of a division) So: 13 / 5 = 2; 13 % 5 = 3;
There's also the binary operators, also called bitwise operators because they operate bit-per-bit on the arguments, the most common are:
& AND | OR (Pipe symbol) ^ XOR (carat symbol) << slide left one bit >> slide right one bit So: 10 & 7 = 2; 10 | 7 = 15; 10 ^ 7 = 13; 10 << 1 = 20; 10 >> 1 = 5;
Exercise to the reader: convert the numbers above to binary base, perform the operations above bit a bit and check the results.
Other basic operator is the assignment operator, that takes one argument, a value and assign it to a variable. The assignment operator returns the value assigned and keeps the same type (integer, real, string, etc). So, all these assignments are valid:
x = 10; x = y + 5; x = y = z + 5;
You can also use the assignment operator to increment the value, either by a directly statement or it's shortcut. Also note that the shortcut works for all other operators:
value = value + 1; value += 1; x -= 10; val /= 5; foo &= bar; etc.
And also, the "increment/decrement by one" operator is so important within programs that it have it's own even-shorter-cut:
++x; x++ --x; x--;
The difference between pre-incremented and pos-incremented as seen below can be quite complex but the rule is simple: ++x first increments than return the value, and the later is the opposite. The same happens with the decrement operator.
x = 10; foo = x++; // foo = 10, x = 11 bar = ++x; // bar = 12, x = 12
To compare things you need equality and inequality operators that always return a boolean value (true or false).
== equals (not the same as the assignment operator) != different < smaller <= smaller or equals > bigger >= bigger or equals
When you group two or more operators on the same statement you may think that there might be some precedence going on, otherwise would just be random operations. This is true and most programming languages follow the same rules (and that's probably the strongest reason you could program on any language if you know the basic rules).
Precedence is about "who comes first" but also have another glitch: from where ? Some operators take precedence from left to right and others from right to left, so it's always good to know the directions:
Oper. Precedence Association * / % left to right binary + - left to right binary << >> left to right binary < <= > >= left to right binary == != left to right binary & left to right binary ^ left to right binary | left to right binary && left to right binary || left to right binary ?: right to left ternary = += and all combined assignment right to left binary , left to right binary