C++ Loops, Increment Operators, and File I/O Fundamentals

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/99

encourage image

There's no tags or description

Looks like no tags are added yet.

100 Terms

1
New cards

What does ++ do?

Increases a variable by 1.

2
New cards

What does -- do?

Decreases a variable by 1.

3
New cards

Prefix increment (++x)

Increases x first, then uses the new value.

4
New cards

Postfix increment (x++)

Uses the current value first, then increases x.

5
New cards

Prefix decrement (--x)

Decreases x first, then uses the new value.

6
New cards

Postfix decrement (x--)

Uses the current value first, then decreases x.

7
New cards

Three main parts of a loop

Initialization, test condition, and update.

8
New cards

Initialization

Sets the starting value of the loop control variable.

9
New cards

Test condition

Determines whether the loop continues.

10
New cards

Update

Changes the loop control variable after an iteration.

11
New cards

Pretest loop

Checks the condition before running the loop body; may execute zero times.

12
New cards

Posttest loop

Executes the body before checking the condition; runs at least once.

13
New cards

while loop syntax

while (condition) { statements; }

14
New cards

while loop purpose

Repeat while a condition remains true, especially when the number of repetitions is unknown.

15
New cards

Input validation

Repeatedly ask for input until the user enters an acceptable value.

16
New cards

Loop counter

Variable used to track or control how many iterations occur.

17
New cards

do-while loop syntax

do { statements; } while (condition);

18
New cards

for loop syntax

for (initialization; condition; update) { statements; }

19
New cards

When should you use a for loop?

When the number of repetitions is known or controlled by a count.

20
New cards

When should you use a while loop?

When repetitions depend on a condition and the count is unknown.

21
New cards

When should you use a do-while loop?

When the loop body must execute at least once.

22
New cards

What does i += 2 mean?

Increase i by 2; equivalent to i = i + 2.

23
New cards

What does j -= 5 mean?

Decrease j by 5; equivalent to j = j - 5.

24
New cards

Running total

A variable that accumulates values across loop iterations.

25
New cards

How do you initialize a running total?

Set it to zero before adding values: int total = 0;

26
New cards

Sentinel value

Special input value that signals a loop to stop.

27
New cards

How do you choose a sentinel?

Use a value outside the range of legitimate input values.

28
New cards

Nested loop

A loop inside another loop.

29
New cards

Nested loop execution

The complete inner loop executes for each iteration of the outer loop.

30
New cards

Pattern building with nested loops

Outer loop usually controls rows; inner loop usually controls characters or columns.

31
New cards

What is an infinite loop?

A loop whose condition never becomes false unless interrupted.

32
New cards

What does break do inside a loop?

Immediately exits the nearest enclosing loop.

33
New cards

What does continue do inside a loop?

Skips the remaining body of the current iteration and proceeds to the next iteration.

34
New cards

What is a file?

Data stored persistently under a filename.

35
New cards

Which header supports file streams?

#include

36
New cards

What is ifstream?

An input file stream used to read data from a file.

37
New cards

What is ofstream?

An output file stream used to write data to a file.

38
New cards

Create an input file object

ifstream fin("data.txt");

39
New cards

Create an output file object

ofstream fout("results.txt");

40
New cards

How do you check whether a file opened?

Use if (fin) for success or if (!fin) for failure.

41
New cards

How do you close an input file?

fin.close();

42
New cards

How do you close an output file?

fout.close();

43
New cards

Operator for writing to a file

<< using an ofstream object.

44
New cards

Operator for formatted reading from a file

>> using an ifstream object.

45
New cards

What does getline(fin, line) do?

Reads an entire line from a file into a string.

46
New cards

What does fin.get(ch) do?

Reads one character, including whitespace.

47
New cards

What does while (fin >> value) mean?

Keep reading values while extraction succeeds.

48
New cards

What does while (getline(fin, line)) mean?

Read and process each line until reading fails.

49
New cards

What happens when opening an ofstream normally?

It creates the file if possible and truncates an existing file unless another open mode is used.

50
New cards

Why close files?

To release file resources and ensure buffered output is written.

