1 / 12

Understanding Variables in C++: Declaration, Input, and Echoing Values

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.

sawyer
Télécharger la présentation

Understanding Variables in C++: Declaration, Input, and Echoing Values

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. Example • intalice; Type: int integer (whole number) alice: name of variable Variable is declared. ;

  4. 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; }

  5. 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

  6. Examples of variable names • count • Sum • Salary, • Next_character • total • reply

  7. Type in and run the cin program. Make sure to type a number in when you run the program • What were the results:

  8. 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

  9. Self Test: What do these lines of code do? • int Alice, Tom; • cin >> Alice >> Tom; • Alice = Tom; • Tom = Alice; • cout << Alice << Tom;

  10. Answer goes here!

  11. 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;

  12. Answer goes here!

More Related