0% found this document useful (0 votes)
16 views19 pages

Conditions: Ali Shakir Alahmed

Uploaded by

Laween Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views19 pages

Conditions: Ali Shakir Alahmed

Uploaded by

Laween Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Duhok Polytechnic University

Shekhan Technical Institute


Information Technology Department

Conditions

Ali Shakir Alahmed

2022–2023
C# Conditions

• C# provides many decision-making statements that help the flow of the


C#
program based on certain logical conditions.
• C# supports the usual logical conditions from mathematics:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b
• These conditions can be used to perform different actions for
different decisions.
C#
Conditions
C# has the following conditional statements:
• If Statement
• Else Statement
• Else If Statement
• Nested If Statement
• Switch Case Statement
C# if
statement
• The if statement is used to specify a block of code to be executed, if
a
specified condition is true
Syntax:
if (condition) {
// block of code to be executed if the condition is
True
}
• In the example below, we test two values to find out if 20 is greater than 18.
If the condition is True, print some text:

if (20 > 18) {


Console.WriteLine("20 is greater than 18");
}

Note: if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
C# if
statement
Example: if (single line statement)
int x =
10; int y
= 5; if
(x > y)
Console.W
riteLine(
"x is
greater
Example: if (multiple lines statements)
than y");
Console.Wr
int x = 10;
iteLine("S
int yline
ingle = 5;
if (x > y)
statement
{
example");
Console.Wr
iteLine("x
is greater
than y");
C# else
statement
• The else statements is used to specify a block of code to be executed, if
the
same condition is false
Syntax:
if (condition) {
// block of code to be executed if the condition is
True
} else {
// block of code to be executed if the condition is
False
}
C# else
statement
Example:

int csharp =
70; if (csharp
>= 50)
{
Console.WriteL
ine("Congratul
ations, you
passed");
}
else
{
Console.WriteL
ine("Sorry,
you failed");
• using System;
• namespace ConsoleApp2
•{
• class Program
• {
• static void Main(string[] args)
• {
• Console.WriteLine("Enter the
Number");
• int x =
int.Parse((Console.ReadLine());
• if( x>20)
• {
• Console.WriteLine("X is greater
than 20");
• }
• else
• {
• Console.WriteLine("X is less than
20");
static void Main(string[] args)
{
Console.WriteLine("Enter Number 1");
int num1= int.Parse((Console.ReadLine());

Console.WriteLine("Enter Number 2");


int num2 = int.Parse((Console.ReadLine());

float re = 0;

if ( num2==0)
{

Console.WriteLine("Error / by Zero");
}
else
{
re = num1 / num2;

}
Console.WriteLine(re);
}}}
C# else if
statement
• The else if statements is used to specify a new condition to test, if the
first
condition is false
Syntax:
if (condition1) {
// block of code to execute if condition1 is
be d True
} else if {
(condition2)
// block of code to execute if the condition1 false
be d is
and condition2 is
True
} else {
// block of code to execute if the condition1 false
be d is
and condition2 is
C# else if statement
Example:

int i = 50;
int j = 70;
if (i > j)
{
Console.WriteLine("i is greater than j");
}
else if (i < j)
{
Console.WriteLine("j is greater than i");
}
else
{
Console.WriteLine("i is equal to j");
}
C# Ternary operator
Syntax:
variable = (condition) ? expressionTrue : expressionFalse;

Solving same previous example using Ternary operator

int csharp = 70;


string result = (csharp >= 50) ?
"Congratulations, you passed" : "Sorry, you
failed"; Console.WriteLine(result);
C# Nested if
statement
• if and if-else statements can be nested, i.e. used inside another if or else
statement
• Every else corresponds to its closest preceding if

if (expression)
{
if (expression)
{
statement;
}
else
{
statement;
}
}
else
statement;
C# Nested if
statement
Example:

int first = 50;


int second = 70;
if (first == second)
{
Console.WriteLine("The two numbers are equal");
}
else
{
if (first > second)
{
Console.WriteLine("The first number is
greater");
}
else
{
Console.WriteLine("The second number is
greater");
}
C# Switch statement
• Use the switch statement to select one of many code blocks to be
executed.

Syntax:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
break;
}
• static void Main(string[]
args)
• {
• Console.WriteLine("Enter the Number");
• int num = int.Parse(Console.ReadLine());
• switch (num)
• {
• case 12:
• Console.WriteLine("12");
• break;
• case 15:
• Console.WriteLine("15");
• Console.WriteLine("No");
• break;
• default:
• Console.WriteLine("Error");
• break;
• }
• }}
C# Switch statement
Example:
int day = 6;
switch
(day)
{ case 1:
Console.WriteLine("Saturday
"); break;
case 2:
Console.WriteLine("Sunday
"); break;
case 3:
Console.WriteLine("Monday
"); break;
case 4:
Console.WriteLine("Tuesday
"); break;
case 5:
Console.WriteLine("Wednesday
"); break;
case 6:
Console.WriteLine("Thursday
"); break;
case 7:
Console.WriteLine("Friday
"); break;
C# Switch statement
break Keyword:

• When C# reaches a break keyword, it breaks out of the switch block.


• This will stop the execution of more code and case testing inside the block.
• When a match is found, and the job is done, it's time for a break. There is
no
need for more testing.

You might also like