C# Relational operators
In this chapter you will learn:
- How to use Relational operators
- Example for Relational operators
- 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:
- What are Conditional Operators
- Boolean logical AND operator
- Boolean logical OR operator
- Boolean logical NOT operator
- Logical operators with an if statement
- Short-circuit evaluation
- Not short-circuit
- When to use Logic operator shortcut
- && vs &
- Side-effects of short-circuit operators