Opérateurs de comparaison
Before diving into control structures, we need to understand some operators. The first set of operators we will look at are called comparison operators.
As expressed by the name, the comparison operators are used for comparing values. Following is a list of all the comparison operators:
Using comparison operators we can create logical expressions that return logical values, such as true or false. For-example the expression 5 < 1 will output false as 5 is not greater than 1.
Note
We can directly put expressions in the
Console.Writemethods.
main.cs
123456789101112using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(5 < 1); // Output: False } } }
Following are some more examples of expressions formed using comparison operators:
main.cs
123456789101112131415161718using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(1 == 2); // Output: False Console.WriteLine(2 == 2); // Output: True Console.WriteLine(5 < 10); // Output: True Console.WriteLine(5 < 5); // Output: False (5 is NOT less than 5) Console.WriteLine(5 <= 5); // Output: True Console.WriteLine(5 >= 5); // Output: True Console.WriteLine(7 != 9); // Output: True } } }
We can also put variables in these expressions:
main.cs
123456789101112131415161718using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value_1 = 7; int value_2 = 9; Console.WriteLine(value_1 == value_2); // Output: False Console.WriteLine(value_1 > value_2); // Output: False Console.WriteLine(value_1 < value_2); // Output: True Console.WriteLine(value_2 > 5); // Output: True } } }
We can store the results of the logical expressions into bool variables since boolean variables can hold a value of true or false:
main.cs
12345678910111213141516using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int x = 5; int y = 7; bool result = x > y; Console.WriteLine(result); // Output: False } } }
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you explain how to use comparison operators with variables?
What are some common mistakes when using comparison operators?
How do comparison operators work with different data types?
Awesome!
Completion rate improved to 1.59
Opérateurs de comparaison
Glissez pour afficher le menu
Before diving into control structures, we need to understand some operators. The first set of operators we will look at are called comparison operators.
As expressed by the name, the comparison operators are used for comparing values. Following is a list of all the comparison operators:
Using comparison operators we can create logical expressions that return logical values, such as true or false. For-example the expression 5 < 1 will output false as 5 is not greater than 1.
Note
We can directly put expressions in the
Console.Writemethods.
main.cs
123456789101112using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(5 < 1); // Output: False } } }
Following are some more examples of expressions formed using comparison operators:
main.cs
123456789101112131415161718using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(1 == 2); // Output: False Console.WriteLine(2 == 2); // Output: True Console.WriteLine(5 < 10); // Output: True Console.WriteLine(5 < 5); // Output: False (5 is NOT less than 5) Console.WriteLine(5 <= 5); // Output: True Console.WriteLine(5 >= 5); // Output: True Console.WriteLine(7 != 9); // Output: True } } }
We can also put variables in these expressions:
main.cs
123456789101112131415161718using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value_1 = 7; int value_2 = 9; Console.WriteLine(value_1 == value_2); // Output: False Console.WriteLine(value_1 > value_2); // Output: False Console.WriteLine(value_1 < value_2); // Output: True Console.WriteLine(value_2 > 5); // Output: True } } }
We can store the results of the logical expressions into bool variables since boolean variables can hold a value of true or false:
main.cs
12345678910111213141516using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int x = 5; int y = 7; bool result = x > y; Console.WriteLine(result); // Output: False } } }
Merci pour vos commentaires !