C# Booleans
You will frequently need a data type in programming that can only contain one of two values, such as:
- YES / NO
- ON / OFF
- TRUE / FALSE
Boolean Values
The bool keyword is used to declare a boolean type, which can only accept the values true or false :
Example
Boolean Expression
Example
Alternatively, even simpler:
Example
Example
Real Life Example
The following example shows how to use the >= comparison operator to determine whether the age of 25 is greater than
or equal to the 18-year-old voting age limit:
Example
Example
If myAge is equal to or greater than 18, then output “Old enough to vote!” In the event when not, output “Not old enough to vote.”
int myAge = 25;
int votingAge = 18;
if (myAge >= votingAge)
{
Console.WriteLine(“Old enough to vote!”);
}
else
{
Console.WriteLine(“Not old enough to vote.”);
}
C# Conditions and If Statements
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to a == b
- Not Equal to: a != b
- Use if to declare a code block that will run if a given condition is met.
- If the same condition is false, use else to designate a block of code to be run.
- If the first condition is false, use else if to define a new condition to test.
- To define numerous alternative code blocks to be performed, use the switch command.
if Statement
if(condition)
{
//block of code to be executed if the given condition is true
}
We test two variables in the example below to see if 20 is bigger than 18. Print some text if the condition is true:
Example
Example
An explanation of an example
The else Statement
Syntax
if(condition)
{
//block of code to be executed if the given condition is true
}
else
{
//block of code to be executed if the given condition is true
}
Example
An explanation of an example
else if Statement
Syntax
if(condition1)
{
//block of code to be executed if the given condition is true
}
else if (condition2)
{
/ /block of code to be executed if the given condition 1 is false and condition2 is True
}
else
{
// block of code to be executed if the condition1 is false and condition2 is False
}
Example
An explanation of an example
Because time (22) in the case above is larger than 10, the first condition is false. After determining that both conditions 1 and 2 in the else if statement are False, we proceed to the else condition and print “Good evening” on the screen.
Short Hand If…Else (Ternary Operator)
Syntax
As an alternative to writing:
Example
You could just write:
Example
C# Switch Statements
Syntax
Here’s how it functions:
- One evaluation of the switch expression is performed.
- The expression’s value is contrasted with each case‘s value.
- The related block of code is run if there is a match.
- Later in this chapter, the break and default keywords will be explained.
Example
break Keyword
default Keyword
If a case match is not found, the default keyword, which is optional, indicates what code should be executed:
Example
Loops
C# While Loop
Syntax
Example
Do/While Loop
Syntax
do { //code to be executed } while(condition) A do/while loop is used in the example below. Because the code block runs before the condition is tested, the loop will always run at least once, even if the condition is false:Example
For Loop
Syntax
Prior to the code block being performed, Statement 1 is run (once).
The requirement for carrying out the code block is specified in Statement 2.
Every time, statement 3 is run following the execution of the code block.
The following example will display the digits 0 through 4:
Example
Example explained
The requirement for the loop to execute ( i must be less than 5) is mentioned in Statement 2. The loop will restart if the condition is true and will finish if it is false.
After every execution of the loop’s code block, Statement 3 raises a value ( i++ ).
One more Example
The values in this example are limited to even numbers between 0 and 10.
Example
Nested Loops
It’s also feasible to nest one loop inside of another. This is termed a nested loop.
For every time the “outer loop” iterates, the “inner loop” will run once: