Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C++ October 2, 2017.

Similar presentations


Presentation on theme: "Introduction to C++ October 2, 2017."— Presentation transcript:

1 Introduction to C++ October 2, 2017

2 Sample C++ Program #include <cstdlib> #include <iostream> using namespace std; int main() { cout << "Hello, world!\n"; return 0; }

3 Sample C++ Program The first two lines are known as include directives: #include <cstdlib> #include <iostream> These tell the compiler/IDE where to find information about certain items that are included in your program. The second directive tells the IDE how to interpret cout. The next line further explains the include directives: using namespace std; This says the names defined in the iostream library should be interpreted in the “standard” (std) way.

4 Sample C++ Program The next line, int main(), says that the main part of the program begins here: int main() { (main part of the program) } Everything between the curly braces { } is the main function. cout << "Hello, world!\n"; outputs “Hello, world!” return 0; tells the IDE “end the program when you get here.” Notice that every statement other than include directives ends with a semicolon!

5 Input and Output Use cout << to output text to the screen and cin >> to input something. Example: Suppose you want to ask the user to input a number. cout << "Please enter a number:\n"; cin >> …then what?

6 Variables A variable is a placeholder for a piece of information that can change, such as a number. A variable is declared with its type name followed by its identifier. To declare an integer with name (identifier) x: int x; Typically, variables should be initialized when declared. int x = 0; If not initialized, the variable will contain a “garbage value,” which may cause unexpected behavior in the program.

7 Variables In C++, = does NOT indicate equality!!
It is used to assign a value to a variable. int x = 5; assigns the integer x the value of 5. x = y does not mean x and y are equal; rather, it assigns x the value of y. int x = 5; int y = 3; x = y; Now the value of x is 3. To indicate equality, use two equals signs: == (“equals equals”). On the AP exam, = does indicate equality;  is used to assign a value.

8 Variables When working with variables, each subsequent line of code gives the variable a new value. int x = 1; x = x + 1;  now the value of x is 2 x = x + 1;  now the value of x is 3 x = x + 3;  now the value of x is 6

9 Practice Question Consider the following code: What is the value of x?
int x = 3; int y = 7; int z = 4; y = z; x = y; What is the value of x?

10 Practice Question Consider the following code: What is the value of x?
int x = 3; int y = 7; int z = 4; z = y; x = z; What is the value of x?

11 Operations Addition, subtraction, multiplication, and division work as you would expect. x + y x – y x * y x / y There are some nuances with division that we will cover later. Parentheses also work as you would expect. (x + y) * z

12 Practice Question Consider the following code: What is the value of x?
int x = 3; int y = 7; int z = 4; x = x + y; What is the value of x?

13 Modular Arithmetic x mod y tells you the remainder when x is divided by y. To find one number mod another, use the percent sign: x % y Example: int x = 3; int y = 8; y % x == 2 x % y == 3

14 Variable Types The most common variable types you will see are:
Integer (int) Double (double), which can hold numbers with a fractional part (e.g., 1.5) String (string) Character (char) Boolean variable (bool), which is either true or false Less commonly used types include: Short integer (short or short int) Long integer (long or long int) Floating-point number (float)—like double, but with a smaller range Long double (long double)

15 The Don’ts of Variables
Don’t put a variable in quotes! cout << x; displays the value of x. cout << "x"; displays the letter x. Text in quotes is considered a string—we’ll revisit this later… You do need to put non-variable text in quotes. Suppose the value of x is 5. cout << "x = " << x; displays x = 5. cout << x = << x; will give you an error.

16 Input and Output Returning to the original example: suppose you want to ask the user to input a number. Use a variable to store the number! int x = 0; cout << "Please enter a number:\n"; cin >> x;

17 Input and Output What if you want the user to enter, say, two numbers?
Use cin >> first number >> second number! int x = 0, y = 0; cout << "Please enter two numbers with a space between them:\n"; cin >> x >> y; cout << "The sum of the two numbers is: " << x + y << endl;


Download ppt "Introduction to C++ October 2, 2017."

Similar presentations


Ads by Google