Loops and Relational Expressions

Introducing the for Loop



// forloop.cpp -- introducing the for loop

#include <iostream.h>
int main(void)
{
	//   initialize; test ; update
	for (int i = 0; i < 5; i++)
		cout << "C++ knows loops.\n";
	cout << "C++ knows when to stop.\n";
	return 0;
}



// num_test.cpp -- use numeric test in for loop

#include <iostream.h>
int main(void)
{
	cout << "Enter the starting countdown value: ";
	int limit;
	cin >> limit;
	for (int i = limit; i; i--)      // entry-condition test
		cout << "i = " << i << "\n";
	cout << "Done now that i = " << i << "\n";
	return 0;
}



// express.cpp -- values of expressions

#include <iostream.h>
int main(void)
{
	int x;

	cout << "The expression x = 100 has the value ";
	cout << (x = 100) << "\n";
	cout << "Now x = " << x << "\n";
	cout << "The expression x < 3 has the value ";
	cout << (x < 3) << "\n";
	cout << "The expression x > 3 has the value ";
	cout << (x > 3) << "\n";
	return 0;
}



// formore.cpp -- more looping with for

#include <iostream.h>
const int ArSize = 16;        // example of external declaration
int main(void)
{
	double factorials[ArSize];
	factorials[1] = factorials[0] = 1.0;
	for (int i = 2; i < ArSize; i++)
		factorials[i] = i * factorials[i-1];
	for (i = 0; i < ArSize; i++)
	{
		cout << i << "! = ";
		cout << factorials[i] << "\n";
	}
	return 0;
}



// bigstep.cpp -- count as directed

#include <iostream.h>
int main(void)
{
	cout << "Enter an integer: ";
	int by;
	cin >> by;
	cout << "Counting by " << by << "s:\n";
	for (int i = 0; i < 100; i = i + by)
		cout << i << "\n";
	return 0;
}



// forstr1.cpp -- using for with a string

#include <iostream.h>
#include <string.h>
const int ArSize = 20;
int main(void)
{
	cout << "Enter a word: ";
	char word[ArSize];
	cin >> word;

	// display letters in reverse order
	for (int i = strlen(word) - 1; i >= 0; i--)
		cout << word[i];
	cout << "\n";
	return 0;
}



// plus_one.cpp -- the increment operator

#include <iostream.h>
int main(void)
{
	int a = 20;
	int b = 20;

	cout << "a   = " << a << ":   b = " << b << "\n";
	cout << "a++ = " << a++ << ": ++b = " << ++b << "\n";
	cout << "a   = " << a << ":   b = " << b << "\n";
	return 0;
}



// block.cpp -- use a block statement

#include <iostream.h>
int main(void)
{
	cout << "The Amazing Accounto will sum and average ";
	cout << "five numbers for you.\n";
	cout << "Please enter five values:\n";
	double number;
	double sum = 0.0;
	for (int i = 1; i <= 5; i++)
	{                                   // block starts here
		cout << "Value " << i << ": ";
		cin >> number;
		sum += number;
	}                                   // block ends here
	cout << "Five exquisite choices indeed! ";
	cout << "They sum to " << sum << "\n";
	cout << "and average to " << sum / 5 << ".\n";
	cout << "The Amazing Accounto bids you adieu!\n";
	return 0;
}



// forstr2.cpp -- reversing an array

#include <iostream.h>
#include <string.h>
const int ArSize = 20;
int main(void)
{
	cout << "Enter a word: ";
	char word[ArSize];
	cin >> word;

	// physically modify array
	char temp;
	int i, j;
	for (j = 0, i = strlen(word) - 1; j < i; i--, j++)
	{                        // start block
		temp = word[i];
		word[i] = word[j];
		word[j] = temp;
	}                        // end block
	cout << word << "\n";
	return 0;
}



Relational Expressions



// equal.cpp -- equality vs assignment

#include <iostream.h>
int main(void)
{
	int quizscores[10] =
		{ 20, 20, 20, 20, 20, 19, 20, 18, 20, 20};

// NOTE: some implementations may need to use
//	static int quizscores[10] to enable initialization

    cout << "Doing it right:\n";
	for (int i = 0; quizscores[i] == 20; i++)
    	cout << "quiz " << i << " is a 20\n";

	cout << "Doing it dangerously wrong:\n";
	for (i = 0; quizscores[i] = 20; i++)
    	cout << "quiz " << i << " is a 20\n";
	
	return 0;
}



