Skip to main content

Conditional Statements(If , if-else , Nested IF ELSE and else if) in C program #DOTC_mdb

Conditional Statement in C Program.

 Hi  Everyone, 

Today we will discuss about condition/decision statement in C like If..else, Nested If..else and else..if Statement with basic example. Before Start with Conditional statement in C , Please go through my previous blogs for your reference where i defined the syntax and keywords with basic C programs included one IF conditional statement so it’s easy for you to go through this blog . Previous blog link is as below :

https://dheeraj60.blogspot.com/2020/06/introduction-to-c-program-dotcmdb.html

https://dheeraj60.blogspot.com/2020/06/c-basics-programs-with-with-different.html

Let’s  Start with Conditional/decision statement in C . Type of Condition/Decision statement in C is as below :

IF

IF –ELSE statement

Nested IF _ELSE Statement

ELSE –IF  Statement

In Previous blog i already defined IF statement with basic programs so let’s start with IF-ELSE statement in C.

IF-ELSE – If condition is true then the statements inside the body of  if are executed and the statements inside body of else are skipped. If condition is  false then the statements inside the body of  if are skipped and the statements in elseare executed. In Simple word we can say  we can opt another option for check if condition are true first statement is executed and if it is false then it will go to other statement that is else.

 

Syntax  of IF – ELSE Statement :

if(condition) {

   // Statements in body executed if condition is true

}

else {

   //Statements will execute if statement is false

}

 

Let’s take one simple program to understand this conditional statement in which we take input from user in number and check if number equals to 18 or greater than 18 it will return number is equal or greater than 18 if user enter less than 18 number the program will give number is less than 18 . So it will check first statement if that is true then it will execute first statement otherwise it will go to else statement and give the output.

 

Below is the Program .

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 

   int number;

   printf("Enter any number:");

   scanf("%d",&number);

   if(number >=21)

   {

            /* This statement will execute if the

             * condition (number>=21) returns true

             */

            printf("number is equal or greater than 21");

   }

   else

   {

            /* This statement will only execute if first statement is false

                         */

            printf("Number is less than 21 !");

   }

   return 0;

}

 

Here in Above Program below is comment only for first if statement you can omit this part, but i suggest you to make comment if you write any big program then it’s helpful for you to understand which statement is doing what .

/* This statement will execute if the

             * condition (number>=21) returns true

             */

 

Once you run this program below is the output of the program in code block.

 

 

 

Here we have given number 67 which is greater than 21 so first statement will executed , if we enter less than 21 then else statement will execute and ouput will be Number is less than 21 ! , but here the first statement is true so output will be number is equal or greater than 21. You can write anything in printf as output for your requirement.


In IF – ELSE statement you can write any program if you are not sure if first statement is true then you can check for another condition and execute the program as per your requirement.


NESTED – IF ELSE STATEMENT: When an if else statement is present inside the body of another if or else then this is known as NESTED –IF ELSE Statement. In other word we can say we can say inner and outer statement.

See the Below syntax of NESTED – IF ELSE STEMENT

if(condition) {

    //Nested if else inside the body of "if"

    if(condition2) {

       //Statements inside the body of nested "if"

    }

    else {

       //Statements inside the body of nested "else"

    }

}

else {

    //Statements inside the body of "else"

}

 

Let’s understand with simple example of NESTED IF – ELSE Statement where we can put multiple condition in nested  IF – ELSE by taking input number from user and display the output against our requirement.

Below is the program where i use two variables with integer type.

 

#include <stdio.h>

#include <stdlib.h>

int main()

{

 int num1, num2;

   printf("Enter the value of num1:");

   scanf("%d", &num1);

   printf("Enter the value of num2:");

   scanf("%d",&num2);

   if (num1 != num2)

   {

            printf("num1 is not equal to num2\n");

            //Nested if else

            if (num1 > num2)

            {

                        printf("num1 is greater than num2\n");

            }

            else

            {

                        printf("num2 is greater than num1\n");

            }

   }

   else

   {

            printf("num1 is equal to num2\n");

   }

   return 0;

}

 

In Above program we use != in first if statement that operator means not equals to . so from the program we will get 2 output as if user will pass 2 different numbers then 1st output will be num1 is not equals to num2 and other output which satisfy the condition passed by user . If user will pass the same number for both variable then only 1 output that is else condition num1 is equal to num2.

 Below is the output of above program in code block .

  


 

 ELSE –IF  Statement

The ELSE - IF statement is mainly used when we need to check multiple conditions within the program, nesting of if-else statement can be avoided using ELSE-IF statement.

Syntax of ELSE- IF Statement

if (condition1)

{

   //This statement  execute if the condition1 is true

}

else if(condition2)

{

   //This statement is execute if the condition2 is true

}

else if (condition3)

{

   //This statements is execute if the condition3 is true

}

.

.

else

{

   //This statements  execute when all the above conditions is false.

}

 

Let’s take the same example nested if else with two variables now we will do same by using ELSE- IF statement and see how it works.

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 

  int num1, num2;

   printf("Enter the value of num1:");

   scanf("%d", &num1);

   printf("Enter the value of num2:");

   scanf("%d",&num2);

   if (num1 !=num2)

   {

            printf("num1 is not equal to num2\n");

   }

   else if (num1 > num2)

   {

            printf("num1 is greater than num2\n");

   }

   else if (num2 > num1)

   {

            printf("num2 is greater than num1\n");

   }

   else

   {

            printf("num1 is equal to num2\n");

   }

   return 0;

}

 

Below is the output of above program in code block compiler .


 

 

 Here if user give inputs for num1 and num2 with different number then it’s satisfies the first condition only and it will give output as num1 is not equal to num2 . This is because in this statement as soon as a condition is satisfied, the statements inside that block are executed and rest of the blocks statement will be ignored and if user inputs num1 and num2 as same value then it will go to else and give the output as num1 is equal to num2.

 

Important Points :

Conditional Statement else and else..if are optional statements,as a program having only if statement would run fine. else and else..if cannot be used without the if. We  can have  any number of else..if statement in a if else..if block. If no condition Matches then last else block will execute . We can also use logical operators such as AND (&&), OR(||) and NOT(!) in conditional statement.


Pictorial representation of conditional Statement in C .



 

Using Condition statement you can now able to write any logical programs by using conditional statement in C for checking numbers greater or equals to etc , also you can check by using condition whether the number passed by user is odd or even . You can write so many basic programs by using conditional statement also for calculation and other programs which we used in previous blog you can use Conditional logic.  So try to write some logical programs by using operators or Logical operator and make good program as per your requirement.

 

I hope you are now able to write some calculation programs used for calculator applications and  can also able to use logical/Conditional operator to make control over program and can pass multiple condition in Program. Please try to write as many programs as you can as in next blog we will move to Loop concept in C Program which is very important for programmer and very interesting as well.

 

This is all about Condition statements in C program. Please read this blog carefully and try to write some good programs so in next blog when we start loop we can use conditional statement in loop.

 Please  let me know if you have any doubts in this blog.

Thanks.


 

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li