0% found this document useful (0 votes)
16 views

3 - NPL - Method, Operators, Conditional Statement

The document discusses .NET programming concepts including methods, operators, and conditional statements. It provides an agenda that covers methods, operators, and conditional statements. It then discusses the objectives of understanding basic concepts of high-level programming languages. It proceeds to sections that explain operators and different types of statements in C# like declaration statements, expression statements, selection statements, and more. It also covers various types of operators used in C# like arithmetic, relational, logical, and assignment operators.

Uploaded by

binhkatekun2002
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

3 - NPL - Method, Operators, Conditional Statement

The document discusses .NET programming concepts including methods, operators, and conditional statements. It provides an agenda that covers methods, operators, and conditional statements. It then discusses the objectives of understanding basic concepts of high-level programming languages. It proceeds to sections that explain operators and different types of statements in C# like declaration statements, expression statements, selection statements, and more. It also covers various types of operators used in C# like arithmetic, relational, logical, and assignment operators.

Uploaded by

binhkatekun2002
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 71

.

NET Programming
Language
Method, Operators, Conditional statement

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use


Before start
 Understand variable, data type
 Visual Studio readiness

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 2


Agenda

1. Method

2. Operators

3. Conditional statements

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 3


Lesson Objectives

Understand basic conceptsThis


of high-level
is a sampleprogramming
text that you can edit.
languages You can change font(size, color, name),
or apply any desired formatting.

01
This is a sample text that you can edit.
03 You can change font(size, color, name),
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 4
or apply any desired formatting.
Section 1

Operators

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 5


Statements and expressions
 A C# program is a set of tasks that perform to achieve the overall
functionality of the program.
 To perform the tasks, programmers provide instructions. These instructions
are called statements.
 A C# statement can contain expressions that evaluates to a value.
 In C#, a statement ends with a semicolon.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 6


Statements
 Statements are used to specify the input, the process, and the output tasks
of a program. Statements can consist of:
 Data types
 Variables
 Operators
 Constants
 Literals
 Keywords

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 7


Statements
 Statements help you build a logical flow in the program. With the help of
statements, you can:
 Initialize variables and objects
 Take the input
 Call a method of a class
 Display the output

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 8


Types of Statements

Declaration Expression Selection Iteration Jump


statements statements statements statements statements
• introduces • calculate a • if • do • break
a new value must • else • for • continue
variable or store the • switch • foreach • default
constant. value in a • case • in • goto
variable. • •
while return
• yield

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 9


Types of Statements

Exception handling
statements
The fix statement
Checked and unchecked The lock statement

The await statement


Labelled statement

The empty statement


The yield return statement

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 10


C# Operators

An operation is an action
performed on single or multiple
Expressions in C# comprise one or The symbol is called an operator
values stored in variables in order
more operators that performs some and it determines the type of action
to modify them or to generate a
operations on variables. to be performed on the value.
new value with the help of
minimum one symbol and a value.

An operand might be a complex


The value on which the operation expression. For example, (X * Y) +
is to be performed is called an (X – Y) is a complex expression,
operand. where the + operator is used to join
two operands.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 11


Type of Operators
 Operators are used to simplify expressions.
 In C#, there is a predefined set of operators used to perform various types
of operations.
 These are classified into six categories based on the action they perform on
values:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Conditional Operators
 Increment and Decrement Operators
 Assignment Operators
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 12
Arithmetic Operators
 Arithmetic operators are binary operators because they work with two
operands, with the operator being placed in between the operands.
 These operators allow you to perform computations on numeric or string
data.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 13


Arithmetic Operators

Operators Description Examples


+ (Addition) Performs addition. If the two operands are strings, then it functions as a 40 + 20
string concatenation operator and adds one string to the end of the
other.
- (Subtraction) Performs subtraction. If a greater value is subtracted from a lower value, 100 - 47
the resultant output is a negative value.
* Performs multiplication. 67 * 46
(Multiplication)
/ (Division) Performs division. The operator divides the first operand by the second 12000 / 10
operand and gives the quotient as the output.
% (Modulo) Performs modulo operation. The operator divides the two operands and 100 % 33
gives the remainder of the division operation as the output.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 14


Arithmetic Operators
 With Division: data type and value of result is based on operand
 int/int -> int (floor).
• Example: 10/3 = 3

 decimal/int -> decimal


 float/int -> float
 …..

 Check and convert data type first


 Always check the second operand to avoid DivideByZero

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 15


Operators Precedence - 1
Category Operator Associativity

Multiplicative * / % Left to right, top to


bottom
Additive + - Left to right, top to
bottom
Use parentheses () to change the order of evaluation imposed by operator
associativity
Example:
int a = 13 / 5 / 2; //// a = 1
int b = 13 / (5 / 2); //// b = 6

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 16


Relational Operators
 Relational operators make a comparison between two operands and return a Boolean
value, true, or false.
Relational Description Examples
Operators
== Checks whether the two operands are identical. 85 == 95
!= Checks for inequality between two operands. 35 != 40
> Checks whether the first value is greater than the second 50 > 30
value.
< Checks whether the first value is lesser than the second 20 < 30
value.
>= Checks whether the first value is greater than or equal to 100 >= 30
the second value.
<= Checks whether the first value is lesser than or equal to 75 <= 80
the second value.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 17
Logical operators
 Logical operators are binary operators that perform logical operations on
two operands and return a Boolean value.

Logical Operators

Boolean Logical
Operators Bitwise Logical Operators

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 18


Logical negation operator !
 The unary prefix ! operator computes logical negation of its operand. That
is, it produces true, if the operand evaluates to false, and false, if the
operand evaluates to true:

a !a
true false
false true

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 19


Logical AND operator &
 The & operator computes the logical AND of its operands. The result of x &
y is true if both x and y evaluate to true. Otherwise, the result is false.

x y y&y
true true true
true false false
false true false
false false false

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 20


Logical OR operator |
 The | operator computes the logical OR of its operands. The result of x | y is
true if either x or y evaluates to true. Otherwise, the result is false.

x y y&y
true true true
true false true
false true true
false false false

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 21


Logical exclusive OR operator ^
 The ^ operator computes the logical exclusive OR, also known as the
logical XOR, of its operands.

x y x^y
true true false
true false true
false true true
false false false

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 22


Conditional logical operator
 There are two types of conditional operators, conditional AND (&&) and
conditional OR (||).
 Conditional operators are similar to the boolean logical operators but have
the following differences:
 The conditional AND operator evaluates the second expression only if the first
expression returns true because this operator returns true only if both expressions
are true.
 The conditional OR operator evaluates the second expression only if the first
expression returns false because this operator returns true if either of the
expressions is true.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 23


Ternary or Conditional Operator ?:
 If the first expression returns a true value, the second expression is
evaluated, whereas if the first expression returns a false value, the third
expression is evaluated.
 Syntax: <Expression 1> ? <Expression 2>: <Expression 3>;
 Expression 1:Is a bool expression.
 Expression 2: Is evaluated if expression 1 returns a true value.
 Expression 3: Is evaluated if expression 1 returns a false value

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 24


Bitwise Logical Operators
 The bitwise logical operators perform logical operations on the
corresponding individual bits of two operands.
 The following table lists the bitwise logical operators along with their
descriptions and an example of each type:

Logical Operators Description Examples


& (Bitwise AND) Compares two bits and returns 1 if both bits are 00111000 &
1, else returns 0. 00011100
| (Bitwise Inclusive Compares two bits and returns 1 if either of the 00010101 |
OR) bits is 1. 00011110
^ (Bitwise Exclusive Compares two bits and returns 1 if only one of 00001011 ^
OR) the bits is 1. 00011110

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 25


Bitwise Logical Operators
 The bitwise AND operator compares the
corresponding bits of the two operands.
 It returns 1 if both the bits in that
position are 1 or else returns 0.
 This comparison is performed on each
of the individual bits and the results of
these comparisons form an 8-bit binary
number.
 This number is automatically converted
to integer, which is displayed as the
output.
 Same rules with OR and XOR
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 26
Increment and Decrement Operators
 Two of the most common calculations performed in programming are
increasing and decreasing the value of the variable by 1.
 In C#, the increment operator (++) is used to increase the value by 1 while
the decrement operator (--) is used to decrease the value by 1.
 If the operator is placed before the operand, the expression is called pre-
increment or pre-decrement.
 If the operator is placed after the operand, the expression is called post-
increment or post-decrement.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 27


Increment and Decrement Operators
 The following table depicts the use of increment and decrement operators
assuming the value of the variable valueOne is 5:

Expression Type Result


valueTwo = ++ValueOne; Pre-Increment valueTwo = 6
valueTwo = valueOne++; Post-Increment valueTwo = 5

valueTwo = --valueOne; Pre-Decrement valueTwo = 4


valueTwo = valueOne--; Post-Decrement valueTwo = 5

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 28


Assignment Operators
 Assignment operators are used to assign the value of the right side
operand to the operand on the left side using the equal to operator (=).
 The assignment operators are divided into two categories in C#. These are
as follows:
 Simple assignment operators: The simple assignment operator is =, which is used to
assign a value or result of an expression to a variable.
 Compound assignment operators: The compound assignment operators are formed
by combining the simple assignment operator with the arithmetic operators.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 29


Assignment Operators
 The following table shows the use of assignment operators assuming the
value of the variable valueOne is 10:

Expression Description Result


valueOne += 5; valueOne = valueOne + 5 valueOne = 15
valueOne -= 5; valueOne = valueOne - 5 valueOne = 5
valueOne *= 5; valueOne = valueOne * 5 valueOne = 50
valueOne %= 5; valueOne = valueOne % 5 valueOne = 0

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 30


Operators Precedence - 2
Precedence (where Operator Description Associativity
1 is the highest)
1 () Parentheses Child to Parent
2 ++ or -- Increment or Decrement Right to Left

3 *, /, % Multiplication, Division, Modulus Left to Right


4 +, - Addition, Subtraction Left to Right

5 <, <=, >, >= Less than, Less than or equal to, Greater than, Left to Right
Greater than or equal to
6 ==, != Equal to, Not Equal to Left to Right

7 && Conditional AND Left to Right

8 || Conditional OR Left to Right


9 =, +=, -=, *=, /=, Assignment Operators Right to Left
%=

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 31


Section 2

Conditional statement

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 32


Conditional Statements
 Is a programming construct supported by C# that controls the flow of a program.
 Executes a particular block of statements based on a boolean condition, which is an
expression returning true or false.
 Is referred to as a decision-making construct.
 Allow you to take logical decisions about executing different blocks of a program to
achieve the required logical output.
 C# supports the following decision-making constructs:
 if…else
 if…else…if
 switch…case

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 33


The if statement
 The if statement allows you to execute a block of statements after evaluating
the specified logical condition.
 The if statement starts with the if keyword and is followed by the condition.
 If the condition evaluates to true, the block of statements following the if
statement is executed.
 If the condition evaluates to false, the block of statements following the if
statement is ignored and the statement after the block is executed.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 34


The if statement
 The following is the syntax for the if statement:

if (condition)
{
// one or more statements;
}

 where
 condition: Is the boolean expression.
 statements: Are set of executable instructions executed when the boolean expression
returns true.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 35


The if statement

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 36


The if…else Construct
 In some situations, it is required to
define an action for a false condition by
using an if...else construct.
 The if...else construct starts with the if
block followed by an else block and the
else block starts with the else keyword
followed by a block of statements.
 If the condition specified in the if
statement evaluates to false, the
statements in the else block are
executed.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 37


The if…else…if Construct
 The if…else…if construct allows you to check multiple conditions to
execute a different block of code for each condition.
 It is also referred to as if-else–if ladder.
 The construct starts with the if statement followed by multiple else if
statements followed by an optional else block.
 The conditions specified in the if…else…if construct are evaluated
sequentially.
 The execution starts from the if statement. If a condition evaluates to false,
the condition specified in the following else…if statement is evaluated.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 38


The if…else…if Construct

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 39


Nested if Construct

The nested if construct starts


with the if statement, which is
The nested if construct
called the outer if statement,
consists of multiple if
and contains multiple if
statements.
statements, which are called
inner if statements.

