Variablename: Int Float
Variablename: Int Float
decimal type integer (no decimal!) higher decimal range lower decimal range
The range of the data type decides what you can do. Think of float and double as different types of planes. A single-aisle plane cant hold as many people as a jumbo jet. Decide what kind of data you will need to store before choosing a data type, otherwise you will use up more memory and slow down the computer. -----------------char char single character or number of characters
-----------------------------------------Declaring Variables -----------------------------------------Datatype variableName; Ex. int score; float GPA; //This is known as a declaration statement.
double salary; //Large number need lots of space. char MI; //Only need one character for a middle initial.
char city[25]; /*Maximum 24 characters in the city name. The 25th character is the string terminating character (like a period). \0 is the string terminating character. So Hoboken would be written as H o b o k e n \0. 0 as in ZERO! This will be known as an array of characters (Such as the Yankees being a team of baseball players). */ You can also do int scores[20]; for an array of integers, however this will not need an extra character for the period when using a number. Placing a number inside the brackets allows you to choose the amount of space. When you declare and initialize at the same time you do not need to decide size. Leave the brackets empty. But make sure to put around the name! You can declare more than one variable in the same statement provided they are the same data type. ------------------------------------Rules to Naming Variables ------------------------------------Variables are also known as identifiers, as they identify a particular item in the program. The variable name must start with an alphabet letter or an underscore. Ex. name or _name. o However these two names are different variables. Variable names are cAse sensITivE. For sake of readability, people use those camelHumps! (Well its good practice) There should be no spaces between the characters of the variable name. Variables can NOT be keywords or special characters. Try to give names that are self-explanatory. So lastName would be someones last name, not someones first name.
--------------------------------------Declaring Constants --------------------------------------Say you want a variable to always have the same value. To do this, declare a constant, so you dont end up changing it by mistake or anything. If you try to change it you will get a syntax error. Syntax: const dataType variableName = valueOfVariable Ex: const float pi = 3.14; const int dozen = 12;
Anytime you declare a constant it must be declared in one statement. ------------------------------------Assignment Statement ------------------------------------int a; a = 65 int x = 25 //Declaration statement //Assignment statement //Declaration and Assignment Statement. Declares a variable and assign a value.
When using an assignment statement, you assign a value to a previously declared variable. = is known as an assignment operator. ------------------------------------------------------Example program: Calculating Gross Pay ------------------------------------------------------#include <iostream> using namespace std; int main () { int hours; float grossPay, payRate; cout << Enter the number of hours worked: ; cin >> hours; cout << Enter the pay rate: ; cin >> payRate; grossPay = hours * payRate; //This is known as an expression or equation or formula.
cout << Your gross pay is << grossPay; cin.get(); //The example in class actually used system(PAUSE). And did not have the cin.stuff. cin.ignore(); return 0; }
Anything written between the two curly braces is part of the body of the program, or a block of statement. -------------------------------------------Arithmetic Operators (yeah no reallyheywe didnt cover precedence?!) -------------------------------------------+ * / % addition subtraction multiplication division modulus operator (remainder division)
When dividing integers like 7/2 the integer does not store the decimal! So 7/2 would equal 3, instead of 3.5. If it was a float value then you would get 3.5. But if you did 7%2 though, you would get 1 as your answer, as 1 is the remainder when you divide 7 by 2. ---------------------------------------------Expressions, Equations, Formulas ----------------------------------------------int a=12, b=16; int c; c=a + b; ----------A mixed expression would combine several types of numbers. Say the mixed expression was 7/2 + 35.5 3 + 35.5 3.0 + 35.5 = 38.5 //Assuming the answer was stored as a float or something //Where 7/2 was an integer and 35.5 was not an integer. //This is an integer expression, because its numbers are made up of integers.
Say c = a + b. a and b would be operands. Binary operator: Needs two operands. Unary operator: Only one operand.