c++

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

For loop vs while loop/do while

*You should use the for loop instead of the while or do-while loop in any situation that clearly requires an initialization, uses a false condition to stop the loop, and requires an update to occur at the end of each loop iteration.

counter

A counter is a variable that is regularly incremented or decremented each time a loop iterates.

functions

A function is a collection of statements that performs a specific task (1)you have created a function named main in every program you've written. (2) you have used library functions such as pow and strcmp.

Returning a Value from a Function

A function may send a value back to the part of the program that called the function. The pow function, which you have already seen, is an example of a value-returning function. Here is an example: double x; x = pow(4.0, 2.0);

infinite loop

A loop in which the terminating condition is never satisfied.

Keeping a Running Total

A 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 .

Sentinels

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

continue statement

A statement that causes the remainder of the current iteration of a loop to be skipped. The flow of execution goes back to the top of the loop, evaluates the condition, and if this is true the next iteration of the loop will begin.

for loop...

Because the for loop tests its test expression before it performs an iteration, it is a pretest loop. It is possible to write a for loop in such a way that it will never iterate

Default Arguments

Default arguments are passed to parameters automatically if no argument is provided in the function call. A function's default arguments should be assigned in the earliest occurrence of the function name. This will usually be the function prototype.

fstream

File stream

Static Local Variables

If a function is called more than once in a program, the values stored in the function's local variables do not persist between function calls. This is because the local variables are destroyed when the function terminates and are then re-created when the function starts again. Static local variables are not destroyed when a function returns. They exist for the lifetime of the program, even though their scope is only the function in which they are defined. If you do provide an initialization value for a static local variable, the initialization only occurs once.

ifstream

Input file stream

ofstream

Output file stream

void Functions

Some functions simply perform one or more statements, which follows terminate. These are called void functions.

stubs and drivers

Stubs and drivers are very helpful tools for testing and debugging programs that use functions. They allow you to test the individual functions in a program, in isolation from the parts of the program that call the functions.

Breaking and Continuing a Loop

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.

do-while Loop

The do-while loop is a posttest loop, which means its expression is tested after each iteration. *The do-while loop must be terminated with a semicolon.* The do-while loop is a posttest loop. This means it does not test its expression until it has completed an iteration You should use the do-while loop when you want to make sure the loop executes at least once. It is ideal in situations where you always want the loop to iterate at least once. The do-while loop is a good choice for repeating a menu.

The exit() Function

The exit() function causes a program to terminate, regardless of which function or control mechanism is executing

for loop

The for loop is ideal for performing a known number of iterations. The first line of the for loop is the loop header. As long as this expression is true, the body of the for loop will repeat. The for loop is a pretest loop only first 2 statements in the body need ; built-in expressions for initializing, testing, and updating. The for loop is ideal in situations where the exact number of iterations is known.

return Statement

The return statement causes a function to end immediately.

Overloading Functions

Two or more functions may have the same name, as long as their parameter lists are different. Sometimes you will create two or more functions that perform the same operation, but use a different set of parameters or parameters of different data types

Using Files for Data Storage

When a program needs to save data for later use, it writes the data in a file. The data can then be read from the file at a later time. word proc image ed spreadsheets games web brows

stub

a dummy function that is called instead of the actual function it represents. It usually displays a test message acknowledging that it was called, and nothing more.

Nested Loops

a loop that is contained within another loop A clock is a good example of something that works like a nested loop.

driver

a program that tests a function by simply calling it. If the function accepts arguments, the driver passes test data. If the function returns a value, the driver displays the return value on the screen. This allows you to see how the function performs in isolation from the rest of the program it will eventually be part of.

Boolean Value example

bool isValid(int number) { bool status; if (number >= 1 && number <= 100) status = true; else status = false; return status; }

while loop

known as a pretest loop, which means it tests its expression before each iteration. An important characteristic of the while loop is that the loop will never iterate if the test expression is false to start with. ideal in situations where you do not want the loop to iterate if the condition is false from the beginning.

File Access Methods

sequential access and direct access. sequential access file, you access data from the beginning of the file to the end of the file. random access file (also known as a direct access file), you can jump directly to any piece of data in the file without reading the data that comes before it. This is similar to the way a CD player or an MP3 player works. You can jump directly to any song that you want to listen to.


Set pelajaran terkait

NCLEX: Assessment of Cardiovascular Function

View Set

CCNA 3 Scaling Networks Final Study Guide

View Set

Займенник як частина мови 4 клас

View Set

Ethics for Life Chapter 3- Conscience and Moral Development

View Set

MGMT 464 Final Exam SELU Honoree

View Set

Chapter 7 - Module 02: Microbial Genetics

View Set