C# Logical Operators
bool both = true && true; stores the word true, and Console.WriteLine(both) writes true. The screen is one line. The two ampersands sit between the two true words in the file, and they are not on that line. Keep the public class named Pine.
Two ampersands need both sides true
Open the Pine file. The class name stays Pine. This page does not rename the type. Delete bool match if that line is still there, including 4 == 4, 4 == 9, and every ask mark between two whole numbers. Delete int total if that line is still there. Delete string[] plants if that line is still there. Delete bool ripe if that line is still there with a bare true or bare false and no combining mark. Delete any ReadLine line. The file you keep has no wait, no match, no total, no plants, and no bare ripe line. The word is written only by the writing line that uses the name that holds the combined answer.
Inside Main, the storing line is bool both = true && true;. bool is the kind word. It says this name holds one of two words, true or false. both is the name, and it starts with a small b. The first = sign stores. On the right of that first = sit two true or false words and one combining mark. The first word is true, every letter small, with no quotes. The combining mark is two ampersands written together with no space between them: &&. The second word is true, every letter small, with no quotes. The semicolon ends the line. This line stores one word. It does not write the screen. Storing is not writing. The screen stays blank through this line. The combining mark does not ask whether two whole numbers match. It does not make a third whole number. It looks at two answers that are already true or false, and it makes one answer from them.
Two ampersands mean both sides must be true. When the left side is true and the right side is true, the combined word is true. When either side is false, the combined word is false. bool both = true && false; stores false. bool both = false && true; stores false. bool both = false && false; stores false. The file you keep uses true && true, so the stored word is true.
A combining mark is not the same work as an ask mark. The comparison page put == between two whole numbers. That ask made true or false from the numbers. This page puts && between two values that are already true or false. The operators page put + between two whole numbers to make a third whole number. && does not make a third whole number. If you write bool both = 4 && 9;, the build writes an error on this page's lesson shape, because the sides this page keeps are true or false, not bare whole numbers. Keep true and false on both sides for the file you keep.
The writing line is the bare name: Console.WriteLine(both);. No quotes, and no combining mark on that line. C# reads the name both, writes the word that name holds, and ends the line. For the file you keep, that word is true. The letters of the name, b, o, t, h, are not on the screen. bool is not on the screen. The two ampersands are not on the screen. The separate true words are not on the screen as two lines. The word true is on the screen, as one line. Save the file.
The file you keep
Keep this teaching shape. One kind word, bool. One name, both. One storing line that writes true, then two ampersands, then true, with no quotes around either word. One writing line, and that line uses the bare name. There is no match line, no total line, and no second writing line.
using System;
public class Pine
{
public static void Main()
{
bool both = true && true;
Console.WriteLine(both);
}
}
Read it from the top. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. bool both = true && true; stores the word true. It writes nothing. Console.WriteLine(both); writes true and ends the line. One line on the screen. No quotes. No second line. There is no bool match line in the file you keep, and there is no line that asks whether 4 matches 4.
Run it with dotnet run. The program writes true. It does not wait for you to type. The two true words and the combining mark were already in the file. If dotnet run waits, ReadLine is still in the file. Delete that line and keep bool both = true && true; and the one writing line. The program finishes after the writing line. The screen shows true. One line. No quotes. If the screen shows both, the writing line has quotes around the name: Console.WriteLine("both");. Those quotes mark letters, so the stored word is not written. If the screen shows true from bool match = 4 == 4;, the comparison file is still in place. Delete the match line. If the screen shows false, one side of && is false, which is allowed to check, and which is not the file you keep. If the screen shows true && true as letters, the writing line has quotes around those characters. You do not reinstall the .NET SDK for a screen that shows the wrong line.
A screen that shows true does not, by itself, prove the combining mark. bool both = true; also writes true. The screen can look the same. The file does not. The file you keep has true, then &&, then true. When you judge this page, look for the two ampersands between the two true words. Do not judge the file from the screen alone.
Spaces around the combining mark are allowed. bool both = true&&true; with no spaces still stores true, and the build writes no error. bool both = true && true; with spaces still stores true. The file you keep uses spaces around the mark, because the mark is easier to see that way. The spaces are not a second value. A space between the two ampersands is not allowed. bool both = true & & true; makes the build write an error. Keep the two ampersands together as &&. One ampersand alone is not the mark this page keeps. bool both = true & true; is not the file you keep. The build may write an error, or the line is not the lesson this page teaches. Write two ampersands.
Quotes around true turn it into letters. bool both = "true" && true; asks C# to combine letters with a true or false word in a way this page does not teach. The build writes an error. Delete the quotes. Both sides of &&, on this page, are the words true or false with no quotes. Console.WriteLine(true && true); with no both name at all also writes true. The screen can look the same. The file you keep stores the combined answer under a name first, then writes that name.
The name has to exist before a line uses it. If Console.WriteLine(both); is written above bool both = true && true;, the build writes an error. Move the storing line above the writing line. Do not write bool a second time for the same name. A later line can replace the word without writing bool again, once both already exists: both = true && true;. That form is allowed. It is not required. The file you keep creates the name and stores the combined answer on one line.
When one side is false
Change only the right word. Keep the kind word bool. Keep the name both. Keep the mark as two ampersands. bool both = true && false; has a true left side and a false right side. Two ampersands need both sides true. The combined word is false. The writing line is still Console.WriteLine(both);. The screen shows false. One line. The name both is not on the screen. The separate words true and false are not on the screen as two lines.
Run dotnet run. The screen shows false. After you have seen false, put the kept file back: bool both = true && true; and one Console.WriteLine(both);. The kept screen is true, one line. This false run is a check. It is not the file you keep.
Two pipes need one side true
A different combining mark uses two pipe marks written together: ||. Two pipes mean at least one side must be true. bool either = true || false; stores true. bool either = false || true; stores true. bool either = true || true; stores true. bool either = false || false; stores false. The name can be either. The kind word stays bool.
using System;
public class Pine
{
public static void Main()
{
bool either = true || false;
Console.WriteLine(either);
}
}
This is the second file. It is not the file you keep. Run dotnet run. The screen shows true. One line. It does not show false. It does not show true || false as letters. If the screen shows false, both sides may still be false, as in bool either = false || false;. That line is allowed, and it is not this second file. After you have seen true from true || false, put the kept file back: bool both = true && true; and Console.WriteLine(both);.
Two ampersands and two pipes are not the same work. && is strict. Both sides must be true. || is open. One true side is enough. The file you keep teaches &&. The second file only shows ||. Do not leave || in the kept file. Do not write one pipe alone. bool either = true | false; is not the mark this page keeps for the open combine.
A bang flips the word
A bang mark sits against one true or false word and flips it. The mark is !. bool flipped = !true; stores false. bool flipped = !false; stores true. There is no second side. The bang is not && and it is not ||. It does not sit between two words. It sits against one word.
using System;
public class Pine
{
public static void Main()
{
bool flipped = !true;
Console.WriteLine(flipped);
}
}
This is the third file. It is not the file you keep. Run dotnet run. The screen shows false. One line. It does not show true. It does not show !true as letters. If the screen shows true, the storing line may be bool flipped = !false;, which flips false into true. That line is allowed, and it is not this third file. After you have seen false from !true, put the kept file back.
Writing the combining mark as letters does not combine. Console.WriteLine("true && true"); writes the characters of that text. The screen shows true && true. That is not the word true from the kept combine. The build stays quiet. The quotes are what stop the mark from working as a mark. The file you keep has no quotes around the true words, and the writing line has no quotes around the name.
You can now store one true or false answer made from two true or false answers, and write that word. bool both = true && true; stores true. Console.WriteLine(both); writes true and ends the line. true && false writes false. true || false writes true. !true writes false. The next page leaves these combining marks on their own. It looks at if and else, which choose a path from a true or false answer.
The screen is the combined word
The file you keep writes true. The name on the writing line is both, and both is not on the screen. The two ampersands are in the file, and they are not on the screen. The two true words are in the file, and they are not on the screen as separate lines. The pipe mark and the bang mark are not the file you keep. Quotes around the mark write letters, not the combined word.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top