In the nested if construct, the


outer if condition controls the
In addition, each inner if
execution of the inner if
statement is executed only if
statements. The compiler
the condition in its previous
executes the inner if
inner if statement is true.
statements only if the condition
in the outer if statement is true.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 40


Nested if Construct

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 41


switch…case Construct
 A program is difficult to comprehend when there are too many if statements
representing multiple selection constructs.
 To avoid using multiple if statements, in certain cases, the switch…case
approach can be used as an alternative.
 The switch…case statement is used when a variable needs to be
compared against different values.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 42


switch…case Construct
 The switch…case construct has the following components:
 switch: The switch keyword is followed by an integer expression enclosed in
parentheses. The expression must be of type int, char, byte, or short. The switch
statement executes the case corresponding to the value of the expression.
 case: The case keyword is followed by a unique integer constant and a colon. Thus,
the case statement cannot contain a variable. The block following a particular case
statement is executed when the switch expression and the case value match. Each
case block must end with the break keyword that passes the control out of the switch
construct.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 43


switch…case Construct
 The switch…case construct has the following components:
 default: If no case value matches the switch expression value, the program control is
transferred to the default block. This is the equivalent of the else block of the
if...else...if construct.
 break: The break statement is optional and is used inside the switch…case statement
to terminate the execution of the statement sequence. The control is transferred to the
statement after the end of switch. If there is no break, execution flows sequentially into
the next case statement. Sometimes, multiple case statements can be present without
break statements between them.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 44


No-Fall-Through Rule
Thus, the list of statements inside a
In C#, the flow of execution from case block generally ends with a
one case statement is not allowed to break or a goto statement, which
continue to the next case statement causes the control of the program to
and is referred to as the ‘no-fall- exit the switch…case construct and
through’ rule of C#. go to the statement following the
construct.

The last case block (or the default C# introduced the no fall-through
block) also needs to have a rule to allow the compiler to
statement like break or goto to rearrange the order of the case
explicitly take the control outside blocks for performance
the switch…case construct. optimization.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 45


No-Fall-Through Rule
 Code will execute statements inside and bellow right case, or until break
 A multiple case statement can be made to execute the same code
sequence.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 46


if – else VS switch

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 47


ifif ––else
elseVSVS switch
switch

1. Check the Testing Expression

An if-else statement can test expressions based on ranges of values or conditions,

whereas

A switch statement tests expressions based only on a single integer, enumerated value, or
String object.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 48


if
if ––else
elseVSVS switch
switch

2. Switch better for Multi way branching

When compiler compiles a switch statement, it will inspect each of the case constants and
create a “jump table” that it will use for selecting the path of execution depending on the value
of the expression. Therefore, if we need to select among a large group of values, a switch
statement will run much faster than the equivalent logic coded using a sequence of if-else.
The compiler can do this because it knows that the case constants are all the same type
and simply must be compared for equality with the switch expression, while in case of if
expressions, the compiler has no such knowledge.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 49


if – else VS switch

3. if-else better for boolean values

If-else conditional branches are great for variable conditions that result into a boolean,

whereas

switch statements are great for fixed data values.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 50


if – else VS switch

4. Speed

A switch statement might prove to be faster than ifs provided number of cases are good. If
there are only few cases, it might not effect the speed in any case. Prefer switch if the number
of cases are more than 5 otherwise, you may use if-else too.
If a switch contains more than five items, it’s implemented using a lookup table or a hash list.
This means that all items get the same access time, compared to a list of ifs where the last
item takes much more time to reach as it has to evaluate every previous condition first.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 51


if – else VS switch

5. Clarity in readability

A switch looks much cleaner when you have to combine cases. Ifs are quite vulnerable to
errors too. Missing an else statement can land you up in havoc. Adding/removing labels is
also easier with a switch and makes your code significantly easier to change and maintain.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 52


if – else VS switch

6. Conversion

All switch statements can be convert to if-else

BUT

Not all if-else statements can be convert to switch

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 53


Give your decision
 Print day of week
 Print month of year
 Branching for check positive or negative number
 Student evaluation by final GPA

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 54


Section 3

Method

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 55


