C# Relational operators

In this chapter you will learn:

  1. How to use Relational operators
  2. Example for Relational operators
  3. Combine relational operators and logic operators

Description

The relational operators are as follows:

Operator Meaning
==Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example


using System; /*from  w ww. j ava 2  s  . co m*/
 
class Example {    
  public static void Main() {    
    int i, j; 
 
    i = 10; 
    j = 11; 
    if(i < j) 
       Console.WriteLine("i < j"); 
    if(i <= j) 
       Console.WriteLine("i <= j"); 
    if(i != j) 
       Console.WriteLine("i != j"); 
    if(i == j) 
       Console.WriteLine("this won't execute"); 
    if(i >= j) 
       Console.WriteLine("this won't execute"); 
    if(i > j) 
       Console.WriteLine("this won't execute"); 
 
  }    
}

The code above generates the following result.

Example 2


/*w  ww  .j a v a 2s  . c o m*/
using System;

class MainClass
{
  static void Main(string[] args)
  {
    int a = 10, b = 20, c = 30;

    if (a < 15 && b < 20)
      c = 10;
        Console.WriteLine(c);

    if (a < 15 || b < 20)
      c = 15;
        
        Console.WriteLine(c);

    if (!(a == 15))
      c = 25;
      
        Console.WriteLine(c);
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What are Conditional Operators
  2. Boolean logical AND operator
  3. Boolean logical OR operator
  4. Boolean logical NOT operator
  5. Logical operators with an if statement
  6. Short-circuit evaluation
  7. Not short-circuit
  8. When to use Logic operator shortcut
  9. && vs &
  10. Side-effects of short-circuit operators
Home »
  C# Tutorial »
    C# Language »
      C# Operators
C# Arithmetic Operators
C# Arithmetic Assignment Operator
C# Increment and Decrement Operators
C# Remainder operator
C# Relational operators
C# Conditional Operators
C# Ternary operator(The ? Operator)
C# Bitwise Operators
C# Bitwise Shift Operators
C# sizeof Operator
C# typeof Operator