Wednesday, July 28, 2010

Conditional Checks

The contractor whom you worked for is impressed by your work. Your pavement cost calculator system is very useful for his customers. One day, the contractor calls you to his office and asks if you could create an inventory system for him. You are not sure and come to me for advice. Yes, you can create an inventory system for the contractor. But before that you have a long way to go. I promise to teach you C/C++ but it is up to you to apply it.
We will look at the C/C++ constructs that you will need going ahead step by step. For starters let’s say that the contractor has two type of paving blocks; one with rounded edges and another with square edges. The rate depends on which type of blocks the customer chooses.
We need a modification in the cost calculation program which will give the customer a choice of the type of paving blocks. This brings us to the if statement. The if…else block is a condition checker. Statements after the if are performed only if the condition is true, otherwise those after else are performed. The else part is not compulsory. Its syntax is as follows:
if (condition)
{
//true part
}
else
{
//false part
}
Just as we mentioned for the while loop, a condition can be anything that can be evaluated to either a zero or a non-zero value. Also notice the scope defining {…}.
To show how the if construct is used, let us write the cost calculation program again.
#include”stdio.h”
int main()
{
int len1, width1, len2, width2, rate, area, typeOfPayment;
printf(“Enter the outside length\n”);
scanf(…);
…
printf(“Select one of the following pavement types\n”);
printf(“1. Rounded Pavements\n”);
printf(“2. Square Pavements\n”);
scanf(“%d”, &typeOfPayment);
…
if (1 == typeOfPayment)
{
rate = 150;
}
else
{
rate = 120;
}
…
}
Now what if there are three kinds of pavement blocks?

if (1 == typeOfPayment)
{ rate = 150; }
else if (2 == typeOfPayment)
{ rate = 120; }
else
{ rate=180; }
The keen observer would have noticed two unusual things. I always leave a white space between if and the “(” but there is no space between printf and “(”. That is just a good programming practice. It is a subtle way to differentiate functions from keywords. Keywords such as if, for, while, switch which have a parenthesis will always have a space between the word and the parenthesis. On the other hand, functions such as main, printf will have no whitespace between the end of the word and the beginning of the parenthesis.
The other thing which is less subtle is the condition inside the if parenthesis. I have always written the constant literal (1, 2 or 3) first and then equated it to the variable. It is not necessary to write in this form and if (typeOfPayment == 2) is equally correct. However, it is important that one not forget to use the double= sign. Instead if someone uses a single equal, typeOfPayment will be assigned the value of 2 rather than being checked if it is equal to one. Moreover, since this assignment will succeed, the result of the condition will be always evaluated to true! If however, one writes the literal as the first operand, and then inadvertently uses the single= the compiler will complain since assignments to literals is not possible. Your mistake of using single= will not go unnoticed. I understand that some people find it easier to remember that they must use == and not = instead of this roundabout manner of comparing variables to literals. Nonetheless many developers follow the style I will follow here. Java does not allow only = inside the if statement(condition must strictly evaluate to Boolean), hence this quirkiness does not apply to Java.

No comments: