Thursday, 4 October 2012

Chapter 11 - Adding Conditions (Using If statement) [Part 1 of 3]


From this tutorial on wards everything you learn will exponentially add up to your capability of programming. Hence make sure what ever you learn here, learn it correctly.

To be a good programmer one has to be confident with the basics. So go through each and every concept again and again till you are can confidently use them when you need them.

Also try to put to use all that you have learned so far, it will always help you fresh with what you have learned.


So lets start with knowing how conditions work

Knowing the Syntax 


if (condition) 
{

   ...........

   ........... 

}



Lets understand this using simple examples in english
  1. If your percentage is above 40 you pass
  2. If your age is above 18 you can vote
  3. If you ran a distance 42 km you have completed the marathon
  4. If a number is divisible by 2 it is even
  5. If you press 'y' you can proceed 
Now that we have our examples the first thing we are going to do is break them into 2 parts. First the condition and the the action.


Condition
Action
Percentage greater than 40
You pass
Age is above  18
You can vote
Ran a distance of 42 km
You completed the marathon
Number divisible by 2
It is even
You press ‘y’
You can proceed


Now that we know how to break the statements into conditions and actions the next part is to know how to implement comparisons progmatically.


   Operator name    
 Syntax 
Equal to
a == b
Not equal to
a != b
Greater than
a > b
Less than
a < b
Greater than or equal to
a >= b
Less than or equal to
a <= b 


Note: '==' stands for comparison and '=' stands for assignment. 
Example :-    a == b compare a is equal to b, where as
                      a =  b means copy value of b in a. 

It is important that you learn to  make this distinction early on as a misinterpretation leads to a lot of errors.
Also every comparison operator returns an integer value 0 or 1. 1 for true and 0 for false.

I have written a small program to illustrate this concept

/*  
 Author : Ryan Sequeira  
 Date : 4th October 2012  
 Title : Program to illustrate the truth values of comparison operation 
*/  

#include<stdio.h>
#include<conio.h>

void main()
 {
  //using two values to compare
  int a=5, b=10;
  int result=0;

  clrscr();

  //comparison returns 0 when false
  result = a == b;
  printf("The result of comparison when false: %d",result);

  //comparison returns 0 when true
  result = a < b;
  printf("\nThe result of comparison when true: %d",result);

  getch();
 }

How the If statement works 

Every If statement requires a condition, and the code written in the if block is executed only of the comparison returns true, i.e 1.



This program is an simple example to show how "If statement" works. To illustrate the point i have written a complete code for only one of the five examples discussed above. Below this code are code block you can replace to implement other examples.

/*  
 Author : Ryan Sequeira  
 Date : 4th October 2012  
 Title : Program to check if one has passed
*/  

#include<stdio.h>
#include<conio.h>

void main()
 {

  int marks=0, pass_val=40;

  clrscr();

  //prompt the user to enter his percentage out of 100
  printf("Enter your marks (out of 100):");
  scanf("%d",&marks);

  //compare the result to the passing value
  if(marks >= pass_val)
   {
    printf("\n+---------------------+");
    printf("\n|                     |");
    printf("\n| You passed the exam |");
    printf("\n|                     |");
    printf("\n+---------------------+");
   }

  getch();
 }

The code block for other examples are : 
 
 if(age >= 18)
 {
  printf("You can vote"); 
 }


 
 if(run_distance > 42)
 {
  printf("You completed the marathon"); 
 }


 
 if(number%2 == 0)
 {
  printf("The number is even"); 
 }


 
 if(key == 'y')
 {
  printf("You can proceed"); 
 }


This is a three part tutorial, the other two will each extend the concepts you have learned in the previous part.

No comments:

Post a Comment