Example
#include <iostream> int main() { int sum = 0, val = 1; // As long as the value of val is less than or equal to 10, the while loop will continue to execute while (val <= 10) { sum += val; //Assign sum+val to sum ++val; // Add 1 to val } std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl; return 0; }
#include <iostream> int main() { int sum = 0; // From 1 to 10 for (int val = 1; val <= 10; val++) sum += val; // Equivalent to sum=sum+val std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl; return 0; }
// It is worth mentioning that after entering the data, // You need to press Ctrl+Z and enter, // Will judge that the istream object is invalid, and the STD:: CIN > > Val condition is false #include <iostream> int main() { int sum = 0, val = 0; while (std::cin >> val) sum += val; // Equivalent to sum=sum+val std::cout << "Sum is: " << sum << std::endl; return 0; }
#include <iostream> int main() { int currVal = 0, val = 0; if (std::cin >> currVal) { int cnt = 1; while(std::cin >> val) if (val == currVal) ++cnt; else { std::cout << currVal << " occurs " << cnt << " times" << std::endl; currVal = val; cnt = 1; } std::cout << currVal << " occurs " << cnt << " times " << std::endl; } return 0; }
Knowledge points
I
The while statement repeatedly executes a piece of code until the given condition is false.
II
The for statement improves the mode that the while statement needs to detect variables in the loop conditions and increment variables in the loop body.
The loop body checks the loop conditions before each execution, and the expression is executed after the for loop.
III
When we use an istream object as a condition, the effect is to detect the state of the flow. If the flow is valid, that is, an error is encountered at the end of the flow, the detection is successful. When an end of file character is encountered, or an invalid input is encountered (for example, the value entered is not an integer), the state of the istream object becomes invalid. An istream object in an invalid state makes the condition false.
IV
if statement supports conditional execution and evaluates a condition.
V
In C + +, assign values with =, and use = = as the equality operator.
Exercises
Section 1.4.1 exercise
Exercise 1.9:
Write a program to add integers from 50 to 100 using a while loop.
#include <iostream> int main() { int sum = 0, val = 50; while (val <= 100) { sum += val; ++val; } std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl; return 0; }
Exercise 1.10:
In addition to the + + operator increasing the value of the operand by 1, there is a decrement operator (–) to reduce the value by 1. Write a program that uses the decrement operator to print out integers between 10 and 0 in decrement order in the loop.
#include <iostream> int main() { int val = 10; while (val >= 0) { std::cout << val << " "; --val; } std::cout << std::endl; return 0; }
Exercise 1.11:
Write a program to prompt the user to enter two integers and print out all integers within the specified range of these two integers.
#include <iostream> int main() { int v1 = 0, v2 = 0; std::cout << "Enter two numbers:" << std::endl; std::cin >> v1 >> v2; if (v1 > v2) // If v1 is larger than v2, swap two numbers { int temp = v2; v2 = v1; v1 = temp; } while (v1 < v2+1) // Output all integers within the specified range of these two integers in turn { std::cout << v1 << " "; ++v1; } std::cout << std::endl; return 0; }
Section 1.4.2 exercise
Exercise 1.12:
What does the following for loop do? What is the final value of sum?
int sum = 0;
for (int i = -100; i <= 100; ++i)
sum+= i;
The function is the sum of all integers from -100 to 100,
The final value of sum is 0
Exercise 1.13:
Repeat all the exercises in section 1.4.1 using the for loop (page 11).
#include <iostream> int main() { int sum = 0; for (int val = 50; val <= 100; ++val) sum += val; std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl; return 0; }
#include <iostream> int main() { for (int val = 10; val >= 0; --val) std::cout << val << " "; std::cout << std::endl; return 0; }
#include <iostream> int main() { int v1 = 0, v2 = 0; std::cout << "Enter two numbers:" << std::endl; std::cin >> v1 >> v2; if (v1 > v2) // If v1 is larger than v2, swap two numbers { int temp = v2; v2 = v1; v1 = temp; } for(v1;v1<=v2;++v1) std::cout << v1 << " "; std::cout << std::endl; return 0; }
Exercise 1.14:
Compared with for loop and while loop, what are the advantages and disadvantages of the two forms?
For is suitable for patterns that require frequent detection of variables in loop conditions and increasing or decreasing variables in the loop body.
while is suitable for loops that do not use a specific number to measure the number of cycles.
Exercise 1.15:
Write a program that contains the common errors discussed in "exploring compilation" on page 14. Be familiar with the error messages generated by the compiler.
#include <iostream> int main( { return 0; }
#include <iostream> int main() { int val = "error"; return 0; }
#include <iostream> int main() { int v1 = 0, v2 = 0; std::cin >> v1 >> v2; cout << v1 + v2 << std::endl; return 0; }
Section 1.4.3 exercise
Exercise 1.16:
Write a program to read a group of numbers from cin and output its sum.
#include <iostream> int main() { int sum = 0, val = 0; while (std::cin >> val) sum += val; std::cout << "Sum is: " << sum << std::endl; return 0; }
Section 1.4.4 exercise
Exercise 1.17:
If all the values entered are equal, what will the program in this section output? What would the output be like if there were no duplicate values?
If all the values entered are equal, the program in this section will output the number of occurrences of 1 item.
If there are no duplicate values, several records with 1 occurrences will be output.
Exercise 1.18:
Compile and run the program in this section and enter all equal values into it. Run the program again and enter no duplicate values.
#include <iostream> int main() { int currVal = 0, val = 0; if (std::cin >> currVal) { int cnt = 1; while (std::cin >> val) if (val == currVal) ++cnt; else { std::cout << currVal << " occurs " << cnt << " times" << std::endl; currVal = val; cnt = 1; } std::cout << currVal << " occurs " << cnt << " times " << std::endl; } return 0; }
Exercise 1.19:
Modify the program you wrote for exercise 1.10 (page 11) in section 1.4.1 (print a range of numbers) so that it can handle the situation that the first number entered by the user is smaller than the second number.
#include <iostream> int main() { int v1 = 0, v2 = 0; std::cout << "Enter two numbers:" << std::endl; std::cin >> v1 >> v2; if (v1 > v2) // If v1 is larger than v2, swap two numbers { int temp = v2; v2 = v1; v1 = temp; } for (v1; v1 <= v2; ++v1) std::cout << v1 << " "; std::cout << std::endl; return 0; }