Lecture 01- Introduction to c++ Programming spring 2025
Lecture 01- Introduction to c++ Programming spring 2025
Introduction
Lecture 1
Spring 2025
• Document each variable declaration with a comment telling what the variable is used for
• Document each segment of code with a comment telling what the segment does.
4
CS1004 - Spring 2025
Structure of a C++ Program
• A C++ program is a collection of definitions and declarations:
• data type definitions
• class definitions
5. Load ..
..
Primary
Memory
6. Execute CPU CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
..
executes.
..
..
CS1004 - Spring 2025 11
C++ Compilation Process
• Compiler compiles a C/C++ program into
executable in 4 steps as shown in the diagram.
For example, a "gcc -o hello.exe hello.c" is
carried out as follows:
• Pre-processing: via the GNU C Preprocessor
(cpp.exe), which includes the headers
(#include) and expands the macros (#define).
• The resultant intermediate file "hello.i"
contains the expanded source code.
• Compilation: The compiler compiles the pre-
processed source code into assembly code for
a specific processor.
• The resultant assembly file is "hello.s".
• Assembly: The assembler (as.exe) converts the
assembly code
into machine code in the object file "hello.o".
• Linker: Finally, the linker (ld.exe) links the
object code with the library code to produce
an executable file "hello.exe".
CS1004 - Spring 2025 12
C++ Programming Examples
• <<
• Stream insertion operator
• Value to the right of the operator (right operand) inserted into output stream
(which is connected to the screen)
• std::cout << “Welcome to C++!\n”;
• \
• Escape character
• Indicates that a “special” character is to be output
3 #include <iostream>
5 int main()
6 {
11 }
Welcome to C++!
• Variables
• Location in memory where a value can be stored for use by a program
• Must be declared with a name and a data type before they can be used
• char – characters
• = (assignment operator)
• Assigns value to a variable
• Binary operator (has two operands)
• Example:
sum = variable1 + variable2;
• To use the following functions without the std:: prefix, write the
following at the top of the program
using std::cout;
using std::cin;
using std::endl;
37
39 }
1900 2135
1950 2235
2000 2335
2050 2435
• If the expression’s value is equal to the value after any of the case labels, the
statements after the matching case label are executed.
• If NO matching value can be found and a default label exists, the statements
after the default label are executed instead.
#include <iostream>
using namespace std;
int main() {
int x = //enter 1-3;
switch (x) {
case 1:
case 2:
case 3:
cout << "Matched case 1, 2, or 3" << endl;
break;
default:
cout << "Default case" << endl;
}
return 0;
}
• The while and for statements perform the action (or group of actions) in their
bodies zero or more times.
• The do…while statement performs the action (or group of actions) in its body at
least once.
• Eventually, the condition will become false, the repetition will terminate, and
the first pseudocode statement after the repetition statement will execute.
• When the following while repetition statement finishes executing, product contains
the result:
int product = 3;
• After the statement has been executed, the do-while loop checks the condition. If
the condition evaluates to true, the path of execution jumps back to the top of
the do-while loop and executes it again.
do
while (condition);
i = 0, j=0
i = 0, j=1
i = 0, j=2
i = 0, j=3
i = 0, j=4
i = 1, j=0
i = 1, j=1
i = 1, j=2
i = 1, j=3
i = 1, j=4
i = 2, j=0
i = 2, j=1
i = 2, j=2
i = 2, j=3
i = 2, j=4
i = 3, j=0
i = 3, j=1
i = 3, j=2
i = 3, j=3
CS1004 - Spring 2025 44
i = 3, j=4
Control Structures (For Statement)
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 0, j = 3
i = 0, j = 4
i = 1, j = 0
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 1, j = 4
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
Exited nested loops.
Processing i = 0…
Processing i = 1…
Processing i = 2…
Processing i = 3…
Processing i = 4…
Processing i = 0…
Processing i = 1…
Skipped i = 2
Processing i = 3…
Processing i = 4…
#include <iostream>
using namespace std;
Output:
int main() { Processing i = 0…
for (int i = 0; i < 5; ++i) { Processing i = 1…
if (i == 2) { Skipped i = 2
cout << "Skipped i = " << i << endl; Processing i = 3…
continue; // Skip the rest of the loop for `i = 2` Processing i = 4…
}
cout << "Processing i = " << i << endl;
}
return 0;
}