SlideShare a Scribd company logo
Conditional Statements
Implementing
Control-Flow Logic in C#
Svetlin Nakov
Technical Trainer
www.nakov.com
Software University
http://softuni.bg
2
1. Comparison and Logical Operators
2. The if Statement
3. The if-else Statement
4. Nested if Statements
5. The switch-case Statement
Table of Contents
Comparison and
Logical Operators
4
Operator Notation in C# Applicable for
Equals == strings / numbers /
dates / most objectsNot Equals !=
Greater Than >
numbers / dates /
comparable objects
Greater Than or Equals >=
Less Than <
Less Than or Equals <=
Comparison Operators
bool result = (5 <= 6);
Console.WriteLine(result); // True
 Example:
5
 De Morgan laws
 !!A  A
 !(A || B)  !A && !B
 !(A && B)  !A || !B
Logical Operators
Operator Notation in C# Example
Logical NOT ! !false  true
Logical AND && true && true  true
Logical OR || true || false  true
Logical Exclusive OR (XOR) ^ true ^ false  true
if and if-else
Implementing Conditional Logic
7
 The most simple conditional statement
 Enables you to test for a condition
 Branch to different blocks in the code depending on the result
 The simplest form of the if statement:
The if Statement
if (condition)
{
statements;
}
8
 The condition can be:
 Boolean variable
 Boolean logical expression
 Comparison expression
 The condition cannot be integer variable (like in C / C++)
 The statement can be:
 Single statement ending with a semicolon
 Block enclosed in curly braces
Condition and Statement
9
 The condition is evaluated
 If it is true, the statement is executed
 If it is false, the statement is skipped
How It Works?
true
condition
statements
false
10
The if Statement – Example
static void Main()
{
Console.WriteLine("Enter two numbers.");
int biggerNum = int.Parse(Console.ReadLine());
int smallerNum = int.Parse(Console.ReadLine());
if (smallerNum > biggerNum)
{
biggerNum = smallerNum;
}
Console.WriteLine("The greater number is: {0}", biggerNum);
}
The if Statement
Live Demo
12
 More complex and useful conditional statement
 Executes one branch if the condition is true, and another if it is false
 The simplest form of an if-else statement:
The if-else Statement
if (expression)
{
some_statement;
}
else
{
another_statement;
}
13
 The condition is evaluated
 If it is true, the first statement is executed
 If it is false, the second statement is executed
How It Works?
condition
first
statement
true
second
statement
false
14
 Checking a number if it is odd or even
if-else Statement – Example
string s = Console.ReadLine();
int number = int.Parse(s);
if (number % 2 == 0)
{
Console.WriteLine("This number is even.");
}
else
{
Console.WriteLine("This number is odd.");
}
The if-else Statement
Live Demo
Nested if Statements
Creating More Complex Logic
17
 if and if-else statements can be nested
 Used inside one inside another
 Each else corresponds to its closest preceding if
Nested if Statements
if (expression)
{
if (expression)
some_statement;
else
another_statement;
}
else
third_statement;
18
 Always use { … } blocks to avoid ambiguity
 Even when a single statement follows
 Avoid using more than three levels of nested if statements
 Put the case you normally expect to process first
 Then write the unusual cases
 Arrange the code to make it more readable
Nested if – Good Practices
19
Nested if Statements – Example
if (first == second)
{
Console.WriteLine("These two numbers are equal.");
}
else
{
if (first > second)
{
Console.WriteLine("The first number is bigger.");
}
else
{
Console.WriteLine("The second is bigger.");
}
}
Nested if
Statements
Live Demo
21
 We may need to use another if-construction in the else block
 Thus else if can be used:
Multiple if-else-if-else-…
int ch = 'X';
if (ch == 'A' || ch == 'a')
{
Console.WriteLine("Vowel [ei]");
}
else if (ch == 'E' || ch == 'e')
{
Console.WriteLine("Vowel [i:]");
}
else if …
else …
Multiple if-else Statements
Live Demo
switch-case
Performing Several Comparisons at Once
24
 Selects for execution a statement from a list depending on the
