Conditional Statements.
Conditional Statements.
Implementi ng
Control-Flow Logic in C#
SoftUni Team
Technical Trainers
Software University
http://softuni.bg
Table of Contents
1. Comparison and Logical Operators
2. The if Statement
3. The if-else Statement
4. Nested if Statements
5. The switch-case Statement
2
Comparison and
Logical Operators
Comparison Operators
Operator Notation in C# Applicable for
Equals == strings / numbers /
Not Equals != dates / most objects
Greater Than >
Greater Than or Equals >= numbers / dates /
Less Than < comparable objects
Less Than or Equals <=
Example:
bool result = (5 <= 6);
Console.WriteLine(result); // True
4
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
De Morgan laws
!!A A
!(A || B) !A && !B
!(A && B) !A || !B
5
if and if-else
Implementi ng Conditional Logic
The if Statement
The simplest 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:
if (condition)
{
statements;
}
7
Condition and Statement
The condition can be:
Boolean variable
Boolean logical expression
Comparison expression
false
condition
true
statements
9
The if Statement – Example
static void Main()
{
Console.WriteLine("Enter two numbers.");
false second
condition
statement
true
first
statement
13
if-else Statement – Example
Checking a number if it is odd or even
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.");
}
14
The if-else Statement
Live Demo
Nested if Statements
Creati ng More Complex Logic
Nested if Statements
if and if-else statements can be nested
Used inside one inside another
Each else corresponds to its closest preceding if
if (expression)
{
if (expression)
some_statement;
else
another_statement;
}
else
third_statement;
17
Nested if – Good Practices
Always use { … } blocks to avoid ambiguity
Even when a single statement follows
18
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.");
}
}
19
Nested if
Statements
Live Demo
Multiple if-else-if-else-…
We may need to use another if-construction in the else block
Thus else if can be used:
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 …
21
Multiple if-else Statements
Live Demo
switch-case
Performing Several Comparisons at Once
The switch-case Statement
Selects for execution a statement from a list depending on the
value of the switch expression
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;
}
24
How switch-case Works?
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
25
The switch-case Statement
Live Demo
Using switch: Rules
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
30
Summary
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
31
Conditional Statements
? ?
sti on s ? ?
Qu e ?
?
?
http://softuni.bg/courses/csharp-basics
License
This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International" license
33
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