[go: up one dir, main page]

0% found this document useful (0 votes)
23 views7 pages

C 52

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

C 52

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
‘111212028 20.58 c++ Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools C++ Variables [crv] Looe | C++ Variables Variables are containers for storing data values. In C++, there are different types of variables (defined with different keywords), for example: + int - stores integers (whole numbers), without decimals, such as 123 or -123 * double - stores floating point numbers, with decimals, such as 19.99 or -19.99 * char - stores single characters, such as ‘a’ or 'B'. Char values are surrounded by single quotes + string - stores text, such as "Hello World". String values are surrounded by double quotes * bool - stores values with two states: true or false Declaring (Creating) Variables To create a variable, specify the type and assign it a value: Syntax type variableName = value; nitpsdiwwwsdschools.comlegpicpn_ variables. asp we ‘111212028 20.58 c++ Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools To create a variable that should store a number, look at the following exampl Example Create a variable called myNum of type int and assign it the value 15: int myNum = 153 cout << myNum; You can also declare a variable without assigning the value, and assign the value later: Example int myNum; myNum = 15; cout << myNum; Note that if you assign a new value to an existing variable, it will overwrite the previous value: Example int myNum = 15; // myNum is 15 myNum = 10; // Now myNum is 10 cout << myNum; // Outputs 10 nitpsdiwwwsdschools.comlegpicpn_ variables. asp ar ‘111212028 20.58 c++ Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools Other Types A demonstration of other data types Example int myNum = 55 // Integer (whole number without decimals) double myFloatNum = 5.99; // Floating point number (with decimals) char myLetter = 'D'; // Character Hello"; // String (text) true; // Boolean (true or false) string myText bool myBoolean You will learn more about the individual types in the Data Types chapter, Display Variables The cout object is used together with the << operator to display variables. To combine both text and a variable, separate them with the << operator: Example nitpsdiwwwsdschools.comlegpicpn_ variables. asp an ‘111212028 20.58 c++ Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools Add Variables Together To add a variable to another variable, you can use the + operator: Example int x = 53 int y = 63 int sum = x + y3 cout << sum; C++ Exercises Exercise: Create a variable named myNum and assign the value 5@ to it. nitpsdiwwwsdschools.comlegpicpn_ variables. asp an ‘111212028 20.58 c++ Variables Tutorialse Exercisesw — Servicese§ QO schools Start the Exercise [

You might also like