PROGRAMMING 1 — MODULE 2 REVIEWER
Course Code: COMP 20023 (BSIT)
Module Title: Basic Program Structure
Module Overview
This module introduces the basic structure of a C++ program, data types, variables,
input/output, and expressions.
Objectives:
• Understand C++ program structure
• Use correct data types and variable names
• Declare and initialize variables
• Distinguish constants from variables
• Apply input/output statements
• Identify arithmetic, relational, and logical expressions
Introduction to C++
Developed by Bjarne Stroustrup (1979, Bell Labs). A middle-level language that combines
low-level hardware control and high-level abstraction. It runs on multiple platforms
(Windows, Mac, UNIX).
Advantages:
• Efficient and close to hardware
• Supports Object-Oriented Programming (OOP)
• High performance for system software
• Widely used in software industry
• Helps understand compiler, linker, loader, and memory concepts
Uses and Applications of C++
C++ is used for system tools, device drivers, teaching, research, and software development.
Applications:
• Operating Systems: Windows, Linux, MacOS
• Browsers: Chrome, Firefox
• Databases: MySQL
• Language Development: Java, C#, PHP, Python
• Scientific computing, game development, embedded systems
Local Environment Setup
To start coding in C++, you need:
1. Text Editor – Notepad, VS Code
2. Compiler – GCC, MinGW
3. IDE – Code::Blocks (recommended)
Code::Blocks is open-source, supports GCC/Clang/Visual C++, uses plugins, and is ideal for
C, C++, and Fortran.
C++ Program Structure
#include <iostream>
using namespace std;
int main() {
cout << 'Hello World';
return 0;
}
Explanation:
• #include <iostream> – for input/output
• using namespace std – allows standard names
• // – comment
• int main() – program entry point
• cout << 'Hello World'; – outputs text
• return 0 – ends program
Semicolons and Blocks
Each statement ends with a semicolon (;). Blocks of code use curly braces { }. Line breaks
are ignored by the compiler.
Identifiers and Keywords
Identifiers: names for variables/functions. Must start with a letter or underscore, may
include digits. Case-sensitive and cannot use symbols like @, $, %.
Examples: studentName, _temp, value_1
Keywords: int, float, while, for, if, else, switch, class, return, void, break, const, true, false
Whitespace and Comments
Whitespace improves readability. Comments explain code:
• Single-line: // comment
• Multi-line: /* comment */
C++ Data Types
Data types define memory size and value range.
Boolean (bool) – true/false
Character (char) – single character
Integer (int) – whole numbers
Float (float) – decimals
Double (double) – higher precision
Void (void) – no value
Wide Character (wchar_t) – extended character
Variables
Variables are named memory storage locations.
Example:
int age = 20;
float grade = 91.5;
char letter = 'A';
Declaration syntax: type variable_list;
Example: int i, j, k; char c = 'A';
Variable Scope
1. Local – inside a function
2. Global – outside all functions
3. Formal Parameters – within parentheses of a function
Example:
int g = 20; // global
int main() { int g = 10; cout << g; } // outputs 10
Initialization
Methods:
1. C-like: int x = 0;
2. Constructor-style: int x(0);
3. Uniform (C++11): int x{0};
Constants
Constants are fixed values declared using const keyword.
Example:
const int minutesPerHour = 60;
const float PI = 3.14;
Input and Output
Input stream: cin
Output stream: cout
Example:
int age;
cout << 'Enter age: ';
cin >> age;
cout << 'Your age is: ' << age;
Expressions and Operators
Types: Arithmetic, Relational, Logical, Assignment
Arithmetic Operators:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
Relational Operators:
== (equal)
!= (not equal)
>, <, >=, <= (comparisons)
Logical Operators:
&& (AND)
|| (OR)
! (NOT)
Assignment Operators: =, +=, -=, *=, /=, %=
Example: C += A; // same as C = C + A
Coding Exercises
Example 1 – Average of 3 Quizzes:
Input 3 quiz scores, compute average.
double q1, q2, q3, ave;
cin >> q1 >> q2 >> q3;
ave = (q1 + q2 + q3)/3;
cout << 'Average: ' << ave;
Example 2 – Simple Calculator:
Input 2 numbers, compute sum, difference, product, and quotient.
double num1, num2;
cin >> num1 >> num2;
cout << num1+num2;