PIC10A Lecture 2

Reading

p6-p13

Identifiers

They are names of variables.

Format

  1. must start with either a letter or the underscore ( _ ) symbol
  2. all the rest of the characters must be letters, digits, or the underscore ( _ )  symbol.
  3. cannot be keywords or reserved words. (e.g. int, double, if, for, .....)

Keywords or reserved words

Exercises

Which of the following can be identifiers? If  it can't be an identifier, specific the reason.
(1) a (2) a2 (3)abc (4) nbc4 (5) 2go
(6) _abc (7) score (8)myFirstHomeworkScroe (9) a2b3c (10)A_2_B_3  
(11) 1010  (12) 3X (13) %change (14) change% (15) data-1
(16)int (17) www.ucla.edu (18) program.cpp (19) ucla_edu

Case-sensitive

Convention

Real Story (you can skip it, it won't be part of your midterm/exam)

When I was a TA, one of my student wrote the following program. Obvious he was a NBA fan.
#include<iostream>
using namespace std;

int main() {
int lakers, bulls, spurs;
double kings;
cin >> lakers >> bulls >> spurs;
kings = 0.25*lakers+0.1*bulls+0.05*spurs;
cout << kings << endl;
return 0;
}

Do you know what the program does? This is what he meant

#include<iostream>
using namespace std;

int main() {
    int numOfQuarters, numOfDimes, numOfNickels;
    double value;
    cin >> numOfQuarters >> numOfDimes >> numOfNickels;
    value = 0.25*numOfQuarters+0.1*numOfDimes+0.05*numOfNickels;
    cout << value << endl;
    return 0;
}


Variables

Variable declaration

Format

Syntax

Type_name variable_name_1, variable_name_2,......;

Example

int numOfQuarters, numOfDimes, numOfNickels;
double speed;


Types

Refer to Savitch for more details.
Type Name
Meaning
Memory Used
short
small integer
2 bytes
int
integer
4 bytes
long
large integer
4 bytes
float
decimal number
4 bytes
double
decimal number (more precious)
8 bytes
long double
decimal number (most precious)
10 bytes
char
characters
1 byte
bool
true/false
1 byte
(The meaning of  bytes will be discussed during the class.)

A program to find the size (won't be tested in the exams)

This program is used to find the sizes of different types.
#include<iostream>
using namespace std;

int main(){
    char ch = 'a';
    int i = 0;
    cout << "A char uses " << sizeof(ch) << " byte(s).\n";
    cout << "An int uses " << sizeof(i) << " byte(s).\n";
    return 0;
}

Modify the program to find the size of double.

Assignment Statements 

Syntax

Variable = Expression;

Example

distance = rate * time;
count = count + 2;
value = 0.25*numOfQuarters+0.1*numOfDimes+0.05*numOfNickels;

Look at the following program. Is there any error? If not, what is the output of the program
#include<iostream>
using namespace std;
int main() {
    int a;
    cout << a;
    return 0;
}

Initializing Variables in declaration

You can initialize a variable(that is, give it a value) at the time you declare the variable.

Syntax

Type_Name Variable_Name_1 = Expression_for_Value_1, Variable_Name_2 = Expression_for_value_2, ....;

Examples

int count = 0, limit = 10;
double distance = 32.5;

or

Syntax

Type_Name Variable_Name_1(Expression_for_Value_1), Variable_Name_2 (Expression_for_value_2), ....;

Examples

int count(0), limit(10);
double distance(32.5);


Example

#include<iostream>
using namespace std;

int main() {
    double quarterValue = 0.25, dimeValue = 0.1, nickelValue = 0.05;
    int numOfQuarters, numOfDimes, numOfNickels;
    double value;
    cout << "Input the number of quarters, dimes and nickels.\n";
    cin >> numOfQuarters >> numOfDimes >> numOfNickels;
    value = quarterValue*numOfQuarters
           + dimeValue*numOfDimes
           + nickelValue*numOfNickels;
    cout << "The value is " << value <<".\n";
 
    return 0;
}

Question

What happens if you write int a = 2.55 ?