Lesson 1: The basics of C++ This tutorial series is designed for everyone: even if you've never programmed before or if you

have extensive experience programming in other languages and want to expand into C++! It is for everyone who wants the feeling of accomplishment from a working program.

What do I mean? C++ is a programming language--it will allow you to control your computer, making it do what you want it to do. This programming tutorial series is all about helping you take advantage of C++. Getting Set Up - C++ Compilers

The very first thing you need to do, before starting out in C++, is to make sure that you have

into an executable that your computer can actually understand and run. If you're taking a cou

own, your best bet is to use Code::Blocks with MinGW. If you're on Linux, you can use g tutorial. Intro to the C++ Language

compiler, such as Turbo C++, you'll need to read this page on compatibility issues.) If you h

A C++ program is a collection of commands, which tell the computer to do "something". T

Commands are either "functions" or "keywords". Keywords are a basic building block of the own outline, composed of sections. Each section might have its own outline, or it might functions and keywords

you'll see this in our very first program, below. (Confused? Think of it a bit like an outline fo

cin. The quotes tell the compiler that y . If you have programmed in another langu however. The semicolon is part of the syntax of C++ The next important line is int main(). I'm alive! Oh.get(). you.But how does a program actually start? Every program in C++ has one function. The #include is a "preprocessor" directive that t actually creating the executable. This line tells the compiler that there is a function nam }) signal the beginning and end of functions and other code blo The next line of the program may seem strange. Let's loo Let's look at the elements of the program. "using namespace std. always nam also call other functions whether they are written by u So how do you get access to those prewritten functions? To access those standard functions 1 2 3 4 5 6 7 8 9 } int main() { cout<<"HEY. using namespace std. #include <iostream> this does is effectively take everything in the header and paste it into your program. By including header files. and Hello World!\n". the cout object is used to display text (pronounced "C out"). It uses the << symbols call with the ensuing text as an argument to the function. you gain access to many differen the statement. This line tells the compiler to use a group of functions semicolon is used to end allow the program to use functions such as cout.".

such The next command is cin. but if we wanted to return something else. A functions require you to manually return a value).get(). I'm alive! Oh. This command keeps that window f tim Upon reaching the end of main. creating you should a simple read the Once you've got your first program running. #include <iostream> The final brace closes off the function. You should try compiling this program and running tutorial actually If you are takes not you using through Code::Blocks. and then close the window. and Hello World!\n".character that stands for a newline (we'll talk about this later in more detail). using namespace std. run the program. int main() { cout<<"HEY. why don't you try playing around with the cout .get(). the closing brace. you. It moves the c end of most lines. 1 2 3 4 5 6 7 8 9 10 11 } return 1. cin. This is another function call: it reads in input and expects Including that line gives you window. our program will return the value of 0 (and value is important as it can be used to tell the OS whether our program succeeded or not.

and should not be used when you need to s Declaring Variables in C++ . Several basic A variable of type char stores a single character. int.is each a keyword that you use when you d What's with all these variable types? right variable type can be important for making your code readable and for efficiency--some Sometimes it can be confusing to have multiple variable types when it seems like some varia are actually stored in memory. variables of type int store integers (numbers Each of these variable types . it will ignore it when running comment use either //. and float . when you the name of the variable. In pro variables which store different kinds of information (e. you must have a place to store that input. you should also start to learn how to explain your program frequently to help When you tell the compiler a section of text is a comment. Be certain no comment) you need for the program. When you are learning to program. but some will not. which tells the compiler that the rest of the line is a comment.char. a float is "inexact". it is useful to be abl User interaction and Saving Information with Variables So far you've learned how to write a simple program to display information typed in by you operator what about interacting with your user? Fortunately.g.An Aside on Commenting Your Programs As you are learning to program. environments will change the color of a commented area. it is also possible for your program to Of course. before you try to receive input. numbers versus letters).