value of the switch expression
The switch-case Statement
switch (day)
{
case 1: Console.WriteLine("Monday"); break;
case 2: Console.WriteLine("Tuesday"); break;
case 3: Console.WriteLine("Wednesday"); break;
case 4: Console.WriteLine("Thursday"); break;
case 5: Console.WriteLine("Friday"); break;
case 6: Console.WriteLine("Saturday"); break;
case 7: Console.WriteLine("Sunday"); break;
default: Console.WriteLine("Error!"); break;
}
25
1. The expression is evaluated
2. When one of the constants specified in a case label is equal to
the expression
 The statement that corresponds to that case is executed
3. If no case is equal to the expression
 If there is default case, it is executed
 Otherwise the control is transferred to the end point of the
switch statement
How switch-case Works?
The switch-case Statement
Live Demo
27
 Variable types like string, enum and integral types can be used
for switch expression
 The value null is permitted as a case label constant
 The keyword break exits the switch statement
 "No fall through" rule
 You are obligated to use break after each case
 Multiple labels that correspond to the same statement are
permitted
Using switch: Rules
28
 Multiple labels allow matching several cases and executing the
same statement in more than one case
Multiple Labels – Example
switch (animal)
{
case "dog":
Console.WriteLine("MAMMAL");
break;
case "crocodile":
case "tortoise":
case "snake":
Console.WriteLine("REPTILE");
break;
default:
Console.WriteLine("I don't know such animal!");
break;
}
Multiple Labels in
a switch-case
Live Demo
30
 There must be a separate case for every normal situation
 Put the normal case first
 Put the most frequently executed cases first and the least
frequently executed last
 Order cases alphabetically or numerically
 In default use case that cannot be reached under normal
circumstances
Using switch – Good Practices
31
 Comparison and logical operators are used to compose logical
conditions
 The conditional statements if and if-else
conditionally execution of blocks of code
 Constantly used in computer programming
 Conditional statements can be nested
 The switch statement easily and elegantly checks an
expression for a sequence of values
Summary
?
http://softuni.bg/courses/csharp-basics/
Conditional Statements
License
 This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-
NonCommercial-ShareAlike 4.0 International" license
33
 Attribution: this work may contain portions from
 "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license
 "C# Part I" course by Telerik Academy under CC-BY-NC-SA license
Free Trainings @ Software University
 Software University Foundation – softuni.org
 Software University – High-Quality Education,
Profession and Job for Software Developers
 softuni.bg
 Software University @ Facebook
 facebook.com/SoftwareUniversity
 Software University @ YouTube
 youtube.com/SoftwareUniversity
 Software University Forums – forum.softuni.bg

More Related Content

What's hot

C# Loops
C# LoopsC# Loops
C# Loops
Hock Leng PUAH
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
Md. Ashraful Islam
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
Sunil OS
 
C# operators
C# operatorsC# operators
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Ahmed Khateeb
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
Conditional Statement in C#
Conditional Statement in C#Conditional Statement in C#
Conditional Statement in C#
Simplilearn
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
karthikenlume
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Steve Johnson
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
Bishal Sharma
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
RTS Tech
 

What's hot (20)

C# Loops
C# LoopsC# Loops
C# Loops
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
C# operators
C# operatorsC# operators
C# operators
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Inline function
Inline functionInline function
Inline function
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Conditional Statement in C#
Conditional Statement in C#Conditional Statement in C#
Conditional Statement in C#
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 

Similar to 05. Conditional Statements

05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
maznabili
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Soran University
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Loops
LoopsLoops
Loops
Kamran
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
ShivamChaturvedi67
 
Ch05.pdf
Ch05.pdfCh05.pdf
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
SHRIRANG PINJARKAR
 
