1/99
Looks like no tags are added yet.
What does ++ do?
Increases a variable by 1.
What does -- do?
Decreases a variable by 1.
Prefix increment (++x)
Increases x first, then uses the new value.
Postfix increment (x++)
Uses the current value first, then increases x.
Prefix decrement (--x)
Decreases x first, then uses the new value.
Postfix decrement (x--)
Uses the current value first, then decreases x.
Three main parts of a loop
Initialization, test condition, and update.
Initialization
Sets the starting value of the loop control variable.
Test condition
Determines whether the loop continues.
Update
Changes the loop control variable after an iteration.
Pretest loop
Checks the condition before running the loop body; may execute zero times.
Posttest loop
Executes the body before checking the condition; runs at least once.
while loop syntax
while (condition) { statements; }
while loop purpose
Repeat while a condition remains true, especially when the number of repetitions is unknown.
Input validation
Repeatedly ask for input until the user enters an acceptable value.
Loop counter
Variable used to track or control how many iterations occur.
do-while loop syntax
do { statements; } while (condition);
for loop syntax
for (initialization; condition; update) { statements; }
When should you use a for loop?
When the number of repetitions is known or controlled by a count.
When should you use a while loop?
When repetitions depend on a condition and the count is unknown.
When should you use a do-while loop?
When the loop body must execute at least once.
What does i += 2 mean?
Increase i by 2; equivalent to i = i + 2.
What does j -= 5 mean?
Decrease j by 5; equivalent to j = j - 5.
Running total
A variable that accumulates values across loop iterations.
How do you initialize a running total?
Set it to zero before adding values: int total = 0;
Sentinel value
Special input value that signals a loop to stop.
How do you choose a sentinel?
Use a value outside the range of legitimate input values.
Nested loop
A loop inside another loop.
Nested loop execution
The complete inner loop executes for each iteration of the outer loop.
Pattern building with nested loops
Outer loop usually controls rows; inner loop usually controls characters or columns.
What is an infinite loop?
A loop whose condition never becomes false unless interrupted.
What does break do inside a loop?
Immediately exits the nearest enclosing loop.
What does continue do inside a loop?
Skips the remaining body of the current iteration and proceeds to the next iteration.
What is a file?
Data stored persistently under a filename.
Which header supports file streams?
#include
What is ifstream?
An input file stream used to read data from a file.
What is ofstream?
An output file stream used to write data to a file.
Create an input file object
ifstream fin("data.txt");
Create an output file object
ofstream fout("results.txt");
How do you check whether a file opened?
Use if (fin) for success or if (!fin) for failure.
How do you close an input file?
fin.close();
How do you close an output file?
fout.close();
Operator for writing to a file
<< using an ofstream object.
Operator for formatted reading from a file
>> using an ifstream object.
What does getline(fin, line) do?
Reads an entire line from a file into a string.
What does fin.get(ch) do?
Reads one character, including whitespace.
What does while (fin >> value) mean?
Keep reading values while extraction succeeds.
What does while (getline(fin, line)) mean?
Read and process each line until reading fails.
What happens when opening an ofstream normally?
It creates the file if possible and truncates an existing file unless another open mode is used.
Why close files?
To release file resources and ensure buffered output is written.
What is integer division?
Division of two integers that discards the fractional portion, such as 5 / 2 producing 2.
What is the modulus operator (%)?
Returns the remainder after integer division.
How do you test whether a number is even?
number % 2 == 0.
What does fixed << setprecision(2) do?
Formats floating-point output with two digits after the decimal point. Modular programming Dividing a program into smaller functions with specific responsibilities.
Function
A named block of code that performs a task and can be called.
Four parts of a function definition
Return type, name, parameter list, and body.
Function header
First line of a function definition containing return type, name, and parameter list.
Function definition
The function header followed by its implementation in braces.
Function prototype
A function declaration without a body, ending in a semicolon.
Example function prototype
int add(int, int);
Example function definition
int add(int a, int b) { return a + b; }
Example function call
add(3, 4);
Return type
The data type of the value returned by a function.
What does void mean?
The function does not return a value.
What happens when a function is called?
Control transfers to the function and returns to the caller after it finishes.
Argument
The actual value or variable supplied in a function call.
Parameter
The variable declared in the function to receive an argument.
Pass by value
The function receives a copy; changes to the parameter do not affect the original variable.
Pass by reference
The function parameter refers to the caller's variable; changes can affect the original.
Reference parameter syntax
void change(int &x);
Value parameter syntax
void change(int x);
What does return do?
Ends the function and optionally sends a value back to the caller.
Can a function call be used in an expression?
Yes, if it returns a value: int result = add(2, 3) * 2;
Boolean function naming
Often begins with is, has, or can, such as isEven().
Local variable
Variable declared inside a function or block; accessible only within its scope.
Global variable
Variable declared outside all functions; accessible according to its scope throughout the program.
Why avoid most global variables?
They can make program behavior harder to understand, test, and maintain.
Why use global constants?
They allow shared read-only values without mutable global state.
Default argument
A parameter value used when the caller omits that argument.
Where should default arguments be written?
Typically in the function prototype; they may be in the definition if no earlier declaration supplies them.
Default parameter placement
Parameters with default arguments must follow all required parameters.
Example default argument
void greet(string name = "Guest");
Function overloading
Multiple functions with the same name but different parameter lists.
How does C++ select an overloaded function?
By the number and types of arguments and which overload is the best match.
Can functions be overloaded by return type alone?
No. The parameter lists must differ.
What does exit() do?
Terminates the program immediately rather than returning to the caller.
Which header declares exit()?
#include
What is EXIT_SUCCESS?
A standard constant indicating successful program termination.
What is EXIT_FAILURE?
A standard constant indicating unsuccessful program termination.
What does exit(EXIT_FAILURE) mean?
Terminate the program and report failure.
What does int main() normally return on success?
return 0; or reaching the end of main implicitly returns zero.
What is scope?
The region of a program where a name is accessible.
What happens to a variable declared inside a for initialization?
Its scope is the for statement; it is not available after the loop.
What does a function's parameter list describe?
The types and order of values the function accepts.
What is a function with no parameters declared as?
For example, void display();
What is a reference parameter useful for?
Allowing a function to modify a caller variable or avoiding copies when passed as const reference.
What does const int &x mean?
A read-only reference to an integer, allowing access without modifying the caller's value.
Why are function prototypes useful?
They let the compiler know the function signature before its definition appears.
What is the difference between a call and a definition?
A call runs a function; a definition provides the code that runs.
Can a void function use return;?
Yes, return; can end a void function early without returning a value.