all declaration and the use of the variable is one reason you might get an undeclared variable erro Using Variables Ok. all language keywords.To declare a variable you use the syntax "type <name>. this is called an undeclared variable. your program will not be compi mistake.". Usually. It is permissible to declare multiple variables of the same type on the same line. float the_float. c. Case Sensitivity Now is a good time to talk about an important concept that can easily throw you off: case sen words Cat and cat mean different things to the compiler. char letter. In C++. Common Errors when Declaring Variables in C++ If you attempt to use a variable that you have not declared. so you now know how to tell the comp Here is a sample program demonstrating the use of a variable: 1 2 #include <iostream> . b. you might have seen that declaration of a variable is always function). each one sho 1 int a. If you were watching closely. d. Here are some variable declaration e 1 2 3 int x.

so we throw it away.ignore() is another function t be truncated (that is. The keyword int declares thisisa must press enter before the number is read by the program.ignore(). int main() { int thisisanumber. cin>> thisisanumber.3 4 5 6 7 8 9 10 11 12 13 14 using namespace std. cin. We don't need this. the decimal component of the number will be ignored). cin. } Let's break apart this program and examine it line by line. cout<<"You entered: "<< thisisanumber <<"\n". Keep in mind that the v response will vary from input to input.get(). Try typing in it takes the enter key too. cin. cout<<"Please enter a number: ". Including multiple insertion operators on one line is perfectly acceptable and all of the variable in order to replace the variable name with the variable when executing the ou enclosed in quotation marks) and variables by giving each its own insertion operators (<<). but in no case is it particularly pretty. T ." The lack of quotation marks informs the on one line. Notice that w the output would be "You Entered: thisisanumber.

Rather. no matter what type you use. // a equals the original value of a with five added to it a == 5 // Does NOT assign five to a. <.com ebook. it checks to see if a equals 5. is not a way to assign a value to a variable. If you forget th the program.not try it. for good measure variables isn't really useful until you have some way of Comparing If you enjoyed this tutorial. The * multiplies. the equal sign compares the value of the left and right values should be used on the right side of an equal sign in left input to the equal sign. It . The other form of equal. Rather. and the + adds. ==. It is of course important to realize tha equal sign. it checks to often use == in such constructions as conditional statements and loops. the . and only one.subtracts. variable equal to the value o ord Here are a few examples: 1 2 3 a = 4 * 6. variables are uninteresting without the ability to mo >. which must be one. // (Note use of comments and of semicolon) a is 24 a = a + 5. check out the Cprogramming. You can prob For example: 1 2 3 a < 5 // Checks to see if a is less than five a > 5 // Checks to see if a is greater than five a == 5 // Checks to see if a equals five. Do not forget to end functions and declarations with a semicolon. Jumping into C++. Changing and Comparing Variables Of course. In some languages.

What punctuation ends most lines of C++ code? A. What is the correct value to return to the operating system upon the successful completion A. ' 5. . Buy Jumping into C++ today! Quiz: The basics of C++ If you haven't already done so. start() B. BEGIN and END D. be sure to read through Cprogramming. { } B. along with tons of sample code and practice problems. Which of the following is a correct comment? A. Programs do not return a value. : D. ** Comment ** C. B. What punctuation is used to signal the beginning and end of code blocks? A. main() D. -> and <C. 2. -1 B. . 1 C. system() C.place. What is the only function all C++ programs must contain? A. */ Comments */ B. ( and ) 4. /* Comment */ .com's introduction to 1. program() 3. C. 0 D.

( and ) 4. := B. Which of the following is not a correct variable type? A. = C. { Comment } 6. 1 C. What punctuation is used to signal the beginning and end of code blocks? A. ' 5. -> and C. { } B. . : D. double 7. 2. equal D. -1 B. C. == Quiz: The basics of C++ 1. Which of the following is a correct comment? . BEGIN and END D. int D.D. What punctuation ends most lines of C++ code? A. 0 D. program() 3. Which of the following is the correct operator to compare two variables? A. real C. B. What is the only function all C++ programs must contain? A. float B. start() B. What is the correct value to return to the operating system upon the successful completion of a program A. system() C. Programs do not return a value. main() D. .

== . real C. equal D. int D. Which of the following is not a correct variable type? A. := B. float B. ** Comment ** C.A. */ Comments */ B. { Comment } 6. double 7. /* Comment */ D. = C. Which of the following is the correct operator to compare two variables? A.

Sign up to vote on this title
UsefulNot useful