Quiz 3 Chapter 4 (Decision Making) & Chapter 5

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

== determines whether a variable is equal to another

= operator assigns the value on the operator right to the variable on the left

True statement: if (x < 20 || x > 40)

False statement: if (x < 20 && x > 40)

In C++ relational expression represent true states with the number 1

False states with the number 0

fstream

File stream. Object of this type can be used to open the file for reading, writing, or both

ifstreeam

Input file stream - You create an object of this data type when you want to open an exisiting file and read data from it

Counter

Variable that is regularly incremented o decremented each time a loop iterates #include <iostream> using namespace std; int main () { const int MIN = 1, MAX = 10; int num = MIN // counter cout << "Number Number Squared\n"; cout << "-------------------------------\n"; while (num <= MAX) { cout << num << "\t\t" << (num * num) << endl; num++; // Increment the counter } return 0; }

read position.

When a file has been opened for input, the file stream object internally maintains a special value

The process of retrieving data from a file is known as reading data from the file.

When a piece of data is read from a file, it is copied from the file into a variable RAM. An input file is a file that data is read from.

Programmers usually refer to the process of saving data in a file as writing data to a file.

When a piece of data is written to a file, it is copied from a variable in RAM to the file. An output file is a file that data is written to.

When a program needs to save a data for later use

it writes the data in a file. The data can then be read from the file at a later time

Each repetition of a loop is known as

iteration

All the relational operators are binary

which means they use two operands ---> x > y

A sentinel is a special value that marks the end of a list value

while (points != -1). The sentinel is minus one

x = 10; if (x ++ > 10) cout << "x is greater than 10.\n"; cout wont execute because it tested the relational first Postfix mode

x = 10; if (++x > 10) cout << "x is greater than 10.\n"; cout will execute because it tested the relational first Prefix mode

! (x>2) is X not greater than 2

!x > 2 Is the logical negation of x greater than 2

Logical operators connect two or more relational expression into one or reverse the logic of an expression

&& AND Both expression must be true || OR One or both must be true ! NOT Reverse the truth

Program that calculate the total of the numbers typically used two elements total += sales

1. A loop that reads each number in the series 2. A variable that accumulates the total of the numbers as they are read

Text file contains the data that has been encoded as text

A binary file contains the data that has not been converted to text.

Relational operators can also be used to compare characters and string objects

Comparing characters & string objects using ASCII

Relational Operators

Allow you to compare numeric and char value and determine whether one is greater that, less than, equal to, or not equal to another ( ==)

If a loop does not have a way of stopping, it is called an infinite loop.

An infinite loop continues to repeat until the program is interrupted example: placing semicolon on the while line

Input validation is the process of inspecting data given to a program by the user and determining if it is valid

As long as the user of a program enters bad input, the program will produce bad output. Program should be written to filter out bad input if (testScore >= MIN && testScore <= MAX)

When a block statement is nested inside another block, a variable defined in the inner block may have the same name as variable defined in the outer block.

