When the increment operator is used as in num1 ++ The expression is in this mode?
Chapter 5: Loops and Files Show 5.1 The Increment and Decrement Operators
++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1
++ can be used before (prefix) or after (postfix) a variable: ++val; val++; -- is the decrement operator. It subtracts one from a variable. val--; is the same as val = val - 1 -- can be also used before (prefix) or after (postfix) a variable: --val; val--; Increment and Decrement Operators in Program 5-1 // This program demonstrates the ++ and -- operators. int main() // Display the value in num. // Use postfix ++ to increment num. // Use prefix ++ to increment num. // Use postfix -- to decrement num. // Use prefix -- to increment num. Prefix vs. Postfix ++ and -- operators can be used in complex statements and expressions
Prefix vs. Postfix - Examples int num, val = 12; cout << val++; // displays 12, // val is now 13; cout << ++val; // sets val to 14, // then displays it num = --val; // sets val to 13, // stores 13 in num num = val--; // stores 13 in num, // sets val to 12 Notes on Increment and Decrement
pre- and post-operations will cause different comparisons 5.2 Introduction to Loops: The while Loop
while (expression) statement;
The while Loop – How It Works while (expression) statement; > expression is evaluated –if true, then statement is executed, and expression is evaluated again –if false, then the loop is finished and program statements following statement execute
Program 5-3 // This program demonstrates a simple while loop. int main() while (number < 5) The while Loop is a Pretest Loop - expression is evaluated before the loop executes. Watch Out for Infinite Loops
Example of an Infinite Loop int number = 1; 5.3 Using the while Loop for Input Validation
Using the while Loop for Input Validation Here's the general approach, in pseudocode: Read an item of input. cout << "Enter a number less than 10: "; Input Validation in Program 5-5 // This program calculates the number of soccer teams int main() // Variables // Get the number of players per
team. // Validate the input. // Get the input again. // Get the number of players available.
// Validate the input. // Calculate the number of teams. // Calculate the number of leftover players. // Display the results. Counters
A Counter Variable Controls the Loop in Program 5-6 // This program displays a list of numbers and int main() int num = MIN_NUMBER; // Counter cout << "Number Number Squared\n"; 5.5 The do-while Loop
do statement; // or block in { } while (expression);
An Example do-while Loop Although the test expression is false, this loop will execute one time because do-while is a posttest loop. A do-while Loop in Program 5-7 // This program averages 3 test scores. It repeats as int main() do
// Calculate and display the average. // Does the user want to average another set? do-while Loop Notes
5.6 The for Loop
for(initialization; test; update)
for Loop - Mechanics for(initialization; test; update) statement; // or block in { } 2)Evaluate test expression –If true, execute statement –If false, terminate loop execution 3)Execute update, then re-evaluate test expression for Loop - Example for (count = 1; count <= 5; count++) A for Loop in Program 5-9 // This program displays the numbers 1 through 10 and // their squares. #include using namespace std; int main() cout << "Number Number Squared\n"; for (num = MIN_NUMBER; num <= MAX_NUMBER; num++) return 0; When to Use the for Loop In any situation that clearly requires –an initialization –a false condition to stop the loop –an update to occur at the end of each iteration
for (count = 11; count <= 10; count++) for Loop - Modifications You can have multiple statements in the initialization expression. Separate the statements with a comma: int x, y; for (x=1, y=1; x <= 5; x++) { cout << x << " plus " << y }
int x, y; for (x=1, y=1; x
<= 5; x++, y++)
int sum = 0, num = 1; for (; num <= 10; num++) sum += num;
int sum = 0; for (int num = 0; num <= 10; num++) sum += num; The scope of the variable num is the for loop. 5.7 Keeping a Running Total
int sum=0, num=1; // sum is the while (num <= 10) // accumulator { sum += num; num++; } cout << "Sum of numbers 1 – 10 is" << sum << endl; A Running Total in Program 5-12 // This program takes daily sales figures over a period of time int
main() // Get the number of days. // Get the sales for each day and accumulate a total. // Display the total sales. 5.8 Sentinels
A Sentinel in Program 5-13 // This program calculates the total number of points a
int main() cout << "Enter the number of points your
team has earned\n"; while (points != -1) 5.9 Deciding Which Loop to Use
5.10 Nested Loops
for (row=1; row<=3; row++) //outer Nested for Loop in Program 5-14 // This program averages test scores. It asks the user
for the int main() // Set up numeric output formatting. // Get the number of students. // Get the number of test scores per student. // Determine each student's average score. Nested Loops - Notes
5.11 Using Files for Data Storage
Files: What is Needed
ifstream for input from a
file
ifstream infile; Opening Files
infile.open("inventory.dat");
Testing for File Open Errors
infile.open("test.txt"); if (!infile) { cout << "File open failure!"; }
Using Files
outfile << "Inventory report";
infile >> partNum; infile >> qtyInStock >> qtyOnOrder; Using Loops to Process Files
while (inputFile >> number) ...
infile.close(); outfile.close();
–may be limit on number of open files –may be buffered output data waiting to send to file Letting the User Specify a Filename
–It returns the contents of the object formatted as a null-terminated C-string. –Here is the general format of how you call the c_str function: stringObject.c_str() Letting the User Specify a Filename in Program 5-24 // This program lets the user enter a filename. int main() // Get the filename from the user. // Open the file. // If the file successfully opened, process it. // Close the file. 5.12 Breaking and Continuing a Loop
The continue Statement
What is the operator used for increment?In languages syntactically derived from B (including C and its various derivatives), the increment operator is written as ++ and the decrement operator is written as -- . Several other languages use inc(x) and dec(x) functions.
What does it mean to increment something what operator is used in C++ to do this?In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning the variable.
What does ++ i mean in programming?i++ increment the variable i by 1. It is the equivalent to i = i + 1. i– decrements (decreases) the variable i by 1.
Which operator is used to increment a variable?A program can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c=c+1 or c+=1. An increment or decrement operator that is prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator, respectively.
|