L01 C++Preliminaries Fall2023
L01 C++Preliminaries Fall2023
CUFE
Dr. Lydia Wahid
Acknowledgment
These slides have been created by
Prof. Magda Fayek
Agenda
Basic Parts of a Program (in C++)
Program Life cycle and IDE
C++ Basics
Basic Parts of Memory dedicated to a
program
Example 1: Program Hello World
# include <iostream>
using namespace std;
int main( )
{
……//Code statements
return 0; // Indicates normal termination.
}
Program life cycle
Text Editor Program Source file
(High Level Language)
e.g. C++
Compiler
(Translating Program)
Linker
Compiler
Integrated+ Development
Editor + Debugger
Environment
+ Linker
IDE
IDE includes:
Text Editor to write your code
Compiler to produce object code
Linker to link your object code files and
produce the final .exe file.
Debugger (optional) to find logical errors in
your code.
Additions: Help, Library
Example IDEs: Visual Studio, Eclipse, Net
Beans, … etc.
Steps to Produce .exe
Source compiler
Object
File File
Linker
Executable
int main ()
{ int y=1;
{
int x=3;
y=x+4;
}
int x=2; ?Does this line cause an error
}
Prepared by Dr. Lydia Wahid
Variables: Global Scope
• Global variables are defined outside of all the functions,
usually on top of the program. The global variables will
hold their value throughout the lifetime of your program.
Example:
#include <iostream>
using namespace std; // allows using names cin, cout
int g; // global variable
// here we know g
int main()
{
…. // here we know g
}
Data Types in C++
Data Type
char
short
int
float
double
bool
Operators in C++
Mathematical
Postfix and Prefix
Logical
Comparison
Input/Output
Mathematical Operations in C++
a = a + b; a += b;
a = a - b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;
Postfix and Prefix Operations
• int n=0, m=0;
• n = ++m;
• cout << n << m;
Output : 1 1
Output : 0 1
Logical Operations in C++
a = a | b; a |= b;
a = a & b; a &= b;
a = a ^ b; a ^= b;
a = a << b; a <<= b;
a = a >> b; a >>= b;
Comparison Operations in C++
a < b; a <= b;
a > b; a >= b;
a == b; a != b;
Input and Output Operators
#include <iostream>
using namespace std; // allows using names cin, cout
int main()
{
int a;
cout << “Enter a value for a: “ << endl;
cin >>a;
cout << “You Entered: “ << a << endl;
}
If statement
Switch Case
Conditional Loops
Iterative Loops
IF Statements
S2
if (condition) if (condition)
x=y + z;
y++; { x=y + z;
else y+
x=y - z; +; }
y--; else
{ x=y - z;
Note
A condition can be ANDed or ORed with
other condition(s). Examples:
C1 && C2
C1 || C2
C1 && (C2 || C3)
Note: && have higher precedence than || if
no parentheses are used
While statement
True False
while (condition) condition
{
S1; S1
S2
}
S2;
While Example
int i=1;
while (i < 10)
{
i ++;
}
What is the value of variable i after finishing the while loop?
False
do True
condition
{
S1; S2
} while (condition) ;
S2;
Do While Example
#include <iostream>
using namespace std;
int main()
{
int n;
do {
cout << "Enter Any number (0 to end): ";
cin >> n;
cout << "You entered: " << n << ‘\n’;
} while (n != 0);
}
S2;
False
S2
For Loop Example
int n = 10;
int x=0;
for (int i = 0; i < n; i++)
{
x=x+1;
}