As long as the variable in the inner block is visible however the variable in the outer block will be hidden int main () { int num; cout << "Enter a number > 0: " cin >> num; if (num > 0) { int num; // Another variable cout << "Enter another num: " cin >> num; cout << "The second num was << num << endl; } cout "The first num is " << num << endl; return 0; }

A loop is a control structure that causes a statement or group of statement to repeat.

C ++ has three looping control structures: The while loop, the do-while loop, and the for loop. The difference between these structures is how they control the repetition

Use the break and continue statements with great caution. Because they bypass the normal condition that controls the loop's iterations, these statements make code difficult to understand and debug.

For this reason, you should avoid using break and continue whenever possible. However, because they are part of the C++ language, we discuss them briefly in this section.

The while loop has two important parts: 1. An expression that is tested for a true or false value 2. A statement or block that is repeated as long as the expression is true

Format: while (expression) statement;

One way to code a decision structure in c++ is with the if statement.

Here is the general format: if (expression) statement; If the value of the expression in the parentheses is true, the very next statement is executed

Relational expression are also known as Boolean expression, which means their value can only be either true or false

If x is greater than y, the expression x > y will be true, while the expression y == x will be false

The if else if statement tests a series of condition.

It is often simpler

The if else if statement tests a series of condition.

It is often simpler to test a series of conditions with the if else is statement than with a set of nested if else statement

The if statement can cause other statements to execute only under certain conditions

Many algorithms require a program to execute some statements only under certain circumstances. This can be accomplished with decision structure

inputFile.open("C:\\data\\inventory.txt") --> In this statement, the file C:\data\inventory.txt is opened and linked with inputFile.

Notice the use of two backslashes in the file's path. Two backslashes are needed to represent one backslash in a string literal.

When a file is used by a program, three must be taken

Open the file - Opening a file creates a connenction between the file and the program Process the file - Data is either written to file or read from the file Close the file - After the program is finished using the file, the file must be closed.

Precedence and Associativity of Logical Operators

Order of Precedence ! && ||

Postfix mode (num++) causes the increment to happen after the value of the variable is used in the expression. cout will display num and the variable will be added to one

Prefix mode (++num) causes the increment to happen first. The variable will incremented and cout will display incremented variable

Sequential access file - you access the data from the beginning to the end, you cannot jump

Random access file - you can jump

All the relational operators have left to right associativity

Recall that associativity is the order in which an operator works with its operand

The switch default section does not need a break statement.

Some programmer prefers to put one there anyway, for consistency

The switch statement lets the value of a variable or expression determine where the program will branch

Test the value of an integer expression and then uses that value to determine which set of statement to branch to switch (IntegerExpression) { case ConstantExpression: // place one or more stmt case ConstantExpression: default: }

The break statement causes a loop to terminate early.

The continue statement causes a loop to stop its current iteration and begin the next one

The for loop is count controlled loop for (num = MIN; num < = MAX; num ++) Pretest loop --> Test expression before it performs an iteration

The loop must posses: 1. Initialize a counter variable to a starting value 2. Test the counter variable by comparing it to a max number. When reaches max the loop terminates 3. Update the counter variable during each iteration. This is usually done by incrementtng the variable

Prefix mode

The operator is placed before the variable name ++num; --> Add 1 from their operand --num; --> Substract 1 from their operand

Running total is a sum of numbers that accumulates with each iteration of a loop

The variable used to keep the running total is called an accumulator

The while loop works like an if statement that executes over and over. As long as the expression inside the parentheses is true, the conditionally executed statement or block will repeat.

The while loop is known as a pretest loop, which means it tests its expression before each iteration

Variable defined inside a set of braces have local scope or block scope

They may only be used in the part of the program between their definition and the block's closing brace

When a program is running and it enters the section of code that constitutes a variable's scope, it is said tha the variable comes into scope.

This simply means the variable is now visible and the program may reference it. Likewise when a variable leaves scope, it may no longer be used

Because of the way that floating point are stored in memory, rounding errors sometime occurs

You should be careful when using the equality (==) to compare floating point numbers

a = 2; b = 5; c = a * b++; cout ----> 2 6 10

a = 2; b = 5; c = a * ++b; cout ---> 2 6 12

A nested loop: A loop that inside another loop

for (int hours = 0; hours < 24; hours++) { for (int min = 0; min < 60; min++) { for (int sec = 0; sec < 60; sec++) { cout << setw(2) << hours << ":"; cout << setw(2) << min << ":"; cout << setw(2) << hours << endl; } } }

The do while loop is a post test loop which means its expression is tested after each iteration Perform at least one iteration

format: do statement; while (expression); multiple statement format: do { statement; statement; } while (expression); The do while statement must be terminated with a semi colon

To test more than one condition, an if statement can be nested onside another if statement

if (expression) if (expression) { statement; statement; } For readability and easier debugging, it's important to use proper alignment and indentation in a set of nested if statements

What if you want an if statement to conditionally execute a group of statement, not just one line

if (expression) { statement; statement; }

x < 0 ? y = 10 : z = 20

if (x < 0) y = 10; else z = 20;

To increment a value means to increase it by one, and to decrement a value means to decrease it by one

num = num + 1; SAME num += 1; num = num - 1; SAME num -= 1; Postfix mode: the operator is placed after the variable

ofstream

output file stream. You create an object of this data type when you want to create a file data to it.

Here are two reasons a program should close files when it is finished using them: inputFile.close();

• Most operating systems temporarily store data in a file buffer before it is written to a file. A file buffer is a small "holding section" of memory that file-bound data is first written to. When the buffer is filled, all the data stored there is written to the file. This technique improves the system's performance. Closing a file causes any unsaved data that may still be held in a buffer to be saved to its file. This means the data will be in the file if you need to read it later in the same program. • Some operating systems limit the number of files that may be open at one time. When a program closes files that are no longer being used, it will not deplete more of the operating system's resources than necessary.


संबंधित स्टडी सेट्स

220 Practice Questions (Got Wrong)

View Set

EAQ Ch. 46 - Urinary Elimination and the Nursing Process

View Set

Khan Academy SAT Reading & Writing Practice

View Set