CSC 283
CSC 283
Data Types
Constants , Variables
& Operators
2
Structure of a C++ Program
return type function name argument list
function body
3
Data Types
Type Size (Bytes) Format Specifier Value Range
char 1 %c 0 to 255
(or -128 to 127)
int 4 %d -32,768 to 32,767
(-2^15 to 2^15-
1)
float 4 %f 1.2E-38 to 3.4E+38
(6 decimal places)
double 8 %lf 2.3E-308 to 1.7E+308
(15 decimal places)
4
Data Types (Cont.)
Data Types
double
5
Reserved Words and Identifiers
Reserve Words
•Word that has a
specific meaning in C++ Identifiers
•Known as keyword •Word used to name and refer
•Ex: int, retrun, main to a data element
•Ex: functions, variables
6
Valid Identifiers
Consists of letters, digits, or underscores only
01 ✔ myName_123
× myName#
Begins with a letter or underscore symbol
02 ✔ myName, MyName, _myName
× 123_MyName
Cannot be a C reserved word
03 × main, for
✔ mainName
04 At least one letter or underscore, should not be empty
Constants
Variables
•A value that can not be
•Is a memory location where
altered throughout the
a value can be stored
program
•Value can change
•Ex: 1, 2, 3
during program
#define PI 3.1416
execution
•Ex: int a = 3;
8
Symbolic Constants
□ In C++, symbolic constants are identifiers that
represent constant values throughout the program.
These constants remain unchanged during the
execution of the program.
Syntax Syntax
10
Dynamic Initialization of Variables
□ In C++, Dynamic initialization refers to the process
of initializing variables during runtime rather than at
compile time.
#include <iostream> using
namespace std;
return 0;
}
In this example, a, b, and sum are dynamically initialized
based on user input at runtime. 11
Dynamic Initialization of Variables
□ In C++ a reference variable is an alias for an existing variable. Once a
reference is initialized with a variable, it becomes an alternative name for
that variable. Any changes made to the reference will affect the original
variable.
#include <iostream> using namespace std;
cout << "x = " << x << endl; // Output: x = 10 cout << "ref = " << ref <<
endl; // Output: ref = 10
ref = 20; // Changing ref changes x cout << "After modifying ref:" << endl;
cout << "x = " << x << endl; // Output: x = 20 cout << "ref = " << ref <<
endl; // Output: ref = 20
return 0;
}
12
Reference Variables
□ A reference in C++ is an alias for another variable. Once a reference is
initialized with a variable, it becomes an alternative name for that variable,
meaning any operation performed on the reference is actually performed
on the original variable.
#include <iostream>
using namespace
std;
Syntax
int main() { int
a = 10;
int &ref =
a; // ref is a
reference to
the variable a
ref = 20; //
Modifies a 13
through ref
Memory Management Operators
□ In C++ provides two key memory management operators for dynamically
allocating and deallocating memory during program execution: new and
delete. These operators allow you to manage memory manually, which is
crucial for efficient use of resources, especially when dealing with large
Syntax amounts of data. #include <iostream> using namespace std;
int main() {
int *ptr = new int(5); // Allocate memory and initialize to 5 cout << "Value: "
<< *ptr << endl; // Output: 5
delete ptr; // Deallocate memory
Type cast
operator
15
C++ Operators
Arithmatic Bitwise
+, -, *, /, % &, |, ~, ^, <<, >>
Relational Assignments
==, !=, <, <=, >, >= =, +=, -=, *=, /=
Logical Special
!, &&, || ++, --, ?, &, *, [], ., ->
16
Scope Resolution Operators
□ The scope resolution operator (::) in C++ is used to define and access
identifiers (such as variables, functions, or classes) that are located in
different scopes. It helps resolve ambiguity when the same name is used in
different scopes (global, class, or namespace).
Accessing the global
Variable
Syntax
#include<iostream> using namespace std;
#include<iostream>
int main() {
std::cout << "first::x = " << first::x << std::endl; // Accessing x from namespace first
std::cout << "second::x = " << second::x << std::endl; // Accessing x from namespace second
return 0;
}
18
Control Structures
19
Selection Control Structures
I. if Statement
II. if-else Statement
III. else if Ladder
IV. switch Statement
20
21
The switch
22
23
Repetition(Loop) Control Structures
I. while
II. do-while
III. for
24
The For Loop
1. How many times the
loop body execute?
25
The Do-While Loop
26
Comparison of 3 loops
27