[[TOC]]
Following every section, you will find some combination of sub-sections titled Exercises, Projects and/or Research. These sections are to test yourself and interact with the material more deeply. They can be used as follows:
Exercises: Like any textbook these are your traditional exercises or problem sets. They are labeled with varying difficulty and will generally have a well-defined answer or at least one that can be found by reading this book. Some exercises will take longer than others to complete, but an advanced student shouldn't find themselves spending too much time to come up with an adequate answer at least. Solutions* to exercises are listed on the book's website. (Not all solutions are definite, or the only possible answers.)
Research: Questions in this section follow a similar pattern to "Exercises" except they are completely open-ended, and you are almost guaranteed not to be able to find an answer in this book (at least at that point in reading). This is your opportunity to practice a Computer Scientist's greatest skill: research.
Projects: Longer and more in-depth than the exercises, Projects will generally take a reader a considerable amount of time to complete. They are generally left open-ended to let your imagination turn them into whatever you like, but some are labeled either solved or verifiable. Projects labeled solved have source code listed on the book's website, while verifiable projects mean you can upload your solution on the book's website to be tested for completeness.
C-x
means CONTROL+x
M-x
means ALT+x
With commands and other _, things wrapped in:
[...]
, are optional.<...>
, are required, but variable.All other text is meant to be verbatim, unless otherwise obvious (such as personalized strings).
To differentiate numbers of different system, we will often prefix them as you will encounter in the next few guides.
Decimal - base 10 - rarely comes with a prefix.
0b
0x
0o
:languages used
notation faq
Hello, World!
Before you start writing any complicated programs, you'll need to get a simple program to work first. Let's take a look at this first program: Hello, World! and see a few examples in popular languages.
A “Hello, world” program is any simple program that you can use to test uf your programming environment is working properly. It can be anything you like, but the convention is to just print something like “Hello, World!” to the screen.
Hello, World! programs are simple enough, yet robust enough to show whether everything is in working order by giving you some output to inspect.
In addition to testing your environment, Hello, World! programs are a great first program for people new to a particular language—or programming in general.
But why Hello, World!?
Its use dates back to the mid 70's (perhaps earlier) with the advent of the C programming language. “Hello, world” was used as an example in Brian Kernighan's Programming in C: A Tutorial, and the popularity of his and Dennis Ritchie's The C Programming Language made it so Hello, World! seen as standard by millions of programmers.
Let's take a look at some examples in popular languages. Read through the explanations, but don't expect to understand everything. As you progress through these courses all of this will reveal itself to you.
As you'll find with most programs, C++ will tend to have a bit more code to complete a similar task. A detailed explanation is given below.
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
We first need to tell the preprocessor to paste in the contents of the file iostream
, which contains declarations for input and output functionality—i.e. printing to the console.
We then create a function for the entry point of our program—where it is that our program starts executing when we run it—called main
. This function declaration for main()
takes no argument but has a return value of type integer that signifies a status code for our program to send back to the operating system after it has finished running.
Next is the definition of main()
—denoted by the opening and closing braces ({ }
). Within this block we pass the cout
object (that is within the std
namespace) the arguments “Hello, World!”
and endl
(so that proceeding text will be on a newline) via the <<
input redirection operator.
Lastly, we return 0
from the main()
function, letting the operating system know the program has finish with no errors.
In JavaScript, we call the log
method attached to the console
object with the argument “Hello, World!”
.
console.log("Hello, World!");
With Python, it is as simple as a call to print
with your desired text.
print("Hello, World!")
In this section, just take a glance at each individual code. Don't worry about the specifics, as its far too much to think about so early in your journey as a programmer. Just note some differences you see, and remember the goal: Print/output Hello, World!
.
public class HelloWorld {
public static void main(string[] args) {
System.out.println("Hello, World!");
}
}
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
}
print("Hello, World!")
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
imperative functional logical
compiled interpreted vm
weak vs strong typed ...
Variables
Literals
Data Types
Integers
int myAge = 72;
Floating Point Values
double pi = 3.14159;
Boolean
bool learning = true;
Characters
char letter = 'N';
Strings
std::string sentence = "A star-ing of characters.";
Arrays
int numbers[] = { 4, 8, 15, 16, 23, 42 };
User-Defined Types
User-defined types let you build types of your own, mixing and matching logic involving exisiting types (such as above), to simplify details or bundle data together.
The usage of a User-Defined type might look something like this, where MyType is a type you would have to create yourself.
MyType something;
User defined types are explained in detail in Classes and Objects.
literals default initialization constants: const / final inferred types vs explicit types different widths
Scope
if if else else
switch
while do while
for for each
class struct / record
c style? lisp style? pythonic?
A Word On Jump Statements (break, continue, goto, return, jump, ...)
Conventions
Case of variables and types
Paradigms
Find a new language and create the equivalent of a "Hello, World" program in that language. Then, answer some of the following questions:
What style does this language use?
What are the benefits of using this language over the ones learned previously?
For what purpose was this language developed?
A few unconventional languages to consider:
common lisp
vimscript
prolog
At this point you should have a general understanding of basic programming concepts and be exposed to some syntax. This guide will serves as a quick review of these concepts by demonstrating how C++ specifically does things, and aims to give students a strong programming foundation for them to move on to other languages or excel further in C/C++.
As introduced in programming basics we will be - temporarily - using Microsoft's Visual Studio Code and its embedded terminal to write and run our programs.
If You went through the [introductory series on programming], this Hello, World! program might look familiar to you. But though it's familiar, it still might not make much sense beyond what the output should be. Let's spend a few minutes deconstructing what we see.
#include <iostream>
int main()
{
std::cout << "Hello, World!" << '\n';
return 0;
}
#include <iostream>
We open the first line with a =#include= (read as "hash-include") of the =iostream= header file.
The hash denotes a preprocessor directive. That is, these lines are run before the compiler compiles your code. The =include= literally means paste the contents of file x into this file. So, on this line we are pasting the all contents of the =iostream= file into this file when we compile our code.
The angle brackets say where the file is located for the preprocessor to look first for your file. For now, just think of =<...>= as for system libraries, when defining your own use ="..."=.
#include "MyFile.h"
Header files are just normal files but they contain all bits of code to say "this code exists in another file that I've defined somewhere else". This will be explained in detail soon, but essentially this is done so that C++ knows our functions or classes (etc.) exist, but we can define them somewhere else so that we aren't pasting all of their contents here, just their /names/.
The =iostream= header file is a /standard library/ header file that contains all the useful declarations for working with input and output streams (hence the =io= and =stream=).
Moving on to line 3 we have our =main= function.
int main()
This is the standard starting point of your program; when you run your program, it will begin executing line-by-line starting here. =int= denotes the return type where =return 0= on line 7 means executed successfully - without error.
This next couple of lines do all the fun stuff.
std::cout << "Hello, World!" << '\n';
We open up our function block on line 4 with ={ ... }= and pass the desired output text to the =cout= object defined in the =std=, standard library, namespace.
(Namespace are essentially tools to define named scopes so you can differentiate between multiple resources that might have the same name, and are discussed in detail later on)
=cout= allows you to put text to the console. It, and its counterpart =cin= will be used extensively in your early programs.
Lastly, =<<= is defined as the /stream insertion operator/ which is really just a fancy function call to pass text to =cout=.
Notice how strings of characters are defined in double quotes (="= ) and single characters are wrapped in single quotes (='=), like the escaped newline character, =\n=.
Lastly, always remember to end your statements in C++ with a semicolon (=;=), and you now understand everything you need to begin writing effective programs.
Try playing around with this starter file to get it to print various string of text, on multiple lines.
We now transition to learning a set of fundamental tools for developers and computer scientists.
Many of the utilities learned here may seem overly complex or outdated. In some cases this may be true. Still, they are utilitized heavily in the real world, and because of their complexity, it will make learning simpler tools all the more simple for you in the future, with the added benefit of gaining an understanding of how you might accomplish more intensive tasks if you need to.
Our first two sections, gcc and the command line, we've already encountered in our foundational guide to C/C++. In those sections we will expand on this learning, providing you with the skills to do anything beyond just running memorized commands.
Beloved by Web Developers and hobbyist alike, VSCode is a top 3 editor of mine. It is very simple and light to download but is infinitely extensible with Extensions from 3rd party creators. However, this is both a blessing and a curse; because of this you can configure it to do just about anything but with many languages you might have trouble with the initial configuration since it is essentially not far off from a fancy Notepad or TextEdit without extensions.
You'll see their products listed frequently below; JetBrains makes a lot of really great products that work as they should out of the box. However, each tool is very narrow to a particular language and seasoned programmers often say they prefer alternatives. But with that being said, they are excellent for beginners (really the only IDEs I would comfortably recommend to people just starting out), have a ton of functionality you don't get with a plain editor, and if and when you decide to try another language, they have a large suite of editors that work well with other languages.
It's not an easy feat, but if you dare, Vim is one of the most incredible tools you will ever utilize as a programmer. It is not just a text editor but a productivity tool and _. Even when I'm not using Vim itself, I have configured all of my editors to emulate its command mode. Interestingly enough, if you're on MacOS or Linux, theres a good chance the command line version is installed on your system already. For more info about Vim see the [official Vim website] or my [complete guide to Vim for beginners].
Another excellent choice but, again, another difficult one to start with. Like Vim, Emacs is an exceptional editor and much like VSCode it is infinitely extensible; but even more so. These guides themselves were, for the most part, all written in Emacs. A more gentle introduction might be a distribution such as [Doom Emacs] or [Spacemacs], but even then you'll be spending more time figuring out your editor instead of learning to program.
Vim is a powerful text editor and key binding set for working with text files. It has been around since the 1970s and is widely used by developers and hobbyist to date.
Essentially, if there is some task you wish to do within a file: Vim can do it, and likely more efficiently than other editors or tools available.
Touch typing isn't a requirement to learn Vim, but it will certainly make things easier.
From the command line run: vim [filename]
, or open the app on your desktop, if it exists.
After making changes, you can save and quit by typing :wq
followed by the ENTER key. To save without quitting use just :w
, or to quit without saving type :q
. If you are unable to quit because you have unsaved changes that you don't want to save, use :q!
to quit and discard those changes.
Move your cursor with h
, j
, k
, l
. They correspond with movement to the LEFT, DOWN, UP, and RIGHT, respectively. This is the hardest but most crucial command-set to learning Vim. Spend a good amount of time here before moving on.
Notice how when you cross vertically over a shorter line (between two longer lines) that Vim remembers the horizontal position you were at before moving.
Move around by words (groups of characters) with w
, b
, e
.
w
moves the cursor forward by the beginning of words, b
moves the cursor backwards by the beginning of words, and e
moves forward by the end of words. Each of these commands have a capital-case counter-part (W
, B
, E
) that essentially do the same command except they are strictly whitespace delimited.
Within a line, you can jump to the beginning or end of it with 0
and $
, respectively. Alternatively, if you want to go to the first non-whitespace character in the line, use ^
instead of 0
.
You can quickly jump to the top or the bottom of a file:
gg
to go to the top, andG
to jump to the bottom.C-f
and C-b
allow you to move forward and backward in the file by a full screen height. Also, C-d
and C-u
are useful, moving up or down by a half screen height.
So we can move around, but what use is an editor without editing. To begin typing text we have to enter INSERT
mode.
i
and a
, and their counterparts I
and A
, put your cursor into INSERT
.
i
before the cursor,a
after the cursor,I
at the beginning of the line (ie. ^i
), and A
at the end of the line.Also, o
and O
are useful for entering INSERT
mode by opening a new line below or above the current line.
Commands like dd
delete the complete current line; C
deletes the current line AFTER the cursor and enters insert mode.
/query
n
N
f
t
;
,
and uppercase version%s/old/new/
%s/old/new/g
'<,'>/old/new/
replace in a selected rangeescape /
s with \
.
See the cheatsheet in More Resources for a full list of available commands. But note: what you can do with Vim far extends the commands listed in the list below, it is the way in which you combine them, extend them, and introduce plug-ins that truly make Vim an every-thing tool.
If still have a taste for what Vim can offer, follow up with some of the supplementary material and look forward to a complete guide to vim coming soon.
We've learned a great deal of programming thus far. Most of our time, however, has been spent in a very narrow (albeit most common) subset of programming called Imperative Programming. This series aims to educate readers in the various other forms of programming that exist. At the very least, you will come away with new - sometimes better - ways to think about computational problems. At most you will discover a style of programming that you prefer, and can further your journey more directed down that pathway.
... which unfortunetly might mean un-learning some of what we just did momentarily. So make sure you're confident with the core concepts explained earlier to ensure that it isn't lost away.
What this courses does not cover, as many paradigms courses do, is a discussion of language implementation, syntax, design, etc. These concepts will be left to a more complete instruction, on Language and Compiler Design.
Write a procedure rotate-right
that emulates the behavior of the built-in rotate
function.
Clean Code? Design Patterns
While Software Engineering deals quite a bit with Project Mananagement, a fuller discussion of this is delegated to the gude on Projects and Employment.
Project Management
Open Source
Work
Startups
** General *** Marketplaces
*** Web Development
Intermediate C++ Programming
Scientific Computing with Python
Full-Stack Web Development
Game Development
Operating Systems
Databases
IT
The Internet
Computer Networking
Web Security
Front-End Web Development
Introduction to the Web
Introduction to HTML
Tags and Elements
Attributes
Boilerplate and Comments
Containers
Text
Images
Links
Structure
Introduction to CSS
Introduction to CSS
Color
Introduction to Flexbox
JavaScript For Programmers
ES6
ReactJS