Tuesday, June 25, 2013

Four Subjects on C++


 Information within this post is derived from the site http://www.cplusplus.com/
It is a valuable resource for beginner programmers.

Structure of a Program:
This introductory taste of C++ programming mirrors our practice in class. The task used in the example is that of writing a standard “Hello World!” sentence. Of course, I would spice it up with a comment such as “I am alive”, or “I am the harbinger of your destruction”, but that is personal preference.
As we discussed in class, the initial step for any programmer is to include a comment about the material he is writing. Comments are marked with // before the text. A more advanced version of the comment involves a block comment, notated with /* */. Unlike a regular comment, a block comment extends across lines until the end half */.
Everything grows more complicated from there. The step “#include <iostream>” is a message to the preprocessor. Essentially, this line is saying to the compiler that a file is going to be used from <iostream> in order to run the program. In our class, we utilized <stdio.h> instead, thus pulling a similar file from a different source.
Following with “int main ()”, which denotes that the main function of the program is about to be written. The brackets {} include the subsequent program. By writing “printf(“text”)”, you are telling the program to read off the text once it is run.
After writing the statement, a semicolon appears, marking the end of said statement. The “return 0;” statement tells the program to finish. Finish it all off with a deciding “}”.
Below are visual representations of the program covered above. Next, I will discuss a section on variables and data types, and will be markedly less involved than with this section.


1
2
3
4
5
6
7
8
9
10
// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}

1
2
// line comment
/* block comment */ 

Variables. Data Types.
The material in this section is rather lengthy and will be broken down on this blog. The initial topics are variables and identifiers. A variable is, basically, a value stored in the computer's memory, such as a=6 or a=b+1. The computer will remember these variables as values for reference.
An identifier is a string of letters, numbers, or underscore characters (_). Called variable identifiers, these formations can be used just as with the more simplistic versions.
Be aware that C++ recognizes various data types (characters, integers, boolean values). When C++ stores a variable, it condenses it into bytes (which can hold a range of representations from 0 to 255). Different values take up different numbers of bytes depending on size.
Variables can be declared by listing their variable type followed by the new declaration. As an example, to declare the variable a as an integer, one would write “int a”. Certain descriptions such as “long” and “unsigned” may be used in conjunction with a declaration such as “int”, but I will not delve into this concept.
An important matter to note is that variables can be declared as global or local. Global variables extend throughout a code in which it is declared, whereas local variables are viable only within their brackets {}.
In order for a variable to take on a definitive value, it must be initialized when it is declared. This may be accomplished through “=” or “()”. As an example, to declare that a=6, one would write either “int a = 6;” or “int a (6);”.
Lastly, there are strings. Strings are long variables that take up more than one character space (a string of characters, you could say). Strings are special in that, in order to be utilized, a special header must be included in the program (#include string). Subsequently, strings function like any other variable.
I apologize for the somewhat rushed nature of this section. Unfortunately, there are two more on their way.

Constants
Any individual with a middle school education will recognize that a constant is a fixed value. Within C++, there is a kind of constant called a literal, which is what most people would recognize. Literal constant are the obvious “a=6” variety. However, literals can be divided into five different varieties, but they are no complicated to discuss or comprehend.
Integer and floating point numbers are the first two literals. Obviously, integers consist of positive and negative values, while floating point numbers are less obviously composed of decimals and e characters (e.g 6.02e23).
Non-numerical literals follow, with character and string literals. Character literals are, you guessed it, single characters (a, b, c). Characters are notated with single quotes, as in 'a'. String literals are strings of characters (recall from a few sentences ago), and are represented with full quotation marks, as in “This is probably not too helpful”. A steady pattern may be noticed in the terminology that permeates C++.
Finally, there are boolean literals. These delightful intervals are adorable not only because their name, but because they represent one of two values: true or false.
Constants may be declared for the entirety of a program through the “#” header as a preprocessor directive. The proper heading is “#define” followed by the literal value.

Operators
At last, we come to the final material for this post: operators.
Assignment is the first major subject discussed. Broken down simplistically (though it is simple to begin with), assignment bestows a value with another value. Variables, constants, or so forth are what become assigned. One makes and assignment via the “=” sign. Assignments are read left to right, such that “a = 6” would assign a with a value of 6.
Arithmetic operators may be used, as well. These are “+, -, *, /”, and should be entirely comprehensible. The only non-obvious arithmetic operator is “modulo”, represented by “%”. Modulo gives the remainder of a division between two numbers.
Rational and equality operators are the next subjects of note. The inclusion of these operators (==, <, >, =<, >=, !=) will result in a boolean value of true or false. This may sound confusing, but consider how 5 = 5 may be true, but 2<1 is false. Care must be taken when working with these operators, however. It is important to include the double == when working with equality, as single = will denote an assignment (recall from earlier).
Logical operators are only slightly more complicated. These operators are composed of “!, &&, ||”, which represent Not, And, and Or. Three examples will be utilized to demonstrate the concepts behind these operators. With Not, the statement !(3 == 3) results in a false statement, because of the Not operator. With And, the statement ( (3 == 3) && (5 < 6) ) results in truth, whereas ( (3 == 3) && (6<5) ) is false, because the And operator sought to combine the two. Had we used the Or operator with the last statement, as in ( (3 == 3) || (6<5) ), the result would have been true because one of those statements is true.
Conditional operators are a bit more complex (at the far end of the simple spectrum). By utilizing the phrase “<condition> ? Result 1: Result 2”, one creates a conditional operator. If a conditional is true, Result 1 will show. If it is false, result 2 will show. To illustrate, take the conditional (2 == 5 ? 1:4). Since 2 is not equal to 5, the conditional is false, and Result 2 is selected (4).
There are a few more operators, but I will not be going over them within this post.


I am entirely uncertain as to whether writing all this was worth the time. I certainly have a stronger comprehension than if I had just read the material. However, if some stranger should happen upon this blog for information, the relative re-hash will likely be insufficient for quality understanding. Regardless, I am going to bed.

No comments:

Post a Comment