Programming Language I
First 2022/2023
Computation
(input) data Code (output)
data
• Input: from keyboard, files, other input devices, other
programs, other parts of a program
• Computation: what our program will do with the input
to produce the output.
• Output: to screen, files, other output devices, other
programs, other parts of a program
The Language of a Computer (cont’d.)
The Language of a Computer (cont'd.)
• ASCII (American Standard Code for Information
Interchange)
– 128 characters
– A is encoded as 1000001 (66th character)
– 3 is encoded as 0110011
ASCII
C++
C, C++, Java, and C# are very similar. C++
evolved from C. Java was modeled after C++.
C# is a subset of C++ with some features
similar to Java. If you know one of these
languages, it is easy to learn the others.
Processing a C++ Program (cont'd.)
• To execute a C++ program:
– Use an editor to create a source program in
C++
– Preprocessor directives begin with # and are
processed by a the preprocessor
– Use the compiler to:
• Indicates something that must be fixed
before the code can be compiled.
• Translate into machine language (object
program)
Processing a C++ Program (cont'd.)
• To execute a C++ program (cont'd.):
– Linker:
• Combines object program with other programs provided by the SDK to
create executable code
– Loader:
• Loads executable program into main memory
– The last step is to execute the program
Processing a C++ Program (cont'd.)
Source code (developed by the programmer)
Create/Modify Source Code
#include <iostream>
using namespace std;
Creating, int main()
{
// Display Welcome to C++ to the console
Saved on the disk
Compiling,
cout << "Welcome to C++!" << endl; Source Code
return 0;
}
and Running Compiler
If compilation errors
Programs stored on the disk
An object file (e.g., [Link]) is created.
Machine Code
program
Linker
stored on the disk
An executable file (e.g., [Link]) is created.
Executable Code
Run Executable Code
e.g., Welcome
Result
If runtime errors or incorrect result
Processing a C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "My first C++ program." <<
endl;
return 0;
}
Sample Run:
My first C++ program.
Hello World
// Hello World program comment
#include <iostream.h> Allows access to
an I/O library
int main() {
Starts definition of special
function main()
cout << "Hello World\n";
output (print) a
return 0; string
}
Program returns a
status code (0 means
OK)
Basic Elements of C++
Comments
• Comments contain text that is not converted to machine
language (it's just there for humans).
• Everything after "//" is ignored by the compiler.
• Everything between "/*" and "*/" is ignored.
Comment Example
Two types:
– Single line
// This is a C++ program. It prints the
sentence:
// Welcome to C++ Programming.
– Multiple line
/*
This is a C++ program. It prints the
sentence:
Welcome to C++ Programming.
*/
C++ Preprocessor
• C++ Compilers automatically invoke a preprocessor that
takes care of #include statements and some other special
directives.
• You don't need to do anything special to run the
preprocessor - it happens automatically.
Some common includes
• Basic I/O: iostream, iostream.h
• I/O manipulation: iomanip, iomanip.h
• Standard Library: stdlib.h
Variables
• The program now uses variables:
int integer1, integer2, sum;
• Variables are just names for locations in
memory.
• In C++ all variables must have a type.
• In C++ all variables must be declared
before they can be used.
Variables (cont.)
• C++ variables are declared like this:
type var_name;
• type indicates what kind of variable.
• C++ built in types include:
int char float double bool
Variable Names
• C++ variable names:
– made up of letters, digits and underscore.
– Must start with a non-digit.
– Case sensitive
• first is not the same name as First
• Can be any length
• Good variable names tell the reader what the variable is
used for!
Names
• A name in a C++ program
– Starts with a letter, contains letters, digits, and
underscores (only)
• x, number_of_elements, Fourier_transform, z2
• Not names:
– 12x
– time!to!market
– main line
– Users can't define names that are taken as keywords
• E.g.:
– int
– if
– while
– double
Reserved Words (Keywords)
• C++ keywords
– Cannot be used as variable names
Examples of C++ Keywords
int double float char const
case continue break bool static_cast
if else switch default return
short long do while for
Allocating Memory with Constants and Variables
• Named constant: memory location whose content can’t
change during execution
• The syntax to declare a named constant is:
• In C++, const is a reserved word
Allocating Memory with Constants and Variables
(cont'd.)
• Variable: memory location whose content may change
during execution
• The syntax to declare a named constant is:
Assignment Statement
Declaring & Initializing Variables
• Variables can be initialized when declared:
int first=13, second=10;
char ch=' ';
double x=12.6;
• All variables must be initialized before they are used
Input (Read) Statement
• cin is used with >> to gather input
• The stream extraction operator is >>
• For example, if miles is a double variable
cin >> miles;
– The computer will get a value of type double
and places it in the variable miles.
Input (Read) Statement (cont'd.)
• Using more than one variable in cin
allows more than one value to be read at a
time
• For example, if feet and inches are
variables of type int, a statement such
as:
cin >> feet >> inches;
– Inputs two integers from the keyboard
– Places them in variables feet and inches
respectively
Input (Read) Statement (cont'd.)
Variable Initialization
• There are two ways to initialize a variable:
int feet;
– By using the assignment statement
feet = 35;
– By using a read statement
cin >> feet;
Increment and Decrement Operators
• Increment operator: increment variable by 1
– Pre-increment: ++variable
– Post-increment: variable++
• Decrement operator: decrement variable by 1
– Pre-decrement: --variable
– Post-decrement: variable—
• What is the difference between the following?
x = 5; x = 5;
y = ++x; y = x++;
More on Assignment Statements
• C++ has special assignment statements called compound
assignments
+=, -=, *=, /=, and %=
• Example:
x *= y;
Assignment and increment
a:
// changing the value of a variable
int a = 7; // a variable of type int called a 7
// initialized to the integer value 7
a = 9; // assignment: now change a's value to 9
9
18
a = a+a; // assignment: now double a's value
20
a += 2; // increment a's value by 2
21
++a; // increment a's value (by 1)
Output
• The syntax of cout and << is:
– Called an output statement
• The stream insertion operator is <<
• Expression evaluated and its value is
printed at the current cursor position on
the screen
Output (cont'd.)
• A manipulator is used to format the output
– Example: endl causes insertion point to move to beginning of
next line
• Example:
Output (cont'd.)
• The new line character is '\n'
– May appear anywhere in the string
cout << "Hello there.";
cout << "My name is James.";
• Output:
Hello [Link] name is James.
cout << "Hello there.\n";
cout << "My name is James.";
• Output :
Hello there.
My name is James.
36
Form and Style
• Consider two ways of declaring variables:
– Method 1
int feet, inch;
double x, y;
– Method 2
int feet,inch;double x,y;
• Both are correct; however, the second is hard to read
Output (cont'd.)
1 // Example 1
2 // Addition program that displays the sum of two numbers.
3 #include <iostream> // allows program to perform input and output
4 using namespace std;
5 // function main begins program execution
6 int main() Declare integer variables
7 {
8 // variable declarations
9 int number1; // first integer to add
10 int number2; // second integer to add
11 int sum; // sum of number1 and number2
Use stream operator with
12 standard input stream to
13 cout << "Enter first integer: "; // prompt user for data obtain user input
14 cin >> number1; // read first integer from user into number1
15
16 cout << "Enter second integer: "; // prompt user for data
17 cin >> number2; // read second integer from user into number2 Stream manipulator
18
endl outputs a newline
19 sum = number1 + number2; // add the numbers; store result in sum
20
21 cout << "Sum is " << sum << endl; // display sum; end line
22
23 return 0; // indicate that program ended successfully
24 Output
25 } // end function main
Enter first integer: 45
Enter second integer: 72
Sum is 117
Another C++ Program
//
// C++
C++ Addition
Addition of
of integers
integers
#include
#include <iostream.h>
<iostream.h>
int
int main()
main() {{
int
int integer1,
integer1, integer2,
integer2, sum;
sum;
cout
cout <<
<< "Enter
"Enter first
first integer\n";
integer\n";
cin
cin >>
>> integer1;
integer1;
cout
cout <<
<< "Enter
"Enter second
second integer\n";
integer\n";
cin
cin >>
>> integer2;
integer2;
sum
sum == integer1
integer1 ++ integer2;
integer2;
cout
cout <<
<< "Sum
"Sum is
is "" <<
<< sum
sum <<
<< endl;
endl;
return
return 0;
0;
}}
Types and literals
• Built-in types
– Boolean type • Boolean literals
• bool – true false
– Character types • Character literals
• char – 'a', 'x', '4', '\n', '$'
– Integer types • Integer literals
• Int – 0, 1, 123, -6, 245, -698
– Floating-point types • Floating point literals
• double – 1.2, 13.345, .3, -0.54
– and float
Expressions
• C++ expressions are used to express computation.
• Expressions include operations and the operands on
which the operations are applied.
• Operands can be variables, literals or function calls.
Precedence
Operators Precedence
() highest (applied
first)
* / %
+ -
< <= > >=
== !=
=
lowest (applied last)
Precedence
• Precedence controls the order of
evaluation of operators.
– A high precedence means an operator is
evaluated (applied) before any lower
precedence operators.
• Operators that have the same precedence
can happen in either order, but in C++ the
one on the left is evaluated first.
Arithmetic Operators and Operator Precedence
• C++ arithmetic operators:
– + addition
– - subtraction
– * multiplication
– / division
– % modulus operator
• +, -, *, and / can be used with integral and floating-point
data types
• Operators can be unary or binary
Order of Precedence
• All operations inside of () are evaluated
first
• *, /, and % are at the same level of
precedence and are evaluated next
• + and – have the same level of
precedence and are evaluated last
• When operators are on the same level
– Performed from left to right (associativity)
• 3 * 7 - 6 + 2 * 5 / 4 + 6 means
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6
Expressions
• If all operands are integers
– Expression is called an integral expression
• Yields an integral result
• Example: 2 + 3 * 5
• If all operands are floating-point
– Expression is called a floating-point
expression
• Yields a floating-point result
• Example: 12.8 * 17.5 - 34.50
Mixed Expressions
• Mixed expression:
– Has operands of different data types
– Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
Mixed Expressions (cont'd.)
• Evaluation rules:
– If operator has same types of operands
• Evaluated according to the type of the operands
– If operator has both types of operands
• Integer is changed to floating-point
• Operator is evaluated
• Result is floating-point
– Entire expression is evaluated according to precedence rules
04/19/2025 49
Precedence Evaluation
What is the value of the expression at the bottom of the
screen?
42
32
0 32
0 4
3 30 8
( 1 + 2 ) % 3 * 4 + 5 * 6 / 7 * ( 8 % 9 ) + 10
Type Conversion (Casting)
• Implicit type coercion: when value of one type is
automatically changed to another type
• Cast operator: provides explicit type conversion
static_cast<dataTypeName>(expression)
Type Conversion (cont'd.)
Relational and Equality Operators
• Relational and Equality operators are
used to compare values:
• Relational Operators:
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
• Equality Operators:
== Equal to
!= Not Equal to
Syntax
• Errors in syntax are found in compilation
int x; //Line 1
int y //Line 2: error
double z; //Line 3
y = w + x; //Line 4: error
Another Program
#include <iostream.h>
Using namespace std;
int main() {
double fahr,celcius;
cout << "Enter Temperature in Fahrenheit\n";
cin >> fahr;
celcius = (fahr - 32.0)*5/9;
cout << fahr << " fahrenheit is " << celcius <<
" Celcius" << endl;
return 0;
}
Math Operator Quiz
What are the values printed?
const int five = 5;
int i = 7;
float x = 7.0;
cout << five + i/2 << endl;
cout << five + x/2 << endl;
04/19/2025 56
cin and the Extraction Operator >>
• The syntax of an input statement using cin and the
extraction operator >> is:
• The extraction operator >> is binary
– Left-side operand is an input stream variable
• Example: cin
– Right-side operand is a variable
Output and Formatting Output
• Syntax of cout when used with <<
• Expression is evaluated
• Value is printed
• Manipulator is used to format the output
– Example: cout<< endl;