Conditions & Iterations – Part 2 of 2

This is part 2 of 2 tutorials on conditions and iterations. Essentially this means conditional execution and repetitive operations. Using arrays and a variable to index into the array we can process an entire array without writing code which specifically accesses each element in the array.

One of the differences I hope you note here is the indentation of the code samples which tended to not show up in the blog. In the next week, we’ll move the other posts over here and indent while we are at it.

In addition to if/else if/else there is a second series of conditional execution statements.

switch(variable to compare)
{
case literal:
… one or more statements
break;
case literal:
… one or more statements
break;
default:
… one or more statements
break;
}

There really isn’t a good use for the switch/case/default structure in this tutorial’s example program. An instance where this is used is when you are offering the user a menu of choices. In that case, the variable to compare is the data that your program collected as the user’s choice from the menu. Each literal would be an option from the menu. In the switch/case/default, the default is optional. The default is the code that gets executed if none of literals in the cases are the same as the variable to compare.

To illustrate this, consider the following menu:

Inventory Management

A – Add an Item
C – Change an Item’s Information
D – Delete an Item
P – Print All Current Items
Q – Quit Program

The switch/case/default might look like this:

switch(choice) // char choice defined above
{

case ‘A’:<

…code to add an item
break; // ends the switch statement and moves execution to closing brace

case ‘C’:

…code to change an item
break;

case ‘D’:

…code to delete an item
break;

case ‘P’:

…code to print items
break;

case ‘Q’:

…code to tell the program to quit
break;

default:

printf(“Not a menu option\n”); // show error message
break;

}

And there are also alternate looping structures.

while(condition to evaluate)
{
… one or more statements
}

or

do
{
… one or more statements
} while(condition to evaluate)

In both the while and the do/while statement is condition to evaluate which has the same application as it did in Part 1. Both the while and the the do/while are looping (or iterating) statements. The code between the braces is executed as 1 iteration of the loop. Then the while and the do/while statement evaluates condition to evaluate and if it evaluates to true then it re-executes the loop. This continues until the condition to evaluate evaluates to false.

The only difference between the while and the do/while is that the while test the condition before the first iteration of the loop but the do/while does not. So, if the condition is false on the first test a while does not execute but a do/while does.

Here is the sample code for this tutorial. It is commented as well.

#include <stdio.h>

/* Sample Program for C Tutorials for the home ed student.
Demonstrating the use of an integer array and a character
array.
Ron R.
August 2005
*/

int main()
{

// some variables for iteration
int i,j;
// some variables to track integer values
int max,min,sum;
// preassign values to integer array
int example[10] = {45,22,67,987,423,11,45,76,16,91};
// space for an extra character has to be left on the end
char sentence[13] = “Hello World!”;
// show the sentence one character at a time
i=0; // set initial value for loop
while(i<13)
{

/* %c = print a character
then printf needs a piece of data for each control string
*/
printf(“%c “,sentence[i]);
i++; // increment counter

}
/* print integer values and calculate values
the next statement is called a compound assignment
sum, min, max will all be set to the value in example[0]
*/
sum = min = max = example[0];

printf(“\n”); // some spacing

j=0;
do
{

// print it – %i = print an integer
printf(“%i:%i\n”,j,example[j]);
/* if this is not the first element in the array we have to do some
other processing
*/
if(j != 0)
{

// add to sum
sum += example[j];
// if its smaller than current min then replace
if(example[j] < min)
{

min = example[j];

}
// it will only be bigger when it is not smaller than min
else if (example[j] > max)
{

max = example[j];

}
}
j++;
} while(j<10)
// print results
printf(“smallest #: %i\n”,min);
printf(“largest #: %i\n”,max);
printf(“sum of #s: %i\n”,sum);
return 0;
}