Home » Science » Computer Science » How to use conditional operator in c++.

How to use conditional operator in c++.

Conditional Operator

The conditional operator is also called Ternary Arithmetic Operator as it takes 3 Operands. We can use the conditional operator to create short expression that works like if/else statements. The operator consists of the question-mark (?) and the colon (:). The syntax is given below:

Syntax: Conditional expression? True expression: False expression

Example:             a < 0 ? b = 100 : c = 200 ;

                                It works like If-Else statement.

                                If (a<0)

                                B=100;

                                Else

                                C=200;

The following expression figure illustrates the roles played by the 3-sub expressions.

Conditional Expression:                                   True Expression:                          False Expression:

Expression to be tested                                                Executes if the condition              Executes if the condition

is true                                                               is false

                                                               

          a < 0 ?                                                                                     b=100 :                                                   c = 200 ;  

       

                                                 

The Conditional Operator

The conditional operator can be applied to many other programming problems.

Cout<<”your grade is:”<<score < 60 ? “Fail” : “Pass”);

 

About

I am a person who is positive about every aspect of life.I am a Computer Science lecturer in a school. I love to spend my free time in learning new things.Working for eVirtualGuru gives me a lot extra knowledge and immense joy.

Leave a Reply

Your email address will not be published. Required fields are marked *