Menu
×
   ❮     
HOME Computer Fundamentals C Language C++ MS-OFFICE TALLY 11/12 SCIENCE 11/12 COMM BCA BCCA BBA MCA
     ❯   

Computer

▸ Computer Fundamentals▸ What is Computer▸ History of Computer▸ Types of Computer▸ History of C Language▸ History of C Language

Computer Components

▸ Computer components▸ Input Devices▸ Output Devices▸ CPU▸ Hardware

Computer Memory

▸ Computer Memory▸ Register Memory

Computer Virus

▸ Computer virus▸ Computer virus 2

Computer Network

▸ Computer Network details

Number Systems

▸ Number Systems

C Control Statements

▸ C if else▸ C Switch Statement▸ if-else vs switch▸ C Loops▸ C Do- While Loop▸ C While Loop▸ C For Loop▸ C For Loop▸ Nested Loops in C▸ Infinite Loop in C▸ C break▸ C continue▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ Type Casting

C Functions

▸ What is Function▸ Recursion in C▸ Call: Value & Reference▸ Call: Value & Reference▸ Storage Classes▸ Storage Classes

C Array

▸ 1 D - Array▸ Return an Array in C▸ Array To Function ▸ 2 - D Array▸ Return an Array in C

C Pointers

▸ C Pointers▸ C Double Pointer (Pointer to Pointer)▸ C Double Pointer (Pointer to Pointer)

C Tutorial

▸ Data Types in C▸ Keywords in C▸ C Identifiers▸ Escape Sequence in C▸ ASCII value in C▸ Constants in C▸ Static in C▸ Programming Errors in C▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Conditional Operator in C▸ Bitwise Operator in C▸ Bitwise Operator in C▸ 2s complement in C▸ What is C Language▸ History of C Language▸ Features of C Language▸ How to install C▸ First C Program▸ Compilation process in c▸ Printf Scanft▸ Variables in C ▸ C Operators▸ Comments in C▸ C Format Specifier▸ Literals in C▸ Tokens in C▸ C Boolean▸ Programming Errors in C

Conditional Operator in C ❮ Edit Details

The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.

As conditional operator works on three operands, so it is also known as the ternary operator.

The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also a decision-making statement.

Syntax of a conditional operator

  1. Expression1? expression2: expression3;  

The pictorial representation of the above syntax is shown below:

 

 

 

 

 

Conditional Operator in C

Meaning of the above syntax. 

  • In the above syntax, the expression1 is a Boolean condition that can be either true or false value.
  • If the expression1 results into a true value, then the expression2 will execute.
  • The expression2 is said to be true only when it returns a non-zero value.
  • If the expression1 returns false value then the expression3 will execute.
  • The expression3 is said to be false only when it returns zero value.

Let's understand the ternary or conditional operator through an example.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int age;  // variable declaration  
  5.     printf("Enter your age");  
  6.     scanf("%d",&age);   // taking user input for age variable  
  7.     (age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));  // conditional operator  
  8.     return 0;  
  9. }  

In the above code, we are taking input as the 'age' of the user. After taking input, we have applied the condition by using a conditional operator. In this condition, we are checking the age of the user. If the age of the user is greater than or equal to 18, then the statement1 will execute, i.e., (printf("eligible for voting")) otherwise, statement2 will execute, i.e., (printf("not eligible for voting")).

Let's observe the output of the above program.

If we provide the age of user below 18, then the output would be:

Conditional Operator in C

If we provide the age of user above 18, then the output would be:

  • In the above syntax, the expression1 is a Boolean condition that can be either true or false value.
  • If the expression1 results into a true value, then the expression2 will execute.
  • The expression2 is said to be true only when it returns a non-zero value.
  • If the expression1 returns false value then the expression3 will execute.
  • The expression3 is said to be false only when it returns zero value.

Let's understand the ternary or conditional operator through an example.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int age;  // variable declaration  
  5.     printf("Enter your age");  
  6.     scanf("%d",&age);   // taking user input for age variable  
  7.     (age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));  // conditional operator  
  8.     return 0;  
  9. }  

In the above code, we are taking input as the 'age' of the user. After taking input, we have applied the condition by using a conditional operator. In this condition, we are checking the age of the user. If the age of the user is greater than or equal to 18, then the statement1 will execute, i.e., (printf("eligible for voting")) otherwise, statement2 will execute, i.e., (printf("not eligible for voting")).

Let's observe the output of the above program.

If we provide the age of user below 18, then the output would be:

Conditional Operator in C

If we provide the age of user above 18, then the output would be:

Conditional Operator in C

As we can observe from the above two outputs that if the condition is true, then the statement1 is executed; otherwise, statement2 will be executed.

Till now, we have observed that how conditional operator checks the condition and based on condition, it executes the statements. Now, we will see how a conditional operator is used to assign the value to a variable.

Let's understand this scenario through an example.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.    int a=5,b;  // variable declaration  
  5.    b=((a==5)?(3):(2)); // conditional operator  
  6.    printf("The value of 'b' variable is : %d",b);  
  7.     return 0;  
  8. }  

In the above code, we have declared two variables, i.e., 'a' and 'b', and assign 5 value to the 'a' variable. After the declaration, we are assigning value to the 'b' variable by using the conditional operator. If the value of 'a' is equal to 5 then 'b' is assigned with a 3 value otherwise 2.

Output

Conditional Operator in C

The above output shows that the value of 'b' variable is 3 because the value of 'a' variable is equal to 5.

As we know that the behavior of conditional operator and 'if-else' is similar but they have some differences. Let's look at their differences.

  • A conditional operator is a single programming statement, while the 'if-else' statement is a programming block in which statements come under the parenthesis.
  • A conditional operator can also be used for assigning a value to the variable, whereas the 'if-else' statement cannot be used for the assignment purpose.
  • It is not useful for executing the statements when the statements are multiple, whereas the 'if-else' statement proves more suitable when executing multiple statements.
  • The nested ternary operator is more complex and cannot be easily debugged, while the nested 'if-else' statement is easy to read and maintain.