Conditional Execution
Program ControlIt is time to turn our attention to a different problem - conditional execution. We often need to be able to choose which set of instructions are obeyed according to a condition. For example, if you're keeping a total and you need to display the message 'OK' if the value is greater than zero you would need to write something like:
if (total>0) printf("OK");The if statement allows you to evaluate a > condition and only carry out the statement, or compound statement, that follows if the condition is true. In other words the printf will only be obeyed if the condition total > 0 is true.
If the condition is false then the program continues with the next instruction. In general the if statement is of the following form:
if (condition) statement;and of course the statement can be a compound statement.
Here's an example program using two if statements:#include
main()
{
int a , b;
do
{
printf("\nEnter first number: ");
scanf("%d" , &a);
printf("\nEnter second number: ");
scanf("%d" , &b);
if (a if (b }
while
(a < 999);
}
Here's another program using an if keyword and a compound statement or a block:
#includemain()
{
int a , b;
do
{
printf("\nEnter first number: ");
scanf("%d" , &a);
printf("\nEnter second number: ");
scanf("%d" , &b);
if (a {
printf("\n\nFirst number is less than second\n");
printf("Their difference is : %d\n" , b-a);
printf("\n");
}
printf("\n");
}
while (a < 999);
}
The if statement lets you execute or skip an instruction depending on the value of the condition. Another possibility is that you might want to select one of two possible statements - one to be obeyed when the condition is true and one to be obeyed when the condition is false. You can do this using the
if (condition) statement1;else statement2;
form of the if statement.
In this case statement1 is carried out if the condition is true and statement2 if the condition is false.
Logical ExpressionsWe have assumed that the way to write the conditions used in loops and if statements is so obvious that we don't need to look more closely. In fact there are a number of deviations from what you might expect. To compare two values you can use the standard symbols:
> (greater than)< (less than)
>= (for greater than or equal to )
<= (for less than or equal to)
== (to test for equality)
The reason for using two equal signs for equality is that the single equals sign always means store a value in a variable - i.e. it is the assignment operator. For example:
if (a = 10) instead of if (a == 10)Just as the equals condition is written differently from what you might expect so the non-equals sign looks a little odd. You write not equals as !=. For example:
if (a != 0)is 'if a is not equal to zero'.
An example program showing the if else construction now follows:
#includemain ()
{
int num1, num2;
printf("\nEnter first number ");
scanf("%d",&num1);
printf("\nEnter second number ");
scanf("%d",&num2);
if (num2 ==0) printf("\n\nCannot devide by zero\n\n");
else
printf("\n\nAnswer is %d\n\n",num1/num2);
}