# COP3014C Test 2 Study Guide
## Introduction
This study guide is designed to help you prepare for a C++ programming test. It covers various control structures and programming concepts, including `if` statements, `if/else` statements, `switch` statements, loops (`do`, `do-while`, `for`), `break` and `continue` statements, as well as key concepts like accumulators, counters, flags, EOF (End of File), and sentinels. You'll also find practice coding questions to reinforce your understanding.
## Control Structures
### 1. `if` Statements
- `if` statements allow conditional execution of code.
- Syntax:
```cpp
if (condition) {
// Code to execute if the condition is true
}
```
### 2. `if/else` Statements
- `if/else` statements allow you to provide an alternative code block if the condition is false.
- Syntax:
```cpp
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
```
### 3. `switch` Statements
- `switch` statements provide a way to execute different code based on the value of an expression.
- Syntax:
```cpp
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
// ...
default:
// Code to execute if none of the cases match
}
```
### Loops
### 4. `do` Loop
- The `do` loop ensures that the loop body is executed at least once before checking the condition.
- Syntax:
```cpp
do {
// Code to execute
} while (condition);
```
### 5. `do-while` Loop
- The `do-while` loop is similar to the `do` loop but checks the condition after executing the loop body.
- Syntax:
```cpp
do {
// Code to execute
} while (condition);
```
### 6. `for` Loop
- The `for` loop is used for iterating a specific number of times.
- Syntax:
```cpp
for (initialization; condition; update) {
// Code to execute
}
```
### Flow Control
### 7. `break` Statement
- The `break` statement is used to exit a loop or switch statement.
- It is commonly used to terminate a loop prematurely.
### 8. `continue` Statement
- The `continue` statement is used to skip the current iteration of a loop and continue to the next one.
### Programming Concepts
### 9. Accumulator
- An accumulator is a variable used to accumulate or store the sum of values.
### 10. Counters
- Counters are variables used to keep track of the number of occurrences or iterations.
### 11. Flag
- A flag is a boolean variable used to control the flow of a program, often in loops.
### 12. EOF (End of File)
- `EOF` is a constant used to detect the end of a file while reading.
### 13. Sentinels
- Sentinels are special values used to indicate the end of input or a specific condition in programs.
## Practice Coding Questions
### Question 1: Find the Largest Number
Write a C++ program that takes three numbers as input and outputs the largest of them using `if` statements.
### Question 2: Grade Calculator
Write a program that calculates the grade of a student based on their score using `if/else` statements. The program should output "A" for scores >= 90, "B" for scores between 80 and 89, and so on.
### Question 3: Month Name
Write a program that takes an integer (1-12) as input and outputs the corresponding month name using a `switch` statement.
### Question 4: Sum of Integers
Write a program that calculates the sum of integers from 1 to N using a `for` loop. N should be taken as input.
### Question 5: Odd Numbers
Write a program that uses a `do-while` loop to print all odd numbers from 1 to 20.
### Question 6: Password Validation
Write a program that asks the user to enter a password. If the password is "password123," output "Access granted." Otherwise, use a `while` loop to keep asking for the correct password until it's entered.
### Question 7: Find the Average
Write a program that calculates the average of a series of numbers (terminated by a sentinel value) using an accumulator and a counter.
### Question 8: File Processing
Write a program that reads numbers from a file until `EOF` is reached and calculates the sum of all the numbers.
### Question 9: Sentinel-Controlled Loop
Write a program that reads numbers from the user until a sentinel value is entered (e.g., -1) and calculates the sum of all entered numbers.
Use this study guide to review the mentioned topics and practice coding questions to ensure you are well-prepared for your C++ programming test.
## Practice Short Answer Questions
1. Explain the purpose of an `if` statement in C++ and provide an example of when you might use it in a program.
2. Differentiate between an `if` statement and an `if/else` statement in C++. Provide a code example for each.
4. What is the role of a `switch` statement in C++? Give an example of a situation where a `switch` statement would be useful.
4. Describe the key difference between a `do` loop and a `do-while` loop in C++. Provide a code example for each.
5. When would you use a `for` loop in C++? Explain the components of a `for` loop and provide an example.
6. What is the purpose of a `break` statement in C++? Give an example of when you might use it within a loop.
7. How does a `continue` statement differ from a `break` statement? Provide a scenario where a `continue` statement would be helpful.
8. Explain the concept of an "accumulator" in programming. How is it typically used, and why is it important?
9. What is the role of a "counter" in programming? Provide an example of a program that uses a counter.
10. Define the term "flag" in the context of programming. How is it used to control program flow?
11. What does "EOF" stand for in C++? How is it typically used when reading data from a file?
12. Explain the purpose of a "sentinel" in programming. How can it be used to control the execution of a program?