02 iec t1s1 oops session 02
TRANSCRIPT
-
8/14/2019 02 Iec t1s1 Oops Session 02
1/25
Slide 1 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
In this session, you will learn to:
Use various operators:
Arithmetic
Arithmetic Assignment
Unary
Comparison
Logical
Use conditional constructs
Use looping constructs
Objectives
-
8/14/2019 02 Iec t1s1 Oops Session 02
2/25
Slide 2 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Applications use operators to process the data entered by a
user.
Operators in C# can be classified as follows:
Arithmetic operators
Arithmetic Assignment operatorsUnary operators
Comparison operators
Logical operators
Using Operators
-
8/14/2019 02 Iec t1s1 Oops Session 02
3/25
Slide 3 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Arithmetic operators are the symbols that are used to
perform arithmetic operations on variables.
The following table describes the commonly used arithmetic
operators.
Arithmetic Operators
Operator Description Example
+ Used to add two numbers X=Y+Z;If Y is equal to 20 and Z is equal to 2, X will have thevalue 22.
- Used to subtract twonumbers
X=Y-Z;If Y is equal to 20 and Z is equal to 2, X will have thevalue 18.
* Used to multiply twonumbers
X=Y*Z;If Y is equal to 20 and Z is equal to 2, X will have thevalue 40.
/ Used to divide one number by another
X=Y/Z;If Y is equal to 21 and Z is equal to 2, X will have thevalue 10.But, if Y is equal to 21.0 and Z is equal to 2, X will havethe value 10.5.
% Used to divide twonumbers and return theremainder
X=Y%Z;If Y is equal to 21 and Z is equal to 2, X will contain thevalue 1.
-
8/14/2019 02 Iec t1s1 Oops Session 02
4/25
Slide 4 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Arithmetic assignment operators are used to perform
arithmetic operations to assign a value to an operand.
The following table lists the usage and describes the
commonly used assignment operators.
Arithmetic Assignment Operators
Operator Usage Description
= X = 5; Stores the value 5 in the variable X.
+= X+=Y; Same as:X = X + Y;
-= X-=Y; Same as:X = X - Y;
*= X*=Y; Same as:
X = X * Y;
/= X/=Y; Same as:X = X / Y;
%= X%=Y; Same as:X = X % Y;
-
8/14/2019 02 Iec t1s1 Oops Session 02
5/25
Slide 5 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Unary operators are used to increment or decrement the
value of an operand by 1.
The following table explains the usage of the increment and
decrement operators.
Unary Operators
Operator Usage Description Example
++ ++Operand;(Preincrement operator)Or,Operand++; (Postincrementoperator)
Used to incrementthe value of anoperand by 1
Y = ++X;If the initial value of X is 5, after the executionof the preceding statement, values of both Xand Y will be 6.Y = X++;If the initial value of X is 5, after the executionof the preceding statement, value of X will be 6and the value of Y will be 5.
-- --Operand;(Predecrement operator)Or,Operand--; (Postdecrement)
Used to decrementthe value of anoperand by 1
Y = --X;If the initial value of X is 5, after the executionof the preceding statement, values of X and Ywill be 4.Y = X--;If the initial value of X is 5, after the executionof the preceding statement, value of X will be 4and the value of Y will be 5.
-
8/14/2019 02 Iec t1s1 Oops Session 02
6/25
Slide 6 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Comparison operators are used to compare two values and
perform an action on the basis of the result of that
comparison.
The following table explains the usage of commonly used
comparison operators.
Comparison Operators
Operator Usage Description Example(In the following examples, the value of X isassumed to be 20 and the value of Y isassumed to be 25)
< expression1 expression1 >expression2
Used to check whether expression1is greater than expression2
bool Result;Result = X > Y;Result will have the value false.
= Y;Result will have the value false.
-
8/14/2019 02 Iec t1s1 Oops Session 02
7/25Slide 7 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Comparison Operators (Contd.)
Operator Usage Description Example(In the following examples, the value of X isassumed to be 20 and the value of Y isassumed to be 25)
== expression1 ==
expression2
Used to check whether
expression1 is equal toexpression2
bool Result;
Result = X == Y;Result will have the value false.
!= expression1 !=expression2
Used to check whetherexpression1 is not equal toexpression2
bool Result;Result = X != Y;Result will have the value true.
-
8/14/2019 02 Iec t1s1 Oops Session 02
8/25Slide 8 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Logical operators are used to evaluate expressions andreturn a Boolean value.
The following table explains the usage of logical operators.
Logical Operators
Operator Usage Description Example
&& expression1 &&expression2
Returns true if bothexpression1 and
expression2 aretrue.
bool Result;string str1, str2;
str1 = Korea;str2 = France;Result= ((str1==Korea) &&(str2==France))
Console.WriteLine (Result .ToString());
The message displays True because str1has the value Korea and str2 has thevalue France.
! ! expression Returns true if the
expression is false.
bool Result
int x;x = 20;Result=(!( x == 10))
Console.WriteLine(Result.ToString());The message displays True because theexpression used returns true.
-
8/14/2019 02 Iec t1s1 Oops Session 02
9/25Slide 9 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Logical Operators (Contd.)
Operator Usage Description Example
|| expression1 ||expression2
Returns true if eitherexpression1 orexpression2 or both ofthem are true.
bool Resultstring str1, str2;str1 = Korea;str2 = England;Result= ((str1==Korea) || (str2== France))
Console.WriteLine (Result .ToString());The message displays True if either str1 has thevalue Korea or str2 has the value France.
^ expression1 expression2
Returns true if eitherexpression1 orexpression2 is true. Itreturns false if bothexpression1 andexpression2 are true or ifboth expression1 andexpression2 are false.
bool Result;string str1, str2;str1 = Korea;str2= France;Result = (str1== Korea) ^ (str2== France);Console.WriteLine (Result .ToString());The message False is displayed because both theexpressions are true.
-
8/14/2019 02 Iec t1s1 Oops Session 02
10/25Slide 10 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Using Conditional Constructs
Conditional constructs allow the selective execution of
statements, depending on the value of expression
associated with them.
The comparison operators are required for evaluating the
conditions.The various conditional constructs are:
The ifelse construct
The switchcase construct
-
8/14/2019 02 Iec t1s1 Oops Session 02
11/25Slide 11 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
The ifelse conditional construct is followed by a logical
expression where data is compared and a decision is made
on the basis of the result of the comparison.
The following is the syntax of the ifelse construct:
if (expression){
statements;
}
else
{statements;
}
The ifelse Construct
-
8/14/2019 02 Iec t1s1 Oops Session 02
12/25Slide 12 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
The ifelse constructs can be nested inside each other.
When ifelse construct is nested together, the construct
is known as cascading ifelse constructs.
The ifelse Construct (Contd.)
-
8/14/2019 02 Iec t1s1 Oops Session 02
13/25Slide 13 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
The switchcase construct is used when there are
multiple values for a variable.
The following is the syntax of the switchcase construct:
switch (VariableName)
{case ConstantExpression_1:
statements;
break;
case ConstantExpression_2:
statements;break;
The switchcase Construct
-
8/14/2019 02 Iec t1s1 Oops Session 02
14/25Slide 14 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
case ConstantExpression_n:
statements;
break;
default:statements;
break;
}
The switchcase Construct (Contd.)
-
8/14/2019 02 Iec t1s1 Oops Session 02
15/25Slide 15 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Problem Statement:
Write a program that emulates a calculator. The calculator
should be able to perform the following mathematical
operations:
Addition
Subtraction
Multiplication
Division
Demo: Calculator Using Conditional Constructs ConditionalConstructs
-
8/14/2019 02 Iec t1s1 Oops Session 02
16/25Slide 16 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Using Loop Constructs
Loop structures are used to execute one or more lines of
code repetitively.
The following loop constructs are supported by C#:
The while loop
The dowhile loopThe for loop
-
8/14/2019 02 Iec t1s1 Oops Session 02
17/25Slide 17 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
The while Loop
The while loop construct is used to execute a block of
statements for a definite number of times, depending on a
condition.
The following is the syntax of the while loop construct:
while (expression){
statements;
}
-
8/14/2019 02 Iec t1s1 Oops Session 02
18/25Slide 18 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
The dowhile Loop
The dowhile loop construct is similar to the while loop
construct.
Both iterate until the specified loop condition becomes false.
The following is the syntax of the dowhile loop construct:
do{
statements;
}while(expression);
-
8/14/2019 02 Iec t1s1 Oops Session 02
19/25Slide 19 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
The dowhile Loop (Contd.)
The following figure shows the difference between the do
while and while loop construct.
False
do while
False
True
Execute body of
Loop
Evaluate
Condition
True
Execute body of
Loop
EvaluateCondition
while
-
8/14/2019 02 Iec t1s1 Oops Session 02
20/25Slide 20 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
The for Loop
The for loop structure is used to execute a block of
statements for a specific number of times.
The following is the syntax of the for loop construct:
for (initialization; termination;
increment/decrement)
{
statements
}
-
8/14/2019 02 Iec t1s1 Oops Session 02
21/25Slide 21 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
The for Loop (Contd.)
The following figure shows the sequence of execution of
a complete for loop construct.
True
False
Initialization
Evaluate
Condition
Body of the
for Loop
Exit the for
Loop
Increment/
Decrement
-
8/14/2019 02 Iec t1s1 Oops Session 02
22/25Slide 22 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
Demo: Fibonacci Series Using Loop Constructs
Problem Statement:
Write a program that generates the Fibonacci series up to 200.
-
8/14/2019 02 Iec t1s1 Oops Session 02
23/25
Slide 23 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
The break and continue Statements
The break statement is used to exit from the loop and
prevents the execution of the remaining loop.
The continue statement is used to skip all the subsequent
instructions and take the control back to the loop.
-
8/14/2019 02 Iec t1s1 Oops Session 02
24/25
Slide 24 of 25Session 2Ver. 1.0
Object-Oriented Programming Using C#
In this session, you learned that:
Operators are used to compute and compare values and test
multiple conditions.
You use arithmetic operators to perform arithmetic operations
on variables like addition, subtraction, multiplication, and
division.
You can use arithmetic assignment operators to perform
arithmetic operations and assign the result to a variable.
The unary operators, such as the increment and decrement
operators, operate on one operand.
Comparison operators are used to compare two values andperform an action on the basis of the result of the comparison.
Logical operators are used to evaluate expressions and return
a Boolean value.
Summary
-
8/14/2019 02 Iec t1s1 Oops Session 02
25/25
Object-Oriented Programming Using C#
Conditional constructs are used to allow the selective
execution of statements. The conditional constructs in C# are:
ifelse
switchcase
Looping constructs are used when you want a section of a
program to be repeated a certain number of times. C# offersthe following looping constructs:
while
dowhile
for
The break and continue statements are used to control theprogram flow within a loop.
Summary (Contd.)