Thursday, August 24, 2017

Decision Making in C

Decision making in C tutorial for beginners.

One day Mr. Brian told his lovely son, “Dear Brite, if you hard work, you will progress in your life”. After listening this tips from his beloved father, Brite started hard working and one day he succeeded on his career.

Now observe the above segment of a story. There has a condition where progressing in life depends on and the condition is hard working. Observing on the conditional statement we can take decision to act and that was taken by Brite.

As Brite, in our daily life we make decision to buy products depending on parse, to go out depending on climate etc.

Decision Making in C

This type making decision is called “Decision making with if statement” in C.

If statement in C Programming

The general formation of if statement in C Programming is:
if(condition test)
{
 /* Executable statement if and only if condition be true */
}
/*Executable Next Statement without depending condition test*/
We can apply the first conditional statement on this.
if(Will you do hard word?)
{
 You will progress in your life.
}
I don’t know what is the next occurrence of your life. 

If you try to run the above on your compiler, you will see many errors in your build results.

Why? Because, the condition test within parentheses is in wrong format.

The condition test must be with relational operators and using logical, arithmetic operators is optional.
At least one variable or constant must be compared with another variable or constant.

The another reasons is we didn’t write any statement according to the rules of declaration statement in C there.

Now we want to write a program where the value of variable number is given. We want to check the number is greater than 5. If number is greater than 5 then we want to print a string “Number is greater than 5.”

Then the program will be:
#include <stdio.h>
int main()
{
    int number;

    number = 10;
    //If statement Start
    if(number > 5) //Condition Test
    {
        printf("%d is greater than 5\n",number); //If condition be true then execute this
    }
    //End of if statement
    printf("Test is End.\n");
    return 0;
}
Output:

10 is greater than 5.
Test is End.

Remember:
  • Variable must be declared and initialized (assigned with constant) before putting within parentheses to compare.
    Example:
    int number; //This is variable declaration
    number = 10; //Initialization
  • Don’t put semicolon after if().
Exercise: Write a program to determine the input number is positive. If positive then print Number is positive.

Nested If Statement in C

If we want to test more after the first test condition be true by if statement, we can do it by nested if statement in C.

We often use nested if statement in our logical life. We declare nested if statement like: If you turn left  you will see a river and if you see any boat across on river then you can ride on it.

Look, riding on boat depends on two conditions: turning left to see a river and remaining boat to ride. If both conditions be true then you will be permitted to ride. These type statement is called nested if statement.

Nested if statement actually refers to AND returns of Nth if statement. 

The general form of nested test is:
if(1st condition test)
{
 if(2nd condition test) /*Executable Test if 1st condition test be true*/
    {
        /*Executable Statement if 2nd condition test be true*/
    }
    /*Executable Statement that doesn't depend on 2nd condition test but 1st condition*/
}
/*Executable Statement that doesn't depend any condition test*/
Example:
#include <stdio.h>
int main()
{
    int number;

    number = 6;

    /*Outer if statement start*/
    if(number >=0)
    {
        printf("%d is positive number.\n",number);
        /*Inner if statement of nested if statement start*/
        if(number>5)
        {
            printf("%d is greater than 5\n",number);
        }
        /*Inner if statement of nested if statement end*/
    }
    /*Outer if statement end*/
    printf("End of if statement.\n");
    return 0;
}

If...else statement in C Programming

Before we have seen if statement returns only one conditional statement. If conditional test be true then its relative statement be executed, otherwise skipped. But if else statement returns two conditional statement: one for true and another for false, but not both.

If Mr. Brian told Brite, “If you hard work, you will progress in your life, otherwise you will be unhappy.”, then the statement can be called if else statement.

The general form of if else statement:
  if(/*Condition Test*/)
    {
        /*Statement will be executed if test return true*/
    }
    else
    {
        /*Statement will be executed if test return false*/
    }

    /*Statement that doesn't depend on condition test*/
Program: Write a program to determine the scanned integer number is positive or negative with if else statement.
#include <stdio.h>
int main()
{
    int number;

    scanf("%d",&number);

    if(number>=0)
    {
        printf("%d is a positive number.\n",number);
    }
    else
    {
        printf("%d is a negative number.\n",number);
    }

    return 0;
}
Question: Can it possible to make nested if statement in if else statement in C? If yes, how?

Ans: Yes, it is possible to make nested if statement in if else statement in C. It is similar as nested if statement. You are allowed to make nested within if block, not within else block.

Example: We can check the above program with some modification.
#include <stdio.h>
int main()
{
    int number;

    scanf("%d",&number);

    if(number>=0)
    {
        if(number==0)
        {
            printf("%d is a ZERO.\n",number);
        }
        else
        {
            printf("%d is a positive number.\n",number);
        }
    }
    else
    {
        printf("%d is a negative number.\n",number);
    }

    return 0;
}

If else ladder statement in C Programming

If we want to return more than two statement by conditional statement, we have to use if else ladder statement in C Programming language. Similarly if else it returns two executed statement but it has additional block between if and else block, if else ladder. if else ladder is used to check conditional test.

The general form of if else ladder statement is:
if(/*1st Condition Test*/)
   {
       /*Executable statement if 1st condition test be true*/
   }
   else if(/*2nd Condition Test*/)
   {
        /*Executable statement if 2nd condition test be true*/
   }
   /*Similarly you can use more else if*/
   else
   {
        /*Executable statement if above all condition test be false*/
   }
Here else block is optional but if block is required. You can use nested if, if else, if else ladder statement within if, else if block.

Now run the following program and observe how it works.
#include <stdio.h>
int main()
{
    int number;

    scanf("%d",&number);

   if(number==0)
   {
       printf("%d is a ZERO.\n",number);
   }
   else if(number<0)
   {
        printf("%d is a NEGATIVE.\n",number);
   }
   else
   {
        printf("%d is a POSITIVE.\n",number);
   }

    return 0;
}

Conditional Operator in C

In decision making conditional operator is alternative of simple if else statement.

General form of conditional operator is:
(condition test) ? (/*Executable Statement if condition be true*/) : (/*Executable Statement if condition be false*/) 
You can assign the returned value from conditional operation.

Example: if we want to test and print 1 when it is even number and 0 when it is odd number by conditional operator, then the program can be as below:
#include <stdio.h>
int main()
{
    int value;
    value = 7;

    (value>0) ? (printf("Positive.\n") ) : (printf("Negative or Zero.\n"));

    return 0;

}

Nested of Conditional Operator

It is possible to make nested of conditional operator but it seems to so complex and should avoid it.

Example of nested conditional operator.
#include <stdio.h>
int main()
{
    int value, result;
    value = 7;

    result = ((value % 2)==0) ? 1 : 0;

    printf("%d\n",result);

    return 0;

}
You can observe the result by changing the value of value.

Remember: Parentheses is important whenever using nested in conditional operator.

Switch Statement in C

Switch statement is for making multi-way decision depending on various condition like if else ladder. But the significant difference between Switch and if else ladder are:
  • Switch statement is more faster than equivalent if else ladder.
  • Switch statement more efficient.
The general form of Switch statement in C is:
 switch(/*Arguement*/)
    {
    case /*Result of Test*/ :
        /*Executable statement if the above case satisfy*/;
        break;
    default:
        /*Executable statement if none the above case satisfy*/;
        break;
    }
Lets start to understand Switch statement.

 Switch, case, break, default are the main keyword in Switch statement.

Here switch is a built in function. The condition test / Expression/ variable within its parentheses works as argument of switch function.

Assume the result of argument may be 0, 1, 2 etc. Now case 0: means if the argument satisfy the result 0 then the executable statement will be … before break statement.

Break statement declares the end of Switch statement. That means, next case (case 1: , case 2: etc.) wont be checked, also default.
Default works as else in if else statement. If the none case satisfy to argument then the default block will be executed.

Remember:
  • Put colon (:) after case with value and default.
  • Put semicolon after break statement and executable statement.
  • Don’t put colon, semicolon or any special character after switch statement.
  • Put break statement at the end of every block.
  • Use proper indention as given.

Now run and observe the following program.
#include <stdio.h>
int main()
{
    int number;

    scanf("%d",&number);

    switch(number % 2)
    {
    case 0 :
        printf("Even.\n");
        break;
    default:
        printf("Odd.\n");
        break;
    }

    return 0;
}

Looking for more effecive Course?

C Programming For Beginners by Udemy icon
$15 (Original $145)
30-Day Money-Back Guarantee
Buy
Includes:
  • 6 hours on-demand video
  • 1 Article
  • 2 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
 Have got this article helpful or not working? Put your comment below.