120 likes | 232 Vues
This guide introduces students to the concept of variables in C++. You'll learn how to declare and use variables to hold various types of data, including integers, letters, and words. The tutorial covers inputting integer values and printing them out, including variable naming rules and unique constraints within the first 32 characters. You'll also explore different methods of assigning values to variables through user input and assignment statements. Through practical examples, this guide aims to enhance your programming skills in handling variable data effectively.
E N D
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
Example • intalice; Type: int integer (whole number) alice: name of variable Variable is declared. ;
program to input an integer and have it print out (echo) #include <iostream> using namespace std; int main (void) { intalice; cin >>alice; cout<< “There is an echo “; cout << alice; cin.get(); return0; }
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
Examples of variable names • count • Sum • Salary, • Next_character • total • reply
Type in and run the cin program. Make sure to type a number in when you run the program • What were the results:
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: • intalice, bill; declares variables • cin>>alice input number for alice • bill = alice; makes bill equal to alice • cout<< bill; prints out number inputted
Self Test: What do these lines of code do? • int Alice, Tom; • cin >> Alice >> Tom; • Alice = Tom; • Tom = Alice; • cout << Alice << Tom;
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;