SlideShare a Scribd company logo
Data Types 
Lecture 2 
Dr. Hakem Beitollahi 
Computer Engineering Department 
Soran University
Objectives of this lecture 
 In this chapter you will learn: 
 Increment and Decrement (++ and --) 
 Assignment Operators 
 Basic data types in C# 
 Constant values 
 Conditional logical operators 
Data Types— 2
Main Mathematic Operations 
Operator Action 
+ Addition 
- Subtraction 
* Multiply 
/ Division 
% Reminder 
++ Increment 
-- Decrement 
Data Types— 3
Increment & decrement (I) 
 C/C++/C# includes two useful operators not found in some other 
computer languages. 
 These are the increment and decrement operators, ++ and - -  
 The operator ++ adds 1 to its operand, and − − subtracts 1. 
 i++ = i + 1 
 i-- = i - 1 
 Both the increment and decrement operators may either precede 
(prefix) or follow (postfix) the operand 
 i = i+1 can be written as i++ or ++i 
 i = i -1 can be written as i-- or --i 
 Please note: There is, however, a difference between the prefix and 
postfix forms when you use these operators in an expression 
Data Types— 4
Increment & decrement (II) 
// Increment and Decrement Operations ++/-- 
using System; 
class Comparison 
{ 
static void Main( string[] args ) 
{ 
int a = 10; 
int b = 10; 
int x, y; 
x = a++; 
y = ++b; 
Console.WriteLine("x = {0}, a ={1}.", x, a); 
Console.WriteLine("y = {0}, b ={1}.", y, b); 
}// end method Main 
} // end class Comparison 
x = 10 a = 11 
y = 11 b = 11 
Data Types— 5
Increment & decrement (III) 
// Increment and Decrement Operations ++/-- 
using System; 
class Comparison 
{ 
static void Main( string[] args ) 
{ 
int a = 10; 
int b = 10; 
Console.WriteLine(a++); 
Console.WriteLine(a); 
Console.WriteLine(++b); 
Console.WriteLine(b); 
}// end method Main 
} // end class Comparison 
10 
11 
11 
11 
Data Types— 6
Common Programming Error 1 
Attempting to use the increment or 
decrement operator on an expression 
other than a variable reference is a syntax 
error. A variable reference is a variable or 
expression that can appear on the left side 
of an assignment operation. For example, 
writing ++(x + 1) is a syntax error, 
because (x + 1) is not a variable 
reference 
Data Types — 7
Assignment Operator 
Data Types — 8
Assignment Operator (I) 
 C# provides several assignment operators for 
abbreviating assignment expressions. 
 Example: 
 c = c + 3; 
 c += 3; 
 where operator is one of the binary operators +, 
-, *, / or %, can be written in the form 
 variable operator= expression; 
Data Types — 9
Assignment Operator (II) 
Data Types — 10
Common Programming Error 1 
Placing a space character between symbols 
that compose an arithmetic assignment 
operator is a syntax error. 
A += 6; True A + = 6; False 
Data Types — 11
Basic Data Types in C# 
Data Types— 12
Basic data types in C# (I) 
 Programming languages store and 
process data in various ways depending 
on the type of the data; consequently, all 
data read, processed, or written by a 
program must have a type 
 A data type is used to 
 Identify the type of a variable when the 
variable is declared 
 Identify the type of the return value of a 
function (later) 
 Identify the type of a parameter expected by 
a function (later) 
Data Types— 13
Basic data types in C# (II) 
 There are 7 major data types in C++ 
 char e.g., ‘a’, ‘c’, ‘@’ 
 string e.g., “Zagros” 
 int e.g., 23, -12, 5, 0, 145678 
 float e.g., 54.65, -0.004, 65 
 double e.g., 54.65, -0.004, 65 
 bool only true and false 
 void no value 
Data Types— 14
Basic data types in C# (III) 
Type Range Size (bits) 
char U+0000 to U+ffff Unicode 16-bit character 
sbyte -128 to 127 Signed 8-bit integer 
byte 0 to 255 Unsigned 8-bit integer 
short -32,768 to 32,767 Signed 16-bit integer 
ushort 0 to 65535 Unsigned 16-bit integer 
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer 
uint 0 to 4,294,967,295 Unsigned 32-bit integer 
long -9,223,372,036,854,775,808 to 
9,223,372,036,854,775,807 
Signed 64-bit integer 
ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer 
float -3.402823e38 .. 3.402823e38 Signed 32 bits 
double -1.79769313486232e308 .. 
1.79769313486232e308 
Signed 64 bits 
decimal -79228162514264337593543950335 .. 
79228162514264337593543950335 
Signed 128 bits 
Data Types— 15
Basic data types in C# (IV) 
 The general form of a declaration is 
 Type variable-list 
 Examples 
 int I, j, k; 
 char ch, a; 
 float f, balance; 
 double d; 
 bool decision; 
 string str; 
 Variables name 
Correct incorrect 
Count 3count 
test23 hi!there 
high_balance high...balance 
_name Test? 
@count co@nt 
 The first character must be a letter 
 -an underscore or @ 
 The subsequent characters must be either letters, digits, or 
underscores 
Data Types— 16
Constants in C# 
Data Types— 17
Const variables (I) 
 Variables of type const may not be changed by your 
program 
 The compiler is free to place variables of this type into 
read-only memory (ROM). 
 Example: 
 const int a = 10; 
Data Types— 18
// Constant learning 
using System; 
class Constant 
{ 
static void Main( string[] args ) 
{ 
const int c = 999; 
// c = 82; Error becuase c is constant and you cannot change it 
// c = 999; Error becuase c is constant and you cannot change it 
Console.WriteLine(c); 
}// end method Main 
} // end class Constant 
Data Types— 19
Conditional Logical operators 
Data Types— 20
Conditional Logical operators (I) 
 Conditional logical operators are useful when we want to 
test multiple conditions. 
 There are 3 types of conditional logical operators and 
they work the same way as the boolean AND, OR and 
NOT operators. 
 && - Logical AND 
 All the conditions must be true for the whole 
expression to be true. 
 Example: if (a == 10 && b == 9 && d == 1) 
means the if statement is only true when a == 10 and 
b == 9 and d == 1. 
Data Types— 21
expression1 expression2 expression1 && expression2 
false false false 
false true false 
true false false 
true true true 
Data Types— 22
Conditional Logical operators (II) 
 || - Conditional logical OR 
 The truth of one condition is enough to make 
the whole expression true. 
 Example: if (a == 10 || b == 9 || d == 1) 
means the if statement is true when either 
one of a, b or d has the right value. 
 ! – Conditional logical NOT (also called 
logical negation) 
 Reverse the meaning of a condition 
 Example: if (!(points > 90)) 
means if points not bigger than 90. 
Data Types— 23
expression1 expression2 expression1 || expression2 
false false false 
false true true 
true false true 
true true true 
Expression !expression 
false true 
true false 
Data Types— 24
Data Types — 25 
// Conditional logical operator 
using System; 
class Conditional_logical 
{ 
static void Main( string[] args ) 
{ 
int a = 10; 
int b = 15; 
if ((a == 10) && (b == 15)) 
Console.WriteLine("Apple"); 
else 
Console.WriteLine("Orange"); 
if ((a == 10) || (b == 15)) 
Console.WriteLine("Apple"); 
else 
Console.WriteLine("Orange"); 
if (!(a > 20)) 
Console.WriteLine("Apple"); 
else 
Console.WriteLine("Orange"); 
}// end method Main 
} // end class 
Apple 
Apple 
Apple
Operator preferences 
Operators Preferences 
() 1 
!,~,--,++ 2 
*,/,% 3 
+, - 4 
<, <=, >=, > 5 
==, != 6 
&& 7 
|| 8 
Note: in a+++a*a, the * has preference over ++ 
Note: in ++a+a*a, the ++ has preference over * 
Data Types— 26
Common Programming Error Tip 
Although 3 < x < 7 is a mathematically correct 
condition, it does not evaluate as you might expect in 
C#. Use ( 3 < x && x < 7 ) to get the proper 
evaluation in C#. 
Using operator == for assignment and using operator = 
for equality are logic errors. 
Use your text editor to search for all occurrences of = in 
your program and check that you have the correct 
assignment operator or logical operator in each place. 
Data Types— 27

More Related Content

What's hot (19)

PDF
CP Handout#3
trupti1976
 
PDF
C++
Shyam Khant
 
PPT
Getting started with c++
K Durga Prasad
 
PPTX
Unit 2. Elements of C
Ashim Lamichhane
 
PDF
CP Handout#1
trupti1976
 
PPTX
CSharp Language Overview Part 1
Hossein Zahed
 
PPSX
Getting started with c++.pptx
Akash Baruah
 
PPT
Getting started with c++
Bussines man badhrinadh
 
PDF
2nd PUC Computer science chapter 5 review of c++
Aahwini Esware gowda
 
PDF
CP Handout#9
trupti1976
 
PDF
Intermediate code generation
Akshaya Arunan
 
PPT
Basics of c++
Huba Akhtar
 
PPTX
C Token’s
Tarun Sharma
 
PPT
Lecture 5
Soran University
 
PPTX
C++ AND CATEGORIES OF SOFTWARE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
PDF
Cs211 module 1_2015
Saad Baig
 
PPT
Methods in C#
Prasanna Kumar SM
 
PPTX
C sharp part 001
Ralph Weber
 
PPT
Unit i intro-operators
HINAPARVEENAlXC
 
CP Handout#3
trupti1976
 
Getting started with c++
K Durga Prasad
 
Unit 2. Elements of C
Ashim Lamichhane
 
CP Handout#1
trupti1976
 
CSharp Language Overview Part 1
Hossein Zahed
 
Getting started with c++.pptx
Akash Baruah
 
Getting started with c++
Bussines man badhrinadh
 
2nd PUC Computer science chapter 5 review of c++
Aahwini Esware gowda
 
CP Handout#9
trupti1976
 
Intermediate code generation
Akshaya Arunan
 
Basics of c++
Huba Akhtar
 
C Token’s
Tarun Sharma
 
Lecture 5
Soran University
 
C++ AND CATEGORIES OF SOFTWARE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
Cs211 module 1_2015
Saad Baig
 
Methods in C#
Prasanna Kumar SM
 
C sharp part 001
Ralph Weber
 
Unit i intro-operators
HINAPARVEENAlXC
 

Viewers also liked (19)

PPT
Administrative
Soran University
 
PPT
Bingham mc cutchen interview questions and answer
JulianDraxler
 
PDF
Untitled Presentation
Colleen Robertson
 
PDF
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
lkcyber
 
PPTX
інтернет і соц. сеті
Анастасия Константинова
 
PDF
Student organization president and vice president training
BelmontSELD
 
PPTX
OSAC: Personal Digital Security Presentation
lkcyber
 
PPTX
Pengukuran aliran a.(differential)
Frenki Niken
 
PPT
Who says 'everything's alright' (3)
GOKELP HR SERVICES PRIVATE LIMITED
 
PDF
Manual de Arborizacao Urbana
Aline Naue
 
PDF
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
Clean Energy Canada
 
PDF
Tagmax_ebooklet
Radoslaw Sosnowski
 
DOCX
Anti inflammatory agents
nawal al-matary
 
PDF
Math Project for Mr. Medina's Class
smit5008
 
PPTX
Dental arts davis square
Dr. Paul Dobrin
 
PDF
Cyber Espionage and Insider Threat: House of Commons Panel Discussion
lkcyber
 
PDF
Proactive Counterespionage as a Part of Business Continuity and Resiliency
lkcyber
 
Administrative
Soran University
 
Bingham mc cutchen interview questions and answer
JulianDraxler
 
Untitled Presentation
Colleen Robertson
 
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
lkcyber
 
інтернет і соц. сеті
Анастасия Константинова
 
Student organization president and vice president training
BelmontSELD
 
OSAC: Personal Digital Security Presentation
lkcyber
 
Pengukuran aliran a.(differential)
Frenki Niken
 
Who says 'everything's alright' (3)
GOKELP HR SERVICES PRIVATE LIMITED
 
Manual de Arborizacao Urbana
Aline Naue
 
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
Clean Energy Canada
 
Tagmax_ebooklet
Radoslaw Sosnowski
 
Anti inflammatory agents
nawal al-matary
 
Math Project for Mr. Medina's Class
smit5008
 
Dental arts davis square
Dr. Paul Dobrin
 
Cyber Espionage and Insider Threat: House of Commons Panel Discussion
lkcyber
 
Proactive Counterespionage as a Part of Business Continuity and Resiliency
lkcyber
 
Ad

Similar to Lecture 2 (20)

PPTX
C and C++ programming basics for Beginners.pptx
renuvprajapati
 
PPTX
the refernce of programming C notes ppt.pptx
AnkitaVerma776806
 
PDF
2 EPT 162 Lecture 2
Don Dooley
 
PDF
Week 02_Development Environment of C++.pdf
salmankhizar3
 
PPT
C program
AJAL A J
 
PPTX
B.sc CSIT 2nd semester C++ Unit2
Tekendra Nath Yogi
 
PPTX
What is c
Nitesh Saitwal
 
PPS
C programming session 02
Dushmanta Nath
 
PPTX
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
PPTX
Chapter1.pptx
WondimuBantihun1
 
PPTX
Class_IX_Operators.pptx
rinkugupta37
 
PPTX
03. operators and-expressions
Stoian Kirov
 
PPTX
additional.pptx
Yuvraj994432
 
PPT
C tutorial
Anurag Sukhija
 
PPT
C material
tarique472
 
PPTX
Programming in C
Nishant Munjal
 
PPS
basics of C and c++ by eteaching
eteaching
 
PPTX
C++ lecture 01
HNDE Labuduwa Galle
 
C and C++ programming basics for Beginners.pptx
renuvprajapati
 
the refernce of programming C notes ppt.pptx
AnkitaVerma776806
 
2 EPT 162 Lecture 2
Don Dooley
 
Week 02_Development Environment of C++.pdf
salmankhizar3
 
C program
AJAL A J
 
B.sc CSIT 2nd semester C++ Unit2
Tekendra Nath Yogi
 
What is c
Nitesh Saitwal
 
C programming session 02
Dushmanta Nath
 
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
Chapter1.pptx
WondimuBantihun1
 
Class_IX_Operators.pptx
rinkugupta37
 
03. operators and-expressions
Stoian Kirov
 
additional.pptx
Yuvraj994432
 
C tutorial
Anurag Sukhija
 
C material
tarique472
 
Programming in C
Nishant Munjal
 
basics of C and c++ by eteaching
eteaching
 
C++ lecture 01
HNDE Labuduwa Galle
 
Ad

More from Soran University (6)

PPT
Lecture 9
Soran University
 
PPT
Lecture 7
Soran University
 
PPT
Lecture 8
Soran University
 
PPT
Lecture 4
Soran University
 
PPT
Lecture 3
Soran University
 
PPT
Lecture 1
Soran University
 
Lecture 9
Soran University
 
Lecture 7
Soran University
 
Lecture 8
Soran University
 
Lecture 4
Soran University
 
Lecture 3
Soran University
 
Lecture 1
Soran University
 

Recently uploaded (20)

PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
PPTX
Ground improvement techniques-DEWATERING
DivakarSai4
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PDF
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
PDF
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
Ground improvement techniques-DEWATERING
DivakarSai4
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 

Lecture 2

  • 1. Data Types Lecture 2 Dr. Hakem Beitollahi Computer Engineering Department Soran University
  • 2. Objectives of this lecture  In this chapter you will learn:  Increment and Decrement (++ and --)  Assignment Operators  Basic data types in C#  Constant values  Conditional logical operators Data Types— 2
  • 3. Main Mathematic Operations Operator Action + Addition - Subtraction * Multiply / Division % Reminder ++ Increment -- Decrement Data Types— 3
  • 4. Increment & decrement (I)  C/C++/C# includes two useful operators not found in some other computer languages.  These are the increment and decrement operators, ++ and - -  The operator ++ adds 1 to its operand, and − − subtracts 1.  i++ = i + 1  i-- = i - 1  Both the increment and decrement operators may either precede (prefix) or follow (postfix) the operand  i = i+1 can be written as i++ or ++i  i = i -1 can be written as i-- or --i  Please note: There is, however, a difference between the prefix and postfix forms when you use these operators in an expression Data Types— 4
  • 5. Increment & decrement (II) // Increment and Decrement Operations ++/-- using System; class Comparison { static void Main( string[] args ) { int a = 10; int b = 10; int x, y; x = a++; y = ++b; Console.WriteLine("x = {0}, a ={1}.", x, a); Console.WriteLine("y = {0}, b ={1}.", y, b); }// end method Main } // end class Comparison x = 10 a = 11 y = 11 b = 11 Data Types— 5
  • 6. Increment & decrement (III) // Increment and Decrement Operations ++/-- using System; class Comparison { static void Main( string[] args ) { int a = 10; int b = 10; Console.WriteLine(a++); Console.WriteLine(a); Console.WriteLine(++b); Console.WriteLine(b); }// end method Main } // end class Comparison 10 11 11 11 Data Types— 6
  • 7. Common Programming Error 1 Attempting to use the increment or decrement operator on an expression other than a variable reference is a syntax error. A variable reference is a variable or expression that can appear on the left side of an assignment operation. For example, writing ++(x + 1) is a syntax error, because (x + 1) is not a variable reference Data Types — 7
  • 9. Assignment Operator (I)  C# provides several assignment operators for abbreviating assignment expressions.  Example:  c = c + 3;  c += 3;  where operator is one of the binary operators +, -, *, / or %, can be written in the form  variable operator= expression; Data Types — 9
  • 10. Assignment Operator (II) Data Types — 10
  • 11. Common Programming Error 1 Placing a space character between symbols that compose an arithmetic assignment operator is a syntax error. A += 6; True A + = 6; False Data Types — 11
  • 12. Basic Data Types in C# Data Types— 12
  • 13. Basic data types in C# (I)  Programming languages store and process data in various ways depending on the type of the data; consequently, all data read, processed, or written by a program must have a type  A data type is used to  Identify the type of a variable when the variable is declared  Identify the type of the return value of a function (later)  Identify the type of a parameter expected by a function (later) Data Types— 13
  • 14. Basic data types in C# (II)  There are 7 major data types in C++  char e.g., ‘a’, ‘c’, ‘@’  string e.g., “Zagros”  int e.g., 23, -12, 5, 0, 145678  float e.g., 54.65, -0.004, 65  double e.g., 54.65, -0.004, 65  bool only true and false  void no value Data Types— 14
  • 15. Basic data types in C# (III) Type Range Size (bits) char U+0000 to U+ffff Unicode 16-bit character sbyte -128 to 127 Signed 8-bit integer byte 0 to 255 Unsigned 8-bit integer short -32,768 to 32,767 Signed 16-bit integer ushort 0 to 65535 Unsigned 16-bit integer int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer uint 0 to 4,294,967,295 Unsigned 32-bit integer long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer float -3.402823e38 .. 3.402823e38 Signed 32 bits double -1.79769313486232e308 .. 1.79769313486232e308 Signed 64 bits decimal -79228162514264337593543950335 .. 79228162514264337593543950335 Signed 128 bits Data Types— 15
  • 16. Basic data types in C# (IV)  The general form of a declaration is  Type variable-list  Examples  int I, j, k;  char ch, a;  float f, balance;  double d;  bool decision;  string str;  Variables name Correct incorrect Count 3count test23 hi!there high_balance high...balance _name Test? @count co@nt  The first character must be a letter  -an underscore or @  The subsequent characters must be either letters, digits, or underscores Data Types— 16
  • 17. Constants in C# Data Types— 17
  • 18. Const variables (I)  Variables of type const may not be changed by your program  The compiler is free to place variables of this type into read-only memory (ROM).  Example:  const int a = 10; Data Types— 18
  • 19. // Constant learning using System; class Constant { static void Main( string[] args ) { const int c = 999; // c = 82; Error becuase c is constant and you cannot change it // c = 999; Error becuase c is constant and you cannot change it Console.WriteLine(c); }// end method Main } // end class Constant Data Types— 19
  • 20. Conditional Logical operators Data Types— 20
  • 21. Conditional Logical operators (I)  Conditional logical operators are useful when we want to test multiple conditions.  There are 3 types of conditional logical operators and they work the same way as the boolean AND, OR and NOT operators.  && - Logical AND  All the conditions must be true for the whole expression to be true.  Example: if (a == 10 && b == 9 && d == 1) means the if statement is only true when a == 10 and b == 9 and d == 1. Data Types— 21
  • 22. expression1 expression2 expression1 && expression2 false false false false true false true false false true true true Data Types— 22
  • 23. Conditional Logical operators (II)  || - Conditional logical OR  The truth of one condition is enough to make the whole expression true.  Example: if (a == 10 || b == 9 || d == 1) means the if statement is true when either one of a, b or d has the right value.  ! – Conditional logical NOT (also called logical negation)  Reverse the meaning of a condition  Example: if (!(points > 90)) means if points not bigger than 90. Data Types— 23
  • 24. expression1 expression2 expression1 || expression2 false false false false true true true false true true true true Expression !expression false true true false Data Types— 24
  • 25. Data Types — 25 // Conditional logical operator using System; class Conditional_logical { static void Main( string[] args ) { int a = 10; int b = 15; if ((a == 10) && (b == 15)) Console.WriteLine("Apple"); else Console.WriteLine("Orange"); if ((a == 10) || (b == 15)) Console.WriteLine("Apple"); else Console.WriteLine("Orange"); if (!(a > 20)) Console.WriteLine("Apple"); else Console.WriteLine("Orange"); }// end method Main } // end class Apple Apple Apple
  • 26. Operator preferences Operators Preferences () 1 !,~,--,++ 2 *,/,% 3 +, - 4 <, <=, >=, > 5 ==, != 6 && 7 || 8 Note: in a+++a*a, the * has preference over ++ Note: in ++a+a*a, the ++ has preference over * Data Types— 26
  • 27. Common Programming Error Tip Although 3 < x < 7 is a mathematically correct condition, it does not evaluate as you might expect in C#. Use ( 3 < x && x < 7 ) to get the proper evaluation in C#. Using operator == for assignment and using operator = for equality are logic errors. Use your text editor to search for all occurrences of = in your program and check that you have the correct assignment operator or logical operator in each place. Data Types— 27