Code block
 Block of statements in sequential to implement specify logical
 C# use bracket to specify a code block {}
 Nested code block
 Sample:
 Read name of user then say Hello
 Get max value of 2 numbers
 Find the greatest common divisor

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 56


Method
 A method is a code block that contains a series of statements.
 A program causes the statements to be executed by calling the method and
specifying any required method arguments.
 In C#, every executed instruction is performed in the context of a method.
 Why use methods? To reuse code
 define the code once
 use it many times

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 57


Method signatures
 A signature of method to let compiler (and developer) understand main idea
of the method
 A signature contains:
 Access level: optional
 Method name
 Method parameters
 Return type

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 58


Method name
 Following basic rules of name in C#
 Is verb or verb-phrase, describe main idea of the body
 Use Pascal Casing - First character of all words are Upper Case and other
characters are lower case

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 59


Method parameters
 The method definition specifies the names and types of any parameters
that are required.
 When calling code calls the method, it provides concrete values called
arguments for each parameter.
 One method has zero, one or many parameters
 All parameters are placed in parentheses()

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 60


Method Overloading
 With method overloading, multiple methods can have the same name with
different parameters
 Overloading types:
 Difference number of parameters
 Difference data type of parameters
 Difference order of parameters with difference data type
 CANNOT: difference return values

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 61


Return values
 Methods can return a value to the caller.
 A statement with the return keyword followed by a variable, constant, or
expression that matches the return type will return that value to the method
caller.
 If method does not return any value, use void in method signature.
 Methods with a non-void return type are required to use the return keyword
to return a value.
 The return keyword also stops the execution of the method.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 62


Return values
 To return more than a single value, from C# 7.0, you can do this easily by
using tuple types and tuple literals.
 The tuple type defines the data types of the tuple's elements.
 Tuple literals provide the actual values of the returned tuple.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 63


Method body rules
 Method implement and only implement specify logical
 Method should use and only use defined parameters
 Parameter is not used => remove from signature
 Use other value => consider move it into parameter
 Method body should not exceed ONE screen of code
 DONOT use goto in method body
 Avoid to return anywhere in method

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 64


Method invocation
 Step 1: prepare arguments
 Step 2: Invoke method
 Step 3: (optional) receive return value

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 65


Method parameters vs. arguments
 Parameter: specifies the names and data type in method signature
 Argument: concrete value that used to pass to method.
 The arguments must be compatible with the parameter type but the
argument name (if any) used in the calling code doesn't have to be the
same as the parameter named defined in the method.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 66


Passing types

Passing by value Passing by reference

• Its copy is passed instead of the instance itself. • a reference to the object is passed

• changes to the argument have no effect on the • the change is reflected in the argument in the
original instance in the calling method. calling method

• To pass a value-type instance by reference,


use the ref keyword

 09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 67


Variable scope
 Class Level Scope
 Declaring the variables in a class but outside any method can be directly accessed
anywhere in the class.
 These variables are also termed as the fields or class members.
 Class level scoped variable can be accessed by the non-static methods of the class in
which it is declared.
 Access modifier of class level variables doesn’t affect their scope within a class.
 Member variables can also be accessed outside the class by using the access
modifiers.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 68


Variable scope
 Method Level Scope
 Variables that are declared inside a method have method level scope. These are not
accessible outside the method.
 However, these variables can be accessed by the nested code blocks inside a
method.
 These variables are termed as the local variables.
 There will be a compile-time error if these variables are declared twice with the same
name in the same scope.
 These variables don’t exist after method’s execution is over.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 69


Variable scope
 Block Level Scope
 These variables are generally declared inside the for, while statement etc.
 These variables are also termed as the loop variables or statements variable as they
have limited their scope up to the body of the statement in which it declared.
 Generally, a loop inside a method has three level of nested code blocks(i.e. class
level, method level, loop level).
 The variable which is declared outside the loop is also accessible within the nested
loops. It means a class level variable will be accessible to the methods and all loops.
Method level variable will be accessible to loop and method inside that method.
 A variable which is declared inside a loop body will not be visible to the outside of loop
body.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 70


THANK YOU!

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use

You might also like