Introduction To C
Introduction To C
Introduction To C
In this lesson, we’ll start learning some basic concepts, and you’ll write your
very first C++ program.
Note: Don’t worry if some words don’t make much sense right now. We’ll
learn about them in a bit!
Hello World!
Take a look at the hello.cpp file in the code editor that is placed in the
middle of the screen. It’s a C++ program!
C++ programs are stored in files which usually have the file extension .cpp,
which simply stands for “C Plus Plus”.
The code inside our C++ file is a classic first step all new programmers take
— they greet the world through the terminal!
The terminal is the black panel on the right. It should be blank right now.
The code in the text editor will print text out onto the terminal. More
specifically, it will print the phrase Hello World!.
Before we explain what all that mumbo jumbo is, let’s run the program to
see what happens.
Instructions
1.
Press Run to see this program in action.
Output
High five! We just got your first program to run.
C++, like most programming languages, runs line by line, from top to
bottom. Here is the structure of a C++ program:
In between the curly braces is what we are going to focus on for now.
std::cout is
the “character output stream”. It is pronounced “see-out”.
<< is an operator that comes right after it.
"Hello World!\n" is what’s being outputted here. You need double quotes
around text. The \n is a special character that indicates a new line.
; isa punctuation that tells the computer that you are at the end of a
statement. It is similar to a period in a sentence.
Instructions
1.
Let’s write the whole std::cout statement from scratch.
Inside the curly braces, type the following and press Run:
Hint
Don’t forget the semicolon ; at the end!
#include <iostream>
int main() {
}
The terminal should look like this after you click Run:
Pattern
We learned how to output a line of text with the following code:
🚙💨
We can also output multiple lines by adding more std::cout statements:
Instructions
1.
Instead of displaying those two lines in the output, edit the code so that we
output the following pattern in the terminal:
1
2 3
4 5 6
7 8 9 10
Exactly how it is.
Things to remember:
Answer:
#include <iostream>
int main() {
Review
Woohoo! You have written a few C++ programs. 🙌
Instructions
1.
Before we move on, let’s write a letter to your future self.
P.S. This letter will be returned when you complete the course.
Checkpoint 2 Passed
Hint
Programming may seem tough and intimidating, but like everything, all you
need is a little patience and resilience.
Example:
#include <iostream>
int main() {
}
And here’s a letter to you.
Dear Learner,
I embarked on my programming journey in 2009 when I enrolled in a C/C++
course in college, not knowing what it meant nor where it could possibly take
me.
In the last ten years, programming has brought tremendous joy to my life
and I met so many wonderful people through it. I am incredibly jealous of
where you are right now because I would love to experience this all over
again.
Thank you for learning C++ with us. I can’t wait to see what you create with
it.
Sonny @ Codecademy