0% found this document useful (0 votes)
17 views15 pages

6-Important Operators

The document discusses important operators in C#, including conditional, logical, and boolean operators. Conditional expressions use relational operators like > and < to compare two operands. Logical operators like && and || allow for short-circuit evaluation where the second expression is only evaluated if needed. The ! operator negates a boolean expression. Boolean variables can hold true or false values and be used directly in conditional expressions without comparison.

Uploaded by

210907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views15 pages

6-Important Operators

The document discusses important operators in C#, including conditional, logical, and boolean operators. Conditional expressions use relational operators like > and < to compare two operands. Logical operators like && and || allow for short-circuit evaluation where the second expression is only evaluated if needed. The ! operator negates a boolean expression. Boolean variables can hold true or false values and be used directly in conditional expressions without comparison.

Uploaded by

210907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Important Operators

Lecture 8
Introduction
• A conditional expression is also referred to as a test condition. To
determine, for example, who receives an A on an exam, a relational
test is made between two operands.

• The first operand is an exam score, and the second is a value such as
90, or some predetermined cutoff value for an A.

• After this conditional expression is written, it can be used with


hundreds of different exam scores to determine who gets the A.
Introduction
(Contd..)
• In C#, the && and || operators are also called the short-circuiting
logical operators.

• These operators enable doing as little as is needed to produce the


final result through short-circuit evaluation.

• With short-circuit evaluation, as soon as the value of the entire


expression is known, evaluation stops. A conditional expression is
evaluated from left to right.
Introduction
(Contd..)
• With expressions involving &&, if the first evaluates as false, there is
no need to evaluate the second.

• The result will be false. With expressions involving ||, if the first
evaluates as true, there is no need to evaluate the second.

• The result will be true.


And
•Operator
To add two points to examScore that range between 70 and 90, you
might write the following pseudocode

if (examScore > 69 AND examScore < 91)


examScore = examScore + 2

When expressions are combined with AND, both expressions must


evaluate to true for the entire compound expression to return true.
Exampl
e
It is also incorrect to write:
(examScore > 69 < 91)
//Invalid

It is also incorrect to write:


(69 < examScore < 91) //Invalid

The correct way to write the expression is: //Correct way


((examScore > 69) && (examScore < 91))
OR
Operator
• || only returns a false result when both of the expressions or
operands are false.

• At all other times, when either one of the expressions or operands


evaluates to true, the entire compound expression evaluates to true.
Exampl
e
Compound expressions using the || must also have a complete
expression on both sides of the logical symbol.

The following is invalid:


(letterGrade == 'A' || 'B') //Invalid

The correct way to write the conditional expression is:


((letterGrade == 'A') || (letterGrade == 'B')) //Correct way
Not Operator
• The ! symbol is the logical negation operator.
• It is a unary operator that negates its operand and is called the NOT
operator.
• It returns true when the expression or operand is false. It returns false
when the expression or operand is true.

Given the following declaration:


char letterGrade = 'A';
Exampl
e
• When the NOT operator (!) is placed in front of the conditional
expression, the statement returns false, as shown in the following
line:
( ! (letterGrade == 'A'))
• The conditional expression first yields a true result (letterGrade is
equal to 'A').

• Adding the NOT operator ! negates that result. Thus, the expression
evaluates to false.
Consider this Example for && and ||
Boolean Operator
• A variable declared to be of bool type holds the value of true or false.

• When a bool variable is used in a conditional expression, you do not have to


add symbols to compare the variable against a value.

• Boolean data types are often used as flags to signal when a condition exists
or when a condition changes.

• You simply write the bool variable name as the expression


bool salariedEmployee;
Boolean Operator (contd..)
• After being declared, a conditional expression could be used to
determine whether salariedEmployee held the values true or false.

• The conditional expression would read:


if (salariedEmployee)
• It is not necessary to write if (salariedEmployee is equal to true)
because salariedEmployee is defined as a bool type.
Boolean Operator
(Contd..)
• To create a flag situation, declare a bool data type variable and initialize it
to either true or false.

• Then use this variable to control processing.

• As long as the value of the Boolean variable remains the same, processing
continues. When some planned condition changes, the Boolean data type
is changed and processing stops.

• For example, moreData is declared as a Boolean, is initialized to true, but


can be changed to false when all of the data has been processed.
Exampl
e

You might also like