Conditional Statements and Conditional Operator | C Weekly

Welcome back,
Today we are gonna talk about very interesting topic in programming - "Conditional Statements".
As we know that a computer program is a set of statements, in previous blog discusses some types of statements in C. Now take a look on Conditional statements.

Conditional Statements:

Conditional statements are those statements which are based on some condition. For example you have some lines of code that you want to run only if a condition satisfy and you do not want that code to be run if the condition is not satisfy. But by default a C program execute in sequential manner that means one statement after another is execute as they were written. Consider the following example.

        Statement 1;
        Statement 2;
        Statement 3;
        Statement 4;

First the compiler execute statement 1 then statement 2 and so on. If you put some condition for a block of statements the flow of execution might change based on the result evaluated by the condition. This process is referred to as decision making in 'C.' The decision-making statements are also called as control statements or conditional statements.

They are further divided into following categories-
    1. Selective statements
    2. Iterative statements

Selective Statements:

These statements are used for selecting some statements  to execute. They are following-
  • if statement
  • if - else statement
  • if -else if - else statement
  • Nested if- else
  • switch statement
  • goto statement

if statement:

It is one of the powerful conditional statement. If statement is always used with a condition. The statement inside the body of the if is only executed when the condition is true. The condition is evaluated first before executing any statement inside the body of If. The syntax for if statement is as follows:


Following program illustrates the use of if construct in 'C' programming:


Output:
 

if - else statement:




if -else is the extended version of if statement. The statement inside the body of if  executes when the condition is true otherwise the statement inside the else is execute.

Following programs illustrate the use of the if-else construct: We will initialize a variable with some value and write a program to determine if the value is less than ten or greater than ten. Let's start.
Output:
 

if - else if - else statement:

when you have to check multiple conditions then if - else if - else statements are very useful.




Let us see the actual working with the help of a program.
 Output:
 

Nested if-else statement:

You can either use if -else if -else statements or Nested if -else statements for multiple conditions.
when the if - else statements are placed inside the body of another if- else then it is called Nested if else statements. The syntax of Nested if else is following:
 


Conditional Operator:

C language provide a conditional operator or it is called as ternary operator also, Because it works with three operands. It is known as conditional operator because it provides the functionality of if - else statement. It consist of '?' and ':' , the syntax of this operator is:


In conditional operator first the expression 1 is evaluated if the result of expression 1 is true than the expression followed by '?' will execute (i.e. expression 2) , but if the result of expression 1 was false than the expression followed by ':' will execute(i.e. expression 3). This operator is very helpful to make your program  compact.

For example:
 
Output :
 

We will discuss about switch and goto statements in our next blog. I hope this was helpful to you.


Comments

Popular posts from this blog

C Weekly | Data Types In C | Variables | Comments | Simple Program

Fundamentals of C | feel_the_coder | C Weekly