Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Logiska Operatorer | Styrstrukturer
Quizzes & Challenges
Quizzes
Challenges
/
C#-Grunder

bookLogiska Operatorer

Logical operators can be used for combining two logical expressions or logical values. Following are the three logical operators:

Note

An operand refers to a value or expression that is used as an input for an operator in a statement or expression. For example in the expression 1 + 2, the values 1 and 2 are operands. In the case of logical operators, an operand is always a boolean expression or value.

You can see the usage of these operands in the examples below:

Let's now use them in practice:

The AND (&&) operator takes two operands and returns true only if both the operands are true. It is demonstrated by the following code:

main.cs

main.cs

copy
123456789101112131415
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(true && true); // Output: True Console.WriteLine(true && false); // Output: False Console.WriteLine(false && true); // Output: False Console.WriteLine(false && false); // Output: False } } }

Instead of directly using the true and false literals (values), we commonly use expressions:

main.cs

main.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { /* A program to check if the variable `value` is in the range specified by `range_start` and `range_end` variables.*/ int value = 7; int range_start = 0; int range_end = 10; Console.WriteLine(range_start < value && value < range_end); // Output: True } } }

The OR (||) operator returns true if any one of the operands is true:

main.cs

main.cs

copy
123456789101112131415
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(true || true); // Output: True Console.WriteLine(true || false); // Output: True Console.WriteLine(false || true); // Output: True Console.WriteLine(false || false); // Output: False } } }

Following is an example which uses the OR (||) operator:

main.cs

main.cs

copy
12345678910111213141516
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int val1 = 5; int val2 = 7; int val3 = 9; Console.WriteLine(val2 > val1 || val2 > val3); // Output: True } } }

The NOT (!) operator simply negates (inverts) the logical expression or logical value. So if an expression returns true, it changes it into false.

main.cs

main.cs

copy
1234567891011121314151617
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(!true); // Output: False Console.WriteLine(!false); // Output: True // We need to enclose expressions in brackets () before negating them Console.WriteLine(!(5 < 0)); // Output: True Console.WriteLine(!(0 < 5 && 5 < 10)); // Output: False } } }
question mark

What is the expression (0 < 5 || 5 < 10) equal to?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you give more examples of how logical operators are used in real code?

What happens if I combine multiple logical operators in one expression?

Can you explain the difference between && and || with more scenarios?

bookLogiska Operatorer

Svep för att visa menyn

Logical operators can be used for combining two logical expressions or logical values. Following are the three logical operators:

Note

An operand refers to a value or expression that is used as an input for an operator in a statement or expression. For example in the expression 1 + 2, the values 1 and 2 are operands. In the case of logical operators, an operand is always a boolean expression or value.

You can see the usage of these operands in the examples below:

Let's now use them in practice:

The AND (&&) operator takes two operands and returns true only if both the operands are true. It is demonstrated by the following code:

main.cs

main.cs

copy
123456789101112131415
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(true && true); // Output: True Console.WriteLine(true && false); // Output: False Console.WriteLine(false && true); // Output: False Console.WriteLine(false && false); // Output: False } } }

Instead of directly using the true and false literals (values), we commonly use expressions:

main.cs

main.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { /* A program to check if the variable `value` is in the range specified by `range_start` and `range_end` variables.*/ int value = 7; int range_start = 0; int range_end = 10; Console.WriteLine(range_start < value && value < range_end); // Output: True } } }

The OR (||) operator returns true if any one of the operands is true:

main.cs

main.cs

copy
123456789101112131415
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(true || true); // Output: True Console.WriteLine(true || false); // Output: True Console.WriteLine(false || true); // Output: True Console.WriteLine(false || false); // Output: False } } }

Following is an example which uses the OR (||) operator:

main.cs

main.cs

copy
12345678910111213141516
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int val1 = 5; int val2 = 7; int val3 = 9; Console.WriteLine(val2 > val1 || val2 > val3); // Output: True } } }

The NOT (!) operator simply negates (inverts) the logical expression or logical value. So if an expression returns true, it changes it into false.

main.cs

main.cs

copy
1234567891011121314151617
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(!true); // Output: False Console.WriteLine(!false); // Output: True // We need to enclose expressions in brackets () before negating them Console.WriteLine(!(5 < 0)); // Output: True Console.WriteLine(!(0 < 5 && 5 < 10)); // Output: False } } }
question mark

What is the expression (0 < 5 || 5 < 10) equal to?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
some-alt