Wednesday, 17 October 2012

Chapter 12 - Adding Conditions (Using If ... else...) [Part 2 of 3]

In the first part we learned how to deal with conditions and how to execute a block of code only when a certain condition is fulfilled

In this chapter we will deal will what we can do if a condition is false.
For this we will make use of the else block.

Take a look at the image above. The flow chart shows how the execution of the program proceeds in either cases. 

Note: It is important to know that it is not necessary to use an else block for every if statement and should be used only when necessary. 
Also the placement of the else block will make a lot of difference.  You will learn about this after we deal with "nested if... else..."

Lets implement a small program to implement if else.

 Author : Ryan Sequeira  
 Date : 17th October 2012  
 Title : Program to check if number is odd or even


void main()

  int num=0;


  //prompt the user to enter a number
  printf("Enter a number : ");

  //out if else block to handle even or odd cases
  if(num % 2 == 0)
   printf("\n%d number is even",num);
   printf("\n%d number is odd",num);


Note that you can skip the parenthesis ( the brackets {}) if you have a single line of code inside if or else

Now that we can handle both if and else cases the next thing to do is, to move ahead with the types of conditions we can use.

We will learn how to implement more complicated (multiple) conditions with some simple examples

The three important notations one are:

  1. &&
  2. ||
  3. !
These are called Boolean operators and each of these has a meaning and should be used carefully.

It took me some time to figure out a robust example that any one (not having knowledge of Boolean logic) could understand.

So the example we are going to be using is the number line example. We will also make use of images to visually illustrate the examples.

First the && (AND) operator

Condition 1:  num > 3
Condition 1:  num < 5
Result:  num > 3 && num < 5

which means the number has to be both greater than 3 and less than 5.


if(num > 3 && num < 5)
          printf("Number Accepted");
Second the || (OR) operator

Condition 1:  num < 3
Condition 1:  num > 5
Result:  num < 3 || num > 5

which means the number has to be either less than 3 or greater than 5. 
Also it can be both condition 1 (less than 3) and condition 2 (greater than 5) , but in this example it is not possible


if(num < 3 || num > 5)
          printf("Number Accepted");

And then the ! (NOT) operator

Here we will us the same 2 conditions from 'And' example and 'Not' the result

Value:  Not (condition/conditions)
Result:  !(num > 3 && num < 5)

so !{(condition 1) && (condition 2)}  translates to NOT ( greater than 3 and less than 5 ) 
which means the number should not be greater than 3 and less than 5.
These are all the values that would have been considered false by the same condition without '!' not operator.


if(!(num > 3 && num < 5))
          printf("Number Accepted");

Important Observation

Not (con1 AND con 2)  = Not (con1) OR Not (con2)

In this example
!(num > 3 && num <5) 
= !(num  > 3) || !(num < 5 )
= (num < 3) OR (num  > 5)   ...........    (This is the result of OR example)

We can compare the 2 results to prove it:


I have tried my best to explain multiple conditions with Boolean operators, but if you still have any doubts or suggestions feel free to comment.

No comments:

Post a Comment