// compstr.cpp -- comparing strings

#include <iostream.h>
#include <string.h>     // prototype for strcmp()
int main(void)
{
	char word[5] = "?ate";

// NOTE: some implementations may need to use
//	static char word[5] to enable initialization

	for (char ch = 'a'; strcmp(word, "mate"); ch++)
    {
		cout << word << "\n";
		word[0] = ch;
	}
	cout << "After loop ends, word is " << word << "\n";
	return 0;
}



The while Loop



// while.cpp -- introducing the while loop

#include <iostream.h>
const int ArSize = 20;
int main(void)
{
	char name[ArSize];

	cout << "Your first name, please: ";
	cin >> name;
	cout << "Here is your name, verticalized and ASCIIized:\n";
	int i = 0;                 // start at beginning of string
	while (name[i] != '\0')    // process to end of string
	{
		cout << name[i] << ": " << int(name[i]) << '\n';
		i++;                   // don't forget this step
    }
	return 0;
}



// waiting.cpp -- using clock() in a time-delay loop

#include <iostream.h> 
#include <time.h> // describes clock() function, clock_t type 
int main(void) 
{
	cout << "Enter the delay time, in seconds: "; 
	float secs; cin >> secs; 
	clock_t delay = secs * CLOCKS_PER_SEC;	// convert to clock ticks 
	cout << "starting\a\n";
	clock_t start = clock();
	while (clock() - start < delay )	// wait until time elapses
	    ; 
	cout << "done \a\n"; 
	return 0;
}



The do while Loop



// dowhile.cpp -- exit-condition loop

#include <iostream.h>
int main(void)
{
	int n;

	cout << "Enter numbers in the range 1-10 to find ";
	cout << "my favorite number\n";
	do
	{
		cin >> n;		// execute body
	} while (n != 7);		// then test
	cout << "Yes, 7 is my favorite.\n" ;
	return 0;
}


Loops and Text Input



// textin1.cpp -- reading chars with a while loop

#include <iostream.h>
int main(void)
{
	char ch;
	int count = 0;		// use basic input

	cin >> ch;		// get a character
	while (ch != '#')	// test the character
	{
		cout << ch;	// echo the character
		count++;	// count the character
		cin >> ch;	// get the next character
	}
	cout << "\n" << count << " characters read\n";
	return 0;
}



// textin2.cpp -- reading chars with a while loop

#include <iostream.h>
int main(void)
{
	char ch;
	int count = 0;

	cin.get(ch);            	// use the cin.get(ch) function
	while (ch != '#')
	{
		cout << ch;
		count++;
		cin.get(ch);    	// use it again 
	}
	cout << "\n" << count << " characters read\n";
	return 0;
}



// textin3.cpp -- reading chars to end of file

#include <iostream.h>
int main(void)
{
	char ch;
	int count = 0;

	while (cin.get(ch))      // cin.get(ch) is 0 on EOF
	{
		cout << ch;
		count++;
	}
	cout << count << " characters read\n";
	return 0;
}



// textin4.cpp -- reading chars with cin.get()

#include <iostream.h>
int main(void)
{
	int ch;				// should be int, not char
	int count = 0;
	
	while ((ch = cin.get()) != EOF)	// test for end-of-file
	{
		cout << char(ch);	// typecast
		count++;
	}
	cout << count << " characters read\n";
	return 0;
}



Nested Loops and Two-Dimensional Arrays



// nested.cpp -- nested loops and 2-D array

#include <iostream.h>

const int Cities = 5;
const int Years = 4;
int main(void)
{
	char * cities[Cities] =		// array of pointers
	{ 					// to 5 strings
		"Gribble City",
		"Gribbleton",
		"New Gribble",
		"San Gribble",
		"Gribble Vista"
	};

	int maxtemps[Years][Cities] =	// 2-D array
	{
		{94, 98, 87, 103, 101},	// values for maxtemps[0]
		{98, 99, 91, 107, 105},	// values for maxtemps[1]
		{93, 91, 90, 101, 104},	// values for maxtemps[2]
		{95, 100, 88, 105, 103}	// values for maxtemps[3]
	};

	cout << "Maximum temperatures for 1990 - 1993\n\n";

	for (int city = 0; city < Cities; city++)
	{
		cout << cities[city] << ":\t";
		for (int year = 0; year < Years; year++)
			cout << maxtemps[year][city] << "\t";
		cout << "\n";
	}

	return 0;
}