51
New cards

What is integer division?

Division of two integers that discards the fractional portion, such as 5 / 2 producing 2.

52
New cards

What is the modulus operator (%)?

Returns the remainder after integer division.

53
New cards

How do you test whether a number is even?

number % 2 == 0.

54
New cards

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.

55
New cards

Function

A named block of code that performs a task and can be called.

56
New cards

Four parts of a function definition

Return type, name, parameter list, and body.

57
New cards

Function header

First line of a function definition containing return type, name, and parameter list.

58
New cards

Function definition

The function header followed by its implementation in braces.

59
New cards

Function prototype

A function declaration without a body, ending in a semicolon.

60
New cards

Example function prototype

int add(int, int);

61
New cards

Example function definition

int add(int a, int b) { return a + b; }

62
New cards

Example function call

add(3, 4);

63
New cards

Return type

The data type of the value returned by a function.

64
New cards

What does void mean?

The function does not return a value.

65
New cards

What happens when a function is called?

Control transfers to the function and returns to the caller after it finishes.

66
New cards

Argument

The actual value or variable supplied in a function call.

67
New cards

Parameter

The variable declared in the function to receive an argument.

68
New cards

Pass by value

The function receives a copy; changes to the parameter do not affect the original variable.

69
New cards

Pass by reference

The function parameter refers to the caller's variable; changes can affect the original.

70
New cards

Reference parameter syntax

void change(int &x);

71
New cards

Value parameter syntax

void change(int x);

72
New cards

What does return do?

Ends the function and optionally sends a value back to the caller.

73
New cards

Can a function call be used in an expression?

Yes, if it returns a value: int result = add(2, 3) * 2;

74
New cards

Boolean function naming

Often begins with is, has, or can, such as isEven().

75
New cards

Local variable

Variable declared inside a function or block; accessible only within its scope.

76
New cards

Global variable

Variable declared outside all functions; accessible according to its scope throughout the program.

77
New cards

Why avoid most global variables?

They can make program behavior harder to understand, test, and maintain.

78
New cards

Why use global constants?

They allow shared read-only values without mutable global state.

79
New cards

Default argument

A parameter value used when the caller omits that argument.

80
New cards

Where should default arguments be written?

Typically in the function prototype; they may be in the definition if no earlier declaration supplies them.

81
New cards

Default parameter placement

Parameters with default arguments must follow all required parameters.

82
New cards

Example default argument

void greet(string name = "Guest");

83
New cards

Function overloading

Multiple functions with the same name but different parameter lists.

84
New cards

How does C++ select an overloaded function?

By the number and types of arguments and which overload is the best match.

85
New cards

Can functions be overloaded by return type alone?

No. The parameter lists must differ.

86
New cards

What does exit() do?

Terminates the program immediately rather than returning to the caller.

87
New cards

Which header declares exit()?

#include

88
New cards

What is EXIT_SUCCESS?

A standard constant indicating successful program termination.

89
New cards

What is EXIT_FAILURE?

A standard constant indicating unsuccessful program termination.

90
New cards

What does exit(EXIT_FAILURE) mean?

Terminate the program and report failure.

91
New cards

What does int main() normally return on success?

return 0; or reaching the end of main implicitly returns zero.

92
New cards

What is scope?

The region of a program where a name is accessible.

93
New cards

What happens to a variable declared inside a for initialization?

Its scope is the for statement; it is not available after the loop.

94
New cards

What does a function's parameter list describe?

The types and order of values the function accepts.

95
New cards

What is a function with no parameters declared as?

For example, void display();

96
New cards

What is a reference parameter useful for?

Allowing a function to modify a caller variable or avoiding copies when passed as const reference.

97
New cards

What does const int &x mean?

A read-only reference to an integer, allowing access without modifying the caller's value.

98
New cards

Why are function prototypes useful?

They let the compiler know the function signature before its definition appears.

99
New cards

What is the difference between a call and a definition?

A call runs a function; a definition provides the code that runs.

100
New cards

Can a void function use return;?

Yes, return; can end a void function early without returning a value.