12
Objective: Students will be able to: Declare and use variables Input integers

Objective: Students will be able to: Declare and use variables Input integers

Embed Size (px)

Citation preview

Page 1: Objective: Students will be able to: Declare and use variables Input integers

Objective:Students will be able to:

• Declare and use variables• Input integers

Page 2: Objective: Students will be able to: Declare and use variables Input integers

Variables

• Hold data

• Can be numbers, letters, words

• Place in main memory that holds the data

• Has a name that is assigned by the programmer

Page 3: Objective: Students will be able to: Declare and use variables Input integers

Example

• int alice;

Type: int integer (whole number)

alice: name of variable

Variable is declared.

;

Page 4: Objective: Students will be able to: Declare and use variables Input integers

program to input an integer and have it print out (echo)

#include <iostream>

using namespace std;

int main (void)

{

int alice;

cin >>alice;

cout<< “There is an echo “;

cout << alice;

cin.get();

return0;

}

Page 5: Objective: Students will be able to: Declare and use variables Input integers

Variable Names

• Can consist of numbers, letters, and underlines

• Can be as long as you like

Provided that:

• It starts with a letter

• It is unique for that program somewhere in the first 32 characters

• Doug and doug are different

• Can’t be a keyword

Page 6: Objective: Students will be able to: Declare and use variables Input integers

Examples of variable names

• count

• Sum

• Salary,

• Next_character

• total

• reply

Page 7: Objective: Students will be able to: Declare and use variables Input integers

Type in and run the cin program. Make sure to type a number in when you run

the program

• What were the results:

Page 8: Objective: Students will be able to: Declare and use variables Input integers

Assignment

• One way to assign a value to a variable is to input a number from the keyboard like we just did in the last program.

• Another way is to use an assignment statement:

• int alice, bill; declares variables

• cin>>alice input number for alice

• bill = alice; makes bill equal to alice

• cout<< bill; prints out number inputted

Page 9: Objective: Students will be able to: Declare and use variables Input integers

Self Test: What do these lines of code do?

• int Alice, Tom;

• cin >> Alice >> Tom;

• Alice = Tom;

• Tom = Alice;

• cout << Alice << Tom;

Page 10: Objective: Students will be able to: Declare and use variables Input integers

Answer goes here!

Page 11: Objective: Students will be able to: Declare and use variables Input integers

Self Test: What do these lines of code do?

• int Alice, Tom, Save;

• cin >> Alice >>Tom;

• Save = Alice;

• Alice = Tom;

• Tom = Save:

• cout<< Alice << Tom;

Page 12: Objective: Students will be able to: Declare and use variables Input integers

Answer goes here!