Lecture 02
Lecture 02
Programming
Syntax:
int main()
{
... code ....
return 0;
}
Blocks
• Blocks are the group of statements that are enclosed within { } braces.
They define the scope of the identifiers and are generally used to
enclose the body of functions and control statements.
• The body of the main function is enclosed within { }.
• Syntax:
{ // Body of the Function return 0; }
7
Semicolon symbol
• As you may have noticed by now, each statement in the above code is
followed by a ( ; ) semicolon symbol. It is used to terminate each line
of the statement of the program. When the compiler sees this
semicolon, it terminates the operation of that line and moves to the
next line.
• Syntax:
any_statement ;
8
Basic Data Types
Data Type Size Description
9
Examples
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
10
Variable Declaration
• A variable is a memory address where data can be stored and
changed.
• Declaring a variable means specifying both its name and its data type.
• Syntax
data_type Variable_Name ;
11
What Does a Variable Declaration
Do?
• A declaration tells the compiler to allocate enough memory to hold a value of
this data type, and to associate the identifier with this location.
int ageOfDog;
char middleInitial;
float taxRate;
Variable Declaration
• All variables must declared before use.
• At the top of the program
• Just before use.
• Commas are used to separate identifiers of the same type.
int count, age;
• Variables can be initialized to a starting value when they are declared
int count = 0;
int age, count = 0;
Variable Initialization
• Syntax
data_type variable_name = value;
14
Basic Output cout
We used the cout method which is the basic output method in C++ to
output the sum of two numbers.
• Syntax:
cout << result << endl;
15
What is an Identifier?
• An identifier is the name to denote labels, types,
variables, constants or functions, in a C++
program.
• C++ is a case-sensitive language.
• Work is not work
executing
Keyboard program Screen
cin cout
cin >> x;
cin >> y;
b 2 4ac b *b 4* a *c
x( y z ) x * ( y z)
1
2 1 /( x * x x 3)
x x 3
a b ( a b) /( c d )
c d
Type compatibilities
• C++ allows us to convert data of one type to that of another. This is
known as type conversion.
• There are two types of type conversion in C++:
• Implicit Conversion
• Explicit Conversion (also known as Type Casting)
Type compatibilities (Implicit
Conversion)
• The type conversion that is automatically done by the compiler is known as implicit type
conversion. This type of conversion is also known as automatic conversion. It can convert
lower data type to higher data type.
• All short and char can be convert into integer. But int can’t be converted into char and short.
• Example
int x= ‘z’; Valid
char x = 2678; Invalid
• Promotes to the smallest type that can hold both values.
• If assign float to int will truncate
int_variable = 2.99; // results in 2 being stored in int_variable
• If assign int to float will promote to double:
double dvar = 2; // results in 2.0 being stored in dvar
Type compatibilities (Explicit
Conversion)
• Casting - forcing conversion by putting (type) in front of variable or expression. Used to insure
that result is of desired type. Used for converting higher data type to lower
• Example: If you want to divide two integers and get a real result you must cast one to double so
that a real divide occurs and store the result in a double.
int r;
r = 5/2; not valid
r = (float) 5/2; valid
e.g. x= int(7.5) covert it into 7. truncate 0.5.
Simple Flow of Control
• Three processes a computer can do:
• Sequential
expressions, insertion and extraction operations
• Selection (Branching)
if statement, switch statement
• Repetition/Iteration (Loop)
while loop, do-while loop, for loop
bool Data Type
• Type bool is a built-in type consisting of just 2 values, the constants
true and false
• We can declare variables of type bool
bool hasFever; // true if has high temperature
bool isSenior; // true if age is at least 55
• The value 0 represents false
• ANY non-zero value represents true
Boolean Expression
• Expression that yields bool result
• Include:
6 Relational Operators
< <= > >= == !=
3 Logical Operators
! && ||
Relational Operators
are used in boolean expressions of form:
ExpressionA Operator ExpressionB
temperature > humidity
B * B - 4.0 * A * C > 0.0
abs (number) == 35
initial != ‘Q’
• Notes:
o == (equivalency) is NOT = (assignment)
o characters are compared alphabetically. However, lowercase letters are higher ASCII value.
o An integer variable can be assigned the result of a logical expression
Relational Operators
int x, y ;
x = 4;
y = 6;
EXPRESSION VALUE
x<y true
x+2<y false
x != y true
x + 3 >= y true
y == x false
y == x+2 true
y=x+3 7
y=x<3 0
y=x>3 1
Logical Operators
are used in boolean expressions of form:
ExpressionA Operator ExpressionB
A || B (true if either A or B or both are true. It is false otherwise)
A && B (true if both A and B are true. It is false otherwise)
Notes:
Highest precedence for NOT, AND and OR are low precedence.
Associate left to right with low precedence. Use parenthesis to override priority or for clarification
• x && y || z will evaluate “x && y ” first
• x && (y || z) will evaluate “y || z” first
Logical Operators
int age ;
bool isSenior, hasFever ;
float temperature ;
age = 20;
temperature = 102.0 ;
isSenior = (age >= 55) ; // isSenior is false
hasFever = (temperature > 98.6) ; // hasFever is true
EXPRESSION VALUE
isSenior && hasFever false
isSenior || hasFever true
!isSenior true
!hasFever false
Precedence Chart
• ++, --, ! Highest
• *, /, %
• + (addition), - (subtraction)
• <<, >>
• <, <=, >, >=
• ==, !=
• &&
• ||
•= Lowest
Boolean Expression (examples)
taxRate is over 25% and income is less than $20000
age is 21 or 22
Boolean Expression (examples)
(taxRate > .25) && (income < 20000)
• Clarity: Tells the user the significance of the number. There may be
the number 0.08 elsewhere in the program, but you know that it
doesn’t stand for TAXRATE.
• Maintainability. Allows the program to be modified easily.
• Ex: Program tax compute has const double TAXRATE=0.0725. If taxes rise to
8%, programmer only has to change the one line to const double
TAXRATE=0.08
• Safety: Cannot be altered during program execution
Summary:
Second week content, I forget to mention lecture content
yesterday, so that I mentioned here as whole.
C++ Header Files, Standard library header files, User-defined
header files, Main Function, Structure of C++, Data Types, What
Does a Variable Declaration Do?, What is an Identifier? ,
Expression in C++?, Assignment Operator, Operators in C++,
Keyboard and Screen I/O, Output Statements (String constant),
Escape Sequences, Arithmetic Expressions