# Ternary Operator (If expressions) in Carbon

Ternary operators are generally used to return a value based on conditions in a single statement. This prevents unnecessary variable declarations and memory consumption. In Carbon we have `if expressions` in place of usual `?:` ternary notations.

## Introduction

An `if` expression is an expression of the form:

`if` condition `then` value1 `else` value2

The condition is converted to a `bool` value in the same way as the condition of an `if` statement.

The value1 and value2 are implicitly converted to their common type, which is the type of the `if` expression.

## Code Example

``````var a: i32 = 50;
var b: i32 = 60;

var c: i32 = if a < b then a else b;
// c = 50

var d: i32 = (if a > b then a - b else b - a) * 10;
// d = (b - a) * 10 = (60 - 50) * 10 = 10 * 10 = 100``````

Get only the best articles delivered to your mail

## Loops in Carbon Language – while, for, break, continue, return

Table of Contents Hide while Loopfor LoopbreakcontinuereturnConclusion We can use for and while loops in carbon language. Within a loop the break and continue statements can be used for flow…

## Operators in Carbon Language

Table of Contents Hide ArithmeticBitwise & BitshiftComparisonConversionLogicalIndexingFunction CallPointerMoveConclusion We have a number of operators in Carbon language like – Arithmetic, Bitwise, BitShift, Comparison, Conversion, Logical, Indexing, function call, pointer, move…

## Functions in Carbon Language

Table of Contents Hide IntroductionCode Example –Conclusion Functions in carbon language has the same role as in C, Php or any other functional language. It is used to separate a…

## Introduction to Carbon Language

Table of Contents Hide IntroductionFeatures of Carbon LanguageWhy developed Carbon? Why not upgraded C++?Where Carbon could be used?Carbon is written in which language?Code Example Carbon Language is the successor to…

## Conditions or Control Flow in Carbon Language – If, elseif, else

Conditions or control flow in carbon language is similar to any other language. It has if, elseif and else keywords. Introduction if and else provide conditional execution of statements. An if statement consists of: An if introducer…

## Tuples in Carbon Language – Composite Types

Table of Contents Hide Code ExamplesHow to access a tuple value?Conclusion A tuple in carbon language is a fixed-size collection of values that can have different types, where each value…