03a control structures
03a   control structures03a   control structures
03a control structures
Manzoor ALam
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrayssshhzap
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
JavvajiVenkat
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
Mehul Desai
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
University of Potsdam
 

Similar to 05. Conditional Statements (20)

05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Loops
LoopsLoops
Loops
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
M C6java5
M C6java5M C6java5
M C6java5
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
03a control structures
03a   control structures03a   control structures
03a control structures
 
Control All
Control AllControl All
Control All
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 

More from Intro C# Book

17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
Intro C# Book
 
Java Problem solving
Java Problem solving Java Problem solving
Java Problem solving
Intro C# Book
 
21. Java High Quality Programming Code
21. Java High Quality Programming Code21. Java High Quality Programming Code
21. Java High Quality Programming Code
Intro C# Book
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
Intro C# Book
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
Intro C# Book
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
Intro C# Book
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
Intro C# Book
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
Intro C# Book
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
Intro C# Book
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
Intro C# Book
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
Intro C# Book
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
Intro C# Book
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
Intro C# Book
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
Intro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
Intro C# Book
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
Intro C# Book
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
Intro C# Book
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
Intro C# Book
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
Intro C# Book
 

More from Intro C# Book (20)

17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 
Java Problem solving
Java Problem solving Java Problem solving
Java Problem solving
 
21. Java High Quality Programming Code
21. Java High Quality Programming Code21. Java High Quality Programming Code
21. Java High Quality Programming Code
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
 

Recently uploaded

The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Florence Consulting
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
eutxy
 
Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027
harveenkaur52
 
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
CIOWomenMagazine
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
ufdana
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
Javier Lasa
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
GTProductions1
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 
Bài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docxBài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docx
nhiyenphan2005
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
keoku
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 

Recently uploaded (20)

The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
 
Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027
 
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 
Bài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docxBài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docx
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 

