Responsive Ads Here

Friday, August 5, 2016

How to Learn C - Conditional Logic

If we need to use any condition to solve a problem, then we’ve to apply if-else method. This is called conditional logic.
c programming

General structure of this program:
#include<stdio.h>
int main()

{
            if ( condition)

                     {
                                    statement
                      }
            else
                    {
                                   statement
                     }
    return 0;
}
We’ve to write the condition in ( ) after “if”. If the given condition is true then, the the program will show the output of “if” area. If the given condition is not true, then the program will the result of “else” area.

We’ll think a problem now and make it solved. Ok, think with me. Suppose, We want to create a program which will be able to take a number input from the user and inform the user that the number is positive or negative. After writing the codes of this program we will try to understand. Now write the program.

How to Learn to Code C

Code:
#include <stdio.h>   
 int main()
 {   
     int n;   
     scanf (“%d”, &n);   
     if(n >= 0)
{   
                        printf("The number is positive\n");  
}   
     else
 {   
                         printf("The number is negative\n");   
            }  
      return 0;   
}
Output:


Run the program and input a decimal number then press on enter to see the output. If the program shows the correct result that means if it can detect the number is negative or positive, the written codes are all right. In the program we used scanf function to input a number from the user. Then we check the inputed number as it is greater than 0 or equal to 0. If it is true then the program can show “The number is positive” for that we keep the line in printf function. If the inputed number is less than 0, the program will make the output of the else area and will show “The number is negative”. ( \n ) is used to see the next result in new line.


If your mind ask you that 0 is not a positive number and you want to show 0 as a nutral number, then you write the following program.

Code:
#include <stdio.h>   
 int main()   
{   
     int n;
     scanf(“%d”,&n);
     if(n < 0) {   
         printf("The number is negative\n");   
     }   
     else if (n > 0) {   
         printf("The number is positive\n");   
     }   
     else if (n == 0) {  
         printf("The number is zero!\n");   
     }   
     return 0;  
}

Output:


In this program, 0 is assigned in a new line and new condition. As there is more than 2 conditions, we used else if.

If (n<0) : Here we examined as n is less than 0 or not. If less, then the program will show “ The number is negative”.

If (n>0) : We check the number as it is greater than 0 or not. If the condition is true, then the program will show the printf area.

If (n==0) : Here we  checked the number as it is equal to 0 or not. We used here ( = =)  to make the compiler understand that n is equal to 0.

No comments:

Post a Comment