Home
C#
Introduction to C#
.NET Framework
VS2008 IDE
How C# is Compiled
Start Visual Studio
Windows Form App
C# OOP Coding
C# Code Example
C# Coding Rules
Recommendations
Error Window
C# Comments
C# Help Window
Language Essentials
C# Data Types
Declare Variables
Declare Constants
Arith. Expressions
Assign Statements
Operator Precedence
Type Casting
Math Class
C# Strings
String Esc Sequences
Convert Data Types
ToString Formatting
Variable Scope Use
C# Enumeration Use
C# Nullable Types
Program #2
C# Code Control
Relational Operators
Logical Operators
If-Else Statement
Switch Statement
|
In order to code control structures in C#, it is necessary to understand how Boolean
Expressions are handled in C#.
There are 6 relational operators in C#. These operators test for equality, inequality,
and relation between the expressions on either side of the operators.
|
Operator
|
Name
|
Description
|
|
= =
|
Equality
|
Returns a true value if the left and right operands are equal.
|
|
! =
|
Inequality
|
Returns a true value if the left and right operands are not equal.
|
|
>
|
Greater Than
|
Returns a true value if the left operand is greater than the right operand.
|
|
<
|
Less Than
|
Returns a true value if the left operand is less than the right operand.
|
|
> =
|
Greater Than or Equal
|
Returns a true value if the left operand is greater than or equal to the right operand.
|
|
< =
|
Less Than or Equal
|
Returns a true value if the left operand is less than or equal to the right operand.
|
Examples
FirstName = = "Frank" // equal to a string literal
txtYears.Text = = " " // equal to an empty string
Message = = null // equal to a null value
DiscountPercent = = 2.3 // equal to a numeric literal
IsValid = = false // equal to the false value
Code = = ProductCode // equal to another variable
LastName ! = "Jones" // not equal to a string literal
Years > 0 // greater than a numeric literal
i < Months // less than a variable
Subtotal > = 500 // greater than or equal to a literal value
Quantity < = ReorderPoint // less than or equal to a variable
|
Description
- You can use the relational operators to create a Boolean expression that compares two
operands and returns a Boolean value.
- To compare two operands for equality, make sure you use two equals signs. If you use a single equals
sign, the compiler will interpret it as an assignment statement, and your code won't compile.
- When comparing strings, you can only use the equality and inequality operators.
- If you compare two numeric operands with different data types, C# will cast the less precise operand
to the type of the more precise operand.
|
Figure CS-54: C# Relational Operator Table
|
Logical Operators perform Boolean logic on two expressions. There are 3 types of logical
operators in C#: bitwise, Boolean and conditional.
|
Operator
|
Name
|
Description
|
|
&&
|
Conditional - And
|
Returns a true value if both expressions are true. This operator only evaluates the second expression if
necessary.
|
|
||
|
Conditional - Or
|
Returns a true value if either expression is true. This operator only evaluates the second expression if
necessary.
|
|
&
|
And
|
Returns a true value if both expressions are true. This operator always evaluates both expressions.
|
|
|
|
Or
|
Returns a true value if either expression is true. This operator always evaluates both expressions.
|
|
!
|
Not
|
Reverses the value of the expression.
|
Examples
Subtotal > = 250 && Subtotal < 500
TimeInService < = 4 || TimeInService > = 12
IsValid = = true & Counter ++ < Years
IsValid = = true | Counter -- > Years
Date > StartDate && Date < ExpirationDate || IsValid = = true
( ( ThisYTD > LastYTD ) || EmployeeType = = " Part Time " ) && StartYear < CurrentYear )
! ( Counter ++ > = Years )
|
Description
- You can use the logical operators to create a Boolean expression that combines two or more
Boolean expressions.
- Since the && and || operators only evaluate the second expression if necessary, they're sometimes
referred to as short-circuit operators. These operators are slightly more efficient than
the & and | operators.
- By default, Not operations are performed first, followed by And operations, and then Or
operations. These operations are performed after arithmetic operations and relational operations.
- You can use parentheses to change the sequence in which the operations will be performed or to
clarify the sequence of operations.
|
Figure CS-55: C# Logical Operators, Examples and Description
|
By far the most common decision-making construct used in programming is the if construct.
The if construct uses Boolean logic to evaluate an expression to either true or false. If the expression evaluates to true,
the statement or block of statements (enclosed in braces) gets executed. If the expression evaluates to false, C# doesn't execute
the statement of statement block for the if construct.
The syntax of the if-else statement:
if ( booleanExpression ) { statements }
[else if ( booleanExpression ) { statements } ] ...
[else { statements } ]
|
If Statements without Else If or Else Clauses
With a Single Statement
if ( Subtotal > = 100 )
DiscountPercent = .2;
With a Block of Statements
if ( Subtotal > = 100 )
{
DiscountPercent = .2;
Status = "Bulk Rate";
}
|
An If Statement with an Else Clause
if ( Subtotal > = 100 )
DiscountPercent = .2;
else
DiscountPercent = .1;
|
An If Statement with Else If and Else Clauses
if ( Subtotal > = 100 && Subtotal < 200 )
DiscountPercent = .2;
else if ( Subtotal > = 200 && Subtotal < 300 )
DiscountPercent = .3;
else if ( Subtotal > = 300 )
DiscountPercent = .4;
else
DiscountPercnet = .1;
|
Nested If Statements
if ( CustomerType = = "R" )
{ // begin nested if
if ( Subtotal > = 100 )
DiscountPercent = .2;
else
DiscountPercent = .1;
} // end nested if
else // customer type isn't "R"
{
DiscountPercent = .4;
}
|
Description
- An if-else statement, or just if statement, always contains an if clause.
In addition, it can contain one or more else if clauses and a final else clause.
- If a clause requires just one statement, you don't have to enclose the statement in braces.
You can just end the clause with a semicolon.
- If a clause requires more than one statement, you enclose the block of statements in braces.
|
Figure CS-56: C# if-else Construct Syntax, Examples and Description
|
At times, the if construct isn't capable of handling a decision situation without a
great deal of work. One such situation is when you need to perform different actions based on numerous possible values of
a single expression, not just true or false. When such a situation arises, C# includes a much better decision making
construct for evaluating a single expression that can contain multiple values: the switch statement.
The Syntax of the Switch Statement:
switch ( SwitchExpression )
{
case Constant Expression:
Statements
break;
[case Constant Expression:
Statements
break;]. . .
[default:
Statements
break;]
}
|
A Switch Statement with a Default Label
switch ( CustomerType )
{
case "R":
DiscountPercent = .1;
break;
case "C"
DiscountPercent = .2;
break;
default:
DiscountPercent = 0;
break;
}
|
A Switch Statement that Falls through the First Case Label:
switch ( CustomerType )
{
case "R":
case "C":
DiscountPercent = .2;
break;
case "T":
DiscountPercent = .4;
break;
}
|
Description:
- A switch statement begins by evaluating its switch expression. This expression must evaluate
to a string, char, long sbtye, shorth, ushort, int, uint, long, or ulong type.
- After evaluating the switch expression, the switch statement transfers control to the appropriate
case label. If control isn't transferred to one of the case labels, the optional
default label is executed.
- The break statement exits the switch statement. If a label contains one or more statements,
the label must end with a break statement.
- If a label doesn't contain any statements, code execution will fall through to the next label.
That means that the statements contained by the next label will be executed.
|
Figure CS-57: C# switch Construct Syntax, Examples and Description
|
|
|