Introduction to Programming Concepts
Introduction to Programming Concepts
DEFINITION:
using computers.
2. Flowcharting
3 Algorithm
1. Pseudocode:
A semiformal, English-like language used to design and describe the procedural
logic of an algorithm in a simple, easy-to-understand way
Designing algorithms
2. High-level languages:
They are not machine oriented: they are portable, meaning that a program
written for one machine will run on any other machine for which the
appropriate compiler or interpreter is available.
There are three main types of language translators: Assemblers, Compilers, and
Interpreters.
1. Compilers
The overall process typically involves five phases: Edit, Preprocess, Compile,
Link, and Load.
edi preprocess compile Link Loa
t d
#include <iostream>
using namespace std;
int main()
{
// The cout object is used to display output.
cout << "Hello, World!" << endl;
}
#include <iostream> : the C++ preprocessor to include the contents of the iostream library .
Tells
Identifiers: These are the names the programmer chooses for things like
variables,
Rules for Creating Identifiers
1. Valid Starting Characters: An identifier must begin with either a letter (A-Z or
a-z) or an
underscore(_).
2. Valid Subsequent Characters: After the first character, an identifier can contain
letters,
digits, or the underscore character.
3. No Reserved Keywords: Identifiers cannot be the same as any of the reserved
Example Validity Reason
keywords
studentName Valid Starts with a letter, contains letters.
in c++.
_temp_value Valid Starts with an underscore, contains letters and underscores.
4. No Spaces: Identifiers cannot contain any white space (spaces or tabs).
Starts with a letter, contains letters and a digit (not as the first
totalSum1 Valid
character).
1stPlace Invalid Starts with a digit (1).
for Invalid Is a reserved C++ keyword.
num Count Invalid Contains a space.
Comments: Lines of text that are included in the source code but are ignored by
the compiler. They are essential for providing notes, documentation,
and explanations for programmers to understand or maintain the code.
C++ supports two ways to insert comments:
o // line comment
o /* block comment */
Input and output ( cin and cout ): These are the primary mechanisms for interaction:
Cout (Console Output) is used to display information (text, numbers, results) to the
user's screen.
Ex. cout << "Hello"; // prints Hello on screen
cout << Hello; // prints the content of Hello variable on screen
Cin (Console Input): is used to read and store data that the user types into the
program.
Ex. cin >> age;
Fundamental Data Types in C++
1. Integer Types
These types are used to store whole numbers (numbers without a fractional
part)
// 1. Integer Types
cout << "Size of char: " << sizeof(char) << " byte(s)" << endl;
cout << "Size of short: " << sizeof(short) << " byte(s)" << endl;
cout << "Size of int: " << sizeof(int) << " byte(s)" << endl;
cout << "Size of long: " << sizeof(long) << " byte(s)" << endl;
cout << "Size of long long: " << sizeof(long long) << " byte(s)" << endl;
2. Floating-Point Types (Real
Numbers)
// 2. Floating-Point Types
cout << "Size of float: " << sizeof(float) << " byte(s)" << endl;
cout << "Size of double :" << sizeof(double) << " byte(s)" << endl;
cout << "Size of long double: " << sizeof(long double) << " byte(s)" << endl;
cout << "Size of bool: " << sizeof(bool) << " byte(s)" << endl;
//C++ program to demonstrate the 'sizeof' operator and display the memory size
// (in bytes) of several fundamental data types.
#include <iostream>
using namespace std;
int main()
{
cout << "--- Fundamental Data Type Sizes ---" << endl;
cout << "Size of char:\t\t" << sizeof(char) << " byte(s)" << endl;
cout << "Size of short:\t\t" << sizeof(short) << " byte(s)" << endl;
cout << "Size of int:\t\t" << sizeof(int) << " byte(s)" << endl;
cout << "Size of long:\t\t" << sizeof(long) << " byte(s)" << endl;
cout << "Size of long long:\t" << sizeof(long long) << " byte(s)" << endl;
cout << "Size of float:\t\t" << sizeof(float) << " byte(s)" << endl;
cout << "Size of double:\t\t" << sizeof(double) << " byte(s)" << endl;
cout << "Size of long double:\t" << sizeof(long double) << " byte(s)" << endl;
cout << "Size of bool:\t\t" << sizeof(bool) << " byte(s)" << endl;
return 0;
}
1. Variable Declaration
Declaration is the act of telling the compiler two things: the name of the
variable and the data type of the data it will hold.
Syntax:
int num1;
int score;
2. Variable Initialization
Initialization is the act of giving a variable its first, initial value. This should
ideally be done immediately upon declaration to prevent the use of garbage
values. #include <iostream>
using namespace std;
int main()
syntax {
int score; //declaration only
int count = 0;
score = 95; // initialization
double pi = 3.14159; cout << "Score initialized later:\t" << score << endl ;
return 0;
}
Operators
An operator is a special symbol used to perform an operation (like calculation or
comparison) on values (operands) and produce a result.
Category Purpose Key Operators
Performs mathematical +, -, *, /, %
Arithmetic
calculations. (Modulus/Remainder)
Increment and
++, --
decrement
Combines Boolean
Logical && (AND), **`
results.
Programming Errors
3. Logic Errors
A Logic Error is an error in the design or implementation of the
algorithm. The program compiles and runs successfully without
crashing, but it produces incorrect or unexpected results because
the programmer's steps (the logic) are flawed.
#include <iostream>
using namespace std;
int main()
{
int numerator = 10;
int denominator = 0;
int val1 = 3;
int val2 = 5;
int final_result = val1 + val2 * 2;
cout << "--- Logic Error Output ---" << endl;
cout << "Result should be 16, but is: " << final_result << endl;
return 0;
}
Chapter Three: Control Statements
3. Control Statement
A Control Statement (or Control Structure) is a programming
construct that allows a programmer to specify the order of execution
of code statements.
They dictate the flow of control within a program, moving beyond
simple,
sequential, top-to-bottom execution.
int main()
{
int dayNumber;
cout << "--- Day of the Week Lookup ---" << endl;
cout << "Enter a number (1-7) to see the day: ";
cin >> dayNumber;
{
case 1:
cout << "Day 1 is: Monday" << endl;
break; // Essential: Exits the switch after a match
case 2:
cout << "Day 2 is: Tuesday" << endl;
break;
case 3:
cout << "Day 3 is: Wednesday" << endl;
break;
case 4:
cout << "Day 4 is: Thursday" << endl;
break;
case 5:
cout << "Day 5 is: Friday" << endl;
break;
case 6:
cout << "Day 6 is: Saturday" << endl;
break;
case 7:
cout << "Day 7 is: Sunday" << endl;
break;
default:
cout << "Error: You entered a number outside the 1-7 range." << endl;
break;
}
return 0;
}
3.5 Introduction to Loops
A loop is a control structure that causes a statement or group of statements to
repeat.
C++ has three looping control structures: the while loop, the do-while loop, and the
for loop.
The difference between each of these is how they control the repetition
int main()
{
int i= 1;
while (i <= 5)
{
cout << "Current count: " << i<< endl;
i++;
}
return 0;
}
3.5.2 The do-while Loop
The body of the loop is executed first, and then the condition is evaluated.
This guarantees that the code inside the loop will run at least once, even if the
condition is initially false.
syntax
•"Check, then execute."
•do-while loop: Executes the code block first, then checks the condition.
•"Execute once, then check."
#include <iostream>
using namespace std;
Difference while loop and do
int main()
while loop
{
while loop: Checks the int i = 1;
return 0;
}
Nested for loops
Nested for loops mean having one for loop inside another for
loop.
This is very useful for working with grids, tables, or multi-
dimensional data.
Example : Star Pattern #include <iostream>
using namespace std;
int main() {
for (int row = 1; row <= 4; row++) Output:
{
for (int col = 1; col <= row; col+ *
+) { **
cout << "* "; ***
} ****
cout << endl;
}
return 0;
Continue and Break statement
break and continue are control statements used to change
the normal flow of loops. Let me explain them clearly with
simple examples.
Break Statement
The break statement immediately terminates the loop and exits it
completely.
#include <iostream> //Break in For Loop
Continue Statement
The continue statement skips the current iteration and moves to the
next iteration of the loop.
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
int main() {
int main() {
for (int i = 1; i <= 10; i++) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) { // If number is even
if (i == 5) {
continue; // Skip even numbers
continue; // Skip iteration when i is
}
5
cout << i << " "; // Only odd numbers reach
}
here
cout << i << " ";
}
}
return 0;
return 0;
}
}
Feature break continue
Terminates the entire Skips only current
Action
loop iteration
Exits the loop
Flow Jumps to next iteration
completely
When you want to stop When you want to skip
When to use
the loop entirely specific cases
Loop continues with next
Result Loop ends prematurely
value
Chapter 4: Function and Passing
argument to
function
4.1 Definitions of Functions
A function is a block of instructions that is executed when it is called from
some other point of the program.
A function is a collection of statements that performs a specific task
There are two types of functions
1. Built-in Functions
2. User-defined Functions
1. built-in function
Functions that are pre-written and come with the programming language
itself.
They are stored in libraries and can be used by including the appropriate
header files.
2. User-defined Functions
Functions that are created by the programmer to perform
specific tasks required in their particular program.
Pre-defined in language
Definition Defined by programmer
libraries
Can be modified as
Modification Cannot be modified
needed
cout << "Enter second number (b): "; cout << "Enter first number (a): ";
cin >> b; cin >> a;
// Using built-in + operator (this is a built-in cout << "Enter second number (b): ";
function) cin >> b;
c = a + b;
// Using our user-defined function
c = addNumbers(a, b);
cout << "Using built-in + operator:" << endl;
cout << a << " + " << b << " = " << c << endl; cout << "Using user-defined function:" << endl;
cout << a << " + " << b << " = " << c << endl;
return 0;
} return 0;
}
4.2 Declaration and calling functions
A function call is a statement that causes a function to execute.
A function definition contains the statements that make up the function.
When creating a function, you must write its definition
All function definitions have the following parts
Name: Every function must have a name.
Parameter list: The program module that calls a function can send
data to it
Body: The body of a function is the set of statements that carry
out the task the function is performing
Return type: A function can send a value back to the program
module that called it