Photo by Matthew Brodeur

Control Flow

So far, we have created basic programs that perform a single task without making any decisions. Most (all but the most trivial) programming languages provide decision-making constructs (Conditional Branching). C++ provides two forms of branching. The “If Statement” and the “Switch Statement” . Note: Looping is also a form of branching. The looping concept is extensive enough that we will deal with looping separately in its own article. If Statement The “If Statement” allows code to be executed when a specific condition is fulfilled and optionally an alternative piece of code otherwise. ...

December 2, 2013 · 6 min · Loki Astari, (C)2013
Photo by Ben Griffiths

Functions

Usage All C++ applications must have at least one function called main(). Additionally, you can have user-defined functions that encapsulate individual tasks, thus making the code cleaner and easier to read. Therefore, this is a useful feature if you repeat the same task many times with only slight variations: function1.cpp #include <string> #include <iostream> int main(int argc, char* argv[]) { std::cout << "What is your first name?\n"; std::string firstName; std::cin >> firstName; std::cout << "What is your second name?\n"; std::string secondName; std::cin >> secondName; std::cout << "What is your Mother's name?\n"; std::string motherName; std::cin >> motherName; std::cout << "What is your Father's name?\n"; std::string fatherName; std::cin >> fatherName; } The obvious repetition here is easy to spot. We can simplify this code using a function that does all the common work. We can pass anything unique as a parameter to the function. ...

November 24, 2013 · 4 min · Loki Astari, (C)2013
Photo by Oscar Nilsson

Variables

In most programming languages, you have the concept of variables. These are named objects that hold a value (more formerly referred to as state). By manipulating a variable, you manipulate the state of the object the variable refers to. add.cpp void addFunction() { int x = 0; // Declare (and initialize) a variable called "x" x = x + 5; // Manipulate the variable "x". // The variable "x" now holds the value "5" int y = x + 3; // Declare (and initialize) a variable called "y" // This will take the value "8" by adding "+" 3 // to the value of "x" } C++ is a strongly typed language. This means each variable has a specific type that does not change (above that type is int). The operations that can be performed on an object depend on the object’s type, and the operation’s result can depend on the types involved. C++ has several built-in types (listed below) but allows the definition of new user-defined types (which will be described in a later article). The standard library provides a set of commonly used standard types (listed below). ...

November 19, 2013 · 3 min · Loki Astari, (C)2013
Photo by Roman Synkevych

Common Mistakes

1: using namespace Every new developer who comes to C++ always starts writing code like this: myfirstprog.cpp #include <iostream> using namespace std; It seems reasonable, and every book on learning C++ perpetrates the same mistake. The problem is the using namespace std;. In programs that are only 10 lines long (like in most books), it does not cause any problems. But as soon as your code strays to any meaningful size, it becomes an issue. The problem with teaching new developers this technique is that they are unaware of the problems it causes, so it becomes a habit for all code they write. Break this habit now before you start doing it without thinking at the top of every source file you write. ...

November 18, 2013 · 4 min · Loki Astari, (C)2013
Photo by ThisisEngineering RAEng

Hello World

This is the first article in the series OK. Let’s do this. I keep trying to think of something big and interesting to write about, but that is just not working. All my time is spent trying to think of a blockbuster idea, which just gets in the way of writing. So, let’s start with the small things. If I can get into the habit of writing something a couple of times a week, maybe we can work up to interesting stuff. ...

November 12, 2013 · 2 min · Loki Astari, (C)2012