CC 101 - Basics in C++
CC 101 - Basics in C++
What is an Identifier?
An identifier is the name to denote labels, types, variables, What is an Expression in C++?
constants or functions, in a C++ program. An expression is a valid arrangement of variables, constants,
C++ is a case-sensitive language. and operators.
Work is not work In C++, each expression can be evaluated to compute a
value of a given type
In C++, an expression can be:
Identifiers should be descriptive using meaningful identifiers A variable or a constant (count, 100)
is a good programming practice An operation (a + b, a * 2)
Function call (getRectangleArea(2, 4))
Rules in Naming Identifiers
Assignment Operator
Identifiers must be unique An operator to give (assign) a value to a variable.
Identifiers cannot be reserved words (keywords) Denote as ‘=‘
double main return Only variable can be on the left side.
Identifier must start with a letter or underscore, and be An expression is on the right side.
followed by zero or more letters (A-Z, a-z), digits (0-9), or Variables keep their assigned values until changed by
underscores another assignment statement or by reading in a new value.
VALID
age_of_dog _taxRateY2K Assignment Operator Syntax
PrintHeading ageOfHorse
NOT VALID Variable = Expression
age# 2000TaxRate Age-Of-Dog main First, expression on right is evaluated.
Then the resulting value is stored in the memory
Primitive Data Types in C++ location of Variable on left.
Integral Types NOTE: An automatic type coercion occurs after evaluation but before
represent whole numbers and their negatives the value is stored if the types differ for Expression and Variable
declared as int, short, or long
Character Types Input and Output
represent single characters
declared as char C++ treats input and output as a stream of characters.
Stored by ASCII values stream: sequence of characters (printable or nonprintable)
Boolean Type The functions to allow standard I/O are in iostream header
declared as bool file or iostream.h.
has only 2 values true/false
Thus, we start every program with
will not print out directly
#include <iostream>
Floating Types using namespace std;
represent real numbers with a decimal point
declared as float, or double Include Directives and Namespaces
Scientific notation where e (or E) stand for “times 10 to
the ” (.55-e6)
include: directive copies that file into your program
namespace: a collection of names and their definitions.
Samples of C++ Data Values
Allows different namespaces to use the same names without
confusion
int sample values
4578 -4578 0
Insertion Operator ( << )
bool values
true false Variable cout is predefined to denote an output stream that
goes to the standard output device (display screen).
float sample values The insertion operator << called “put to” takes 2 operands.
95.274 95.0 .265 The left operand is a stream expression, such as cout. The
right operand is an expression of simple type or a string
char sample values constant.
‘B’ ‘d’ ‘4’ ‘?’ ‘*’
Output Statements
What is a Variable?
SYNTAX
A variable is a memory address where data can be stored cout << Expression << Expression . . . ;
and changed.
cout statements can be linked together using << operator.
Declaring a variable means specifying both its name and its These examples yield the same output:
data type. cout << “The answer is “ ;
cout << 3 * 4 ;
What Does a Variable Declaration Do?
A declaration tells the compiler to allocate enough memory cout << “The answer is “ << 3 * 4 ;
to hold a value of this data type, and to associate the
identifier with this location. Output Statements (String constant)
Variable Declaration String constants (in double quotes) are to be printed as is,
without the quotes.
All variables must declared before use. cout<<“Enter the number of candy bars ”;
At the top of the program OUTPUT: Enter the number of candy bars
Just before use. “Enter the number of candy bars ” is called a prompt.
Commas are used to separate identifiers of the same type. All user inputs must be preceded by a prompt to tell the user
int count, age; what is expected.
Variables can be initialized to a starting value when they are You must insert spaces inside the quotes if you want them in
declared the output.
int count = 0; Do not put a string in quotes on multiple lines.
int age, count = 0;
Output Statements (Expression)
All expressions are computed and then outputted.
cout << “The answer is ” << 3 * 4 ;
OUTPUT: The answer is 12 Mixed type expressions: Both must be int to return int,
Escape Sequences otherwise float.
Type compatibilities (Implicit Conversion)
The backslash is called the escape character.
It tells the compiler that the next character is “escaping” its The compiler tries to be value-preserving.
typical definition and is using its secondary definition. General rule: promote up to the first type that can contain the
Examples: value of the expression.
new line: \n Note that representation doesn’t change but values can be
horizontal tab: \t altered .
backslash: \\ Promotes to the smallest type that can hold both values.
double quote \” If assign float to int will truncate
EXPRESSION VALUE
isSenior && hasFever false
isSenior || hasFever true
!isSenior true
!hasFever false
Precedence Chart
Highest
++, --, !, - (unary minus), + (unary plus)
*, /, %
+ (addition), - (subtraction)
<<, >>
<, <=, >, >=
==, !=
&&
||
=
Lowest
Program Style
Indenting: