Computer Programming 1 - C++ Basics and Programming Cycle
The Programming Cycle
The programming cycle is a structured, 5-step process that programmers follow to design effective, error-free software.
Defining the Problem
Answers the core question: "What needs to be done?"
Involves carefully analyzing requirements to understand all inputs, calculations, and expected outputs before writing any code.
Planning the Solution
Selects the most efficient strategy to solve the problem.
Creates a flowchart to visually model the program's logic and execution flow.
Chooses an appropriate programming language for implementation.
Coding the Solution
Translates the flowchart logic into functional code using the correct syntax of the chosen language.
Checking the Program
Debugging: Tracing the code step-by-step to find and fix errors (bugs).
Testing: Executing the program with sample input data to ensure it produces accurate results.
Common Types of Errors:
Syntax Error: Happens when code breaks the grammatical rules of the programming language (caught during coding or compilation).
Logical Error: Occurs when the program runs without crashing, but outputs wrong results due to flaws in mathematical or logical formulas.
Documentation
Creates clear records and manuals, including the problem description, code listings, execution instructions, and specifications for future maintenance.
C++ Program Structure & Dissection
Basic Computer Operations
Most basic programs execute three main steps:
Input Data: Receiving values from user input or files.
Process Data: Performing calculations or logical checks on the data.
Output Results: Displaying computed results on screen or writing to a file.
Standard Program Template
#include <iostream>
using namespace std;
int main() {
// Code goes here
}
Program Statements
A program consists of sequential instructions called statements.
Statements specify actions (like getting input or printing text).
Every C++ statement usually ends with a semicolon
;.
Breakdown of a Sample Program (Hello.cpp)
// Hello.cpp: My first C++ program
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!";
}
// Hello.cpp...: Single-line comment. Explains the code to human readers and is ignored by the compiler.#include <iostream>: Preprocessor directive. Includes the standard input/output stream library, allowing the use of objects likecout.using namespace std;: Gives direct access to standard C++ functions and objects without requiring extra prefix tags.int main() { ... }: The main function. It serves as the primary entry point where execution begins.cout << "Hello world!";: Sends the text string"Hello world!"to the screen using thecoutobject and insertion operator<<.
C++ Comment Formats
Single-line comment: Starts with
//and ends at the end of the line.Multi-line comment: Starts with
/*and ends with*/, spanning one or more lines.
Identifiers and Naming Conventions
Identifiers
Identifiers are user-defined names given to variables, functions, and classes.
Case-Sensitive:
myVarandMYVARare treated as entirely different identifiers.Naming Rules: Must begin with a letter or underscore
_, followed by letters, digits, or underscores. Cannot start with a digit.
Naming Conventions (camelCase Standard)
Variables and Functions:
Start with a lowercase letter.
Capitalize the first letter of each subsequent word.
Examples:
average,annualGrossIncome,computePay,ratePerHour.
Classes:
Capitalize the first letter of every word, including the first word.
Examples:
Person,Employee,MyClass.
C++ Primitive Data Types
char: Stores a single character. Requires a minimum of () of memory.int: Stores whole integers (positive or negative). Requires a minimum of () of memory.float: Stores single-precision floating-point (decimal) numbers.double: Stores double-precision floating-point (decimal) numbers with higher accuracy.bool: Stores boolean values (trueorfalse).void: Represents an empty data type or absence of value.
Variable Declarations and Scope
Variable Declaration
All variables must be declared before they can be used in program statements.
General Format:
data_type var_name;Examples:
int i, j, x;double balance, profit, loss;
Variable Scopes
Local Variables:
Declared inside a function or code block enclosed by
{}.Accessible only within that specific block. Created upon block entry and deleted upon exit.
Formal Parameters:
Declared inside function header signatures to receive passed arguments.
Act like local variables inside the function body.
Global Variables:
Declared outside all functions.
Accessible anywhere throughout the program during its full execution lifecycle.
Assignment Statements and Operators
Assignment Operator (=)
Assigns the result of an expression on the right side (rvalue) to a variable on the left side (lvalue).
lvalue (Left-Hand Side): Must be a writable variable location in memory.
rvalue (Right-Hand Side): Can be any literal value, variable, or mathematical expression.
Valid:
a = 10;Invalid:
10 = a;(Syntax error: constants cannot be lvalues).
Assignment Shortcuts
x = x + 1x++orx += 1x = x - 1x--orx -= 1x = x * 1x *= 1x = x / 1x /= 1x = x % 1x %= 1
Arithmetic Operators
+Addition-Subtraction*Multiplication/Division%Modulus (Integer remainder; works only with integer operands)
Relational Operators
Compares two values and returns 1 for true or 0 for false:
>Greater than>=Greater than or equal to<Less than<=Less than or equal to==Equal to!=Not equal to
Logical Operators
Combines or negates boolean conditions (returns 1 for true, 0 for false):
!Logical NOT&&Logical AND||Logical OR
Motivational Quote
"A positive attitude will lead to positive outcomes."