05. Conditional Statements

  • 1. Conditional Statements Implementing Control-Flow Logic in C# Svetlin Nakov Technical Trainer www.nakov.com Software University http://softuni.bg
  • 2. 2 1. Comparison and Logical Operators 2. The if Statement 3. The if-else Statement 4. Nested if Statements 5. The switch-case Statement Table of Contents
  • 4. 4 Operator Notation in C# Applicable for Equals == strings / numbers / dates / most objectsNot Equals != Greater Than > numbers / dates / comparable objects Greater Than or Equals >= Less Than < Less Than or Equals <= Comparison Operators bool result = (5 <= 6); Console.WriteLine(result); // True  Example:
  • 5. 5  De Morgan laws  !!A  A  !(A || B)  !A && !B  !(A && B)  !A || !B Logical Operators Operator Notation in C# Example Logical NOT ! !false  true Logical AND && true && true  true Logical OR || true || false  true Logical Exclusive OR (XOR) ^ true ^ false  true
  • 6. if and if-else Implementing Conditional Logic
  • 7. 7  The most simple conditional statement  Enables you to test for a condition  Branch to different blocks in the code depending on the result  The simplest form of the if statement: The if Statement if (condition) { statements; }
  • 8. 8  The condition can be:  Boolean variable  Boolean logical expression  Comparison expression  The condition cannot be integer variable (like in C / C++)  The statement can be:  Single statement ending with a semicolon  Block enclosed in curly braces Condition and Statement
  • 9. 9  The condition is evaluated  If it is true, the statement is executed  If it is false, the statement is skipped How It Works? true condition statements false
  • 10. 10 The if Statement – Example static void Main() { Console.WriteLine("Enter two numbers."); int biggerNum = int.Parse(Console.ReadLine()); int smallerNum = int.Parse(Console.ReadLine()); if (smallerNum > biggerNum) { biggerNum = smallerNum; } Console.WriteLine("The greater number is: {0}", biggerNum); }
  • 12. 12  More complex and useful conditional statement  Executes one branch if the condition is true, and another if it is false  The simplest form of an if-else statement: The if-else Statement if (expression) { some_statement; } else { another_statement; }
  • 13. 13  The condition is evaluated  If it is true, the first statement is executed  If it is false, the second statement is executed How It Works? condition first statement true second statement false
  • 14. 14  Checking a number if it is odd or even if-else Statement – Example string s = Console.ReadLine(); int number = int.Parse(s); if (number % 2 == 0) { Console.WriteLine("This number is even."); } else { Console.WriteLine("This number is odd."); }
  • 16. Nested if Statements Creating More Complex Logic
  • 17. 17  if and if-else statements can be nested  Used inside one inside another  Each else corresponds to its closest preceding if Nested if Statements if (expression) { if (expression) some_statement; else another_statement; } else third_statement;
  • 18. 18  Always use { … } blocks to avoid ambiguity  Even when a single statement follows  Avoid using more than three levels of nested if statements  Put the case you normally expect to process first  Then write the unusual cases  Arrange the code to make it more readable Nested if – Good Practices
  • 19. 19 Nested if Statements – Example if (first == second) { Console.WriteLine("These two numbers are equal."); } else { if (first > second) { Console.WriteLine("The first number is bigger."); } else { Console.WriteLine("The second is bigger."); } }
  • 21. 21  We may need to use another if-construction in the else block  Thus else if can be used: Multiple if-else-if-else-… int ch = 'X'; if (ch == 'A' || ch == 'a') { Console.WriteLine("Vowel [ei]"); } else if (ch == 'E' || ch == 'e') { Console.WriteLine("Vowel [i:]"); } else if … else …
  • 24. 24  Selects for execution a statement from a list depending on the value of the switch expression The switch-case Statement switch (day) { case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; case 4: Console.WriteLine("Thursday"); break; case 5: Console.WriteLine("Friday"); break; case 6: Console.WriteLine("Saturday"); break; case 7: Console.WriteLine("Sunday"); break; default: Console.WriteLine("Error!"); break; }
  • 25. 25 1. The expression is evaluated 2. When one of the constants specified in a case label is equal to the expression  The statement that corresponds to that case is executed 3. If no case is equal to the expression  If there is default case, it is executed  Otherwise the control is transferred to the end point of the switch statement How switch-case Works?
  • 27. 27  Variable types like string, enum and integral types can be used for switch expression  The value null is permitted as a case label constant  The keyword break exits the switch statement  "No fall through" rule  You are obligated to use break after each case  Multiple labels that correspond to the same statement are permitted Using switch: Rules
  • 28. 28  Multiple labels allow matching several cases and executing the same statement in more than one case Multiple Labels – Example switch (animal) { case "dog": Console.WriteLine("MAMMAL"); break; case "crocodile": case "tortoise": case "snake": Console.WriteLine("REPTILE"); break; default: Console.WriteLine("I don't know such animal!"); break; }
  • 29. Multiple Labels in a switch-case Live Demo
  • 30. 30  There must be a separate case for every normal situation  Put the normal case first  Put the most frequently executed cases first and the least frequently executed last  Order cases alphabetically or numerically  In default use case that cannot be reached under normal circumstances Using switch – Good Practices
  • 31. 31  Comparison and logical operators are used to compose logical conditions  The conditional statements if and if-else conditionally execution of blocks of code  Constantly used in computer programming  Conditional statements can be nested  The switch statement easily and elegantly checks an expression for a sequence of values Summary
  • 33. License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license 33  Attribution: this work may contain portions from  "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license  "C# Part I" course by Telerik Academy under CC-BY-NC-SA license
  • 34. Free Trainings @ Software University  Software University Foundation – softuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg  Software University @ Facebook  facebook.com/SoftwareUniversity  Software University @ YouTube  youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bg

Editor's Notes

  1. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  2. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  3. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  4. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  5. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  6. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  7. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  8. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*