C++ primer plus (Sixth Edition) Programming Exercise Answers Chapter 5 Loops and Relational Expressions

1. Program list

forloop.cpp

// forloop.cpp -- introducing the for loop
#include <iostream>
int main()
{
	using namespace std;
	int i;  // create a counter
//   initialize; test ; update
	for (i = 0; i < 5; i++)
		cout << "C++ knows loops.\n";
	cout << "C++ knows when to stop.\n";
	// cin.get();
	return 0;
}

Results of the:

C++ knows loops.
C++ knows loops.
C++ knows loops.
C++ knows loops.
C++ knows loops.
C++ knows when to stop.

 num_test.cpp

// num_test.cpp -- use numeric test in for loop
#include <iostream>
int main()
{
	using namespace std;
	cout << "Enter the starting countdown value: ";
	int limit;
	cin >> limit;
	int i;
	for (i = limit; i; i--)     // quits when i is 0
		cout << "i = " << i << "\n";
	cout << 

Guess you like

Origin blog.csdn.net/qq_43445867/article/details/129780920