C / C++ Tutorial Part 2


Making Decisions
Download Source

if( condition )
{
   /* do things here */
}
else
{
   /* do other things here */
}

Above is the use of if. if condition is true then the braces with "do things here" will be entered into. Otherwise ( condition is false) the braces with "do other things" will be entered into.

The question on your lips will probably be, " What is condition ???".

Condition can be any instruction. The useful things are :

X == Y True if X is equal to Y
X != Y True is X is NOT equal to Y
X > Y True if X is greater than Y
X < Y True if X is less than Y

An example of the use of if and else


#include <stdio.h>


void main(void)
{
    /* Our variables        */
    char cInputChar = 0;
    
    /* Prompt the user      */
    printf("Do you want to carry on ? (y/n)\n");
    
    /* Get the character from the user */
    cInputChar = getchar();
    
    /* If it is 'y' then we want to carry on */
    if(cInputChar == 'y')
    {
        printf("You want to carry on\n");
    }
    else
    {
        printf("You don't want to carry on !\n");d
    }
}

The above program only checks to see if you type 'y'. If you type anything other than 'y' in then you don't want to carry on. If you type in capitol 'Y' then you don't want to carry on ! Lets improve it. We can follow the else with an if, we can also trail the "else if"s to add further checking, NOTE: you don't need to have an else at all if you don't want or need one. Another thing we can do is increase the complexity of the condition. A condition has to eventually come out to zero or non-zero. So lets put some logic in that condition.

We have certain kinds of switches available to use to make decisions. You may recognise these from digital electronics, internet search engines or many places.

&& AND
|| OR
! NOT

if ((condition1) && (condition2))
condition1 and condition2 must be non zero for the if to work. If either of the conditions is zero then the if will not be executed - the else will be if there is one.

if ((condition1) || (condition2))
If condition1 or condition2 is non-zero then the if operation will be carried out. If both are zero then the if wil not be executed.

if(!(condition))
If the condition is zero then we will carry out the if.

Lets have a go at that :

#include <stdio.h>


void main(void)
{
    /* Our variables        */
    char cInputChar = 0;
    
    /* Prompt the user      */
    printf("Do you want to carry on ? (y/n)\n");
    
    /* Get the character from the user */
    cInputChar = getchar();
    
    /* If it is 'y' then we want to carry on */
    if((cInputChar == 'y') || (cInputChar == 'Y'))
    {
        printf("You want to carry on\n");
    }
    else if((cInputChar == 'n') || (cInputChar == 'n'))
    {
        printf("You don't want to carry on !\n");
    }
    else
    {
        printf("Invalid entry\n");
    }
}

It would be nice if we could go back and ask the user to enter their choice again ir they didn't type 'y' or n' wouldn't it ? Lets go do that.

Part 3

Index


Last Updated 22 February 1998 by Nik Swain (email: nikswain@geocities.com)

This page hosted by Geocities Icon Get your own Free Home Page

1