Kurdistan Reginal Government
Iraq Duhok polytechnic
univversity Tecnhnical
institute of Akre Dept :It
if and else
Supervisor: [Link] majed
Prepared by:
Saif masoud yaseen
2022.2023
C# if-else
In C# programming, the if statement is used to test the condition. There are various types
of if statements in C#.
o if statement
o if-else statement
o nested if statement
o if-else-if ladder
C# IF Statement
The C# if statement tests the condition. It is executed if condition is true.
Syntax:
1. if(condition){
2. //code to be executed
3. }
C# If Example
1. using System;
2. public class IfExample
3. {
4. public static void Main(string[] args)
5. {
6. int num = 10;
7. if (num % 2 == 0)
8. {
9. [Link]("It is even number");
10. }
11.
12. }
13. }
Output:
It is even number
C# IF-else Statement
The C# if-else statement also tests the condition. It executes the if block if condition is
true otherwise else block is executed.
Syntax:
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
C# If-else Example
1. using System;
2. public class IfExample
3. {
4. public static void Main(string[] args)
5. {
6. int num = 11;
7. if (num % 2 == 0)
8. {
9. [Link]("It is even number");
10. }
11. else
12. {
13. [Link]("It is odd number");
14. }
15.
16. }
17. }
Output:
It is odd number
C# If-else Example: with input from user
In this example, we are getting input from the user using [Link]() method. It
returns string. For numeric value, you need to convert it into int
using Convert.ToInt32() method.
1. using System;
2. public class IfExample
3. {
4. public static void Main(string[] args)
5. {
6. [Link]("Enter a number:");
7. int num = Convert.ToInt32([Link]());
8.
9. if (num % 2 == 0)
10. {
11. [Link]("It is even number");
12. }
13. else
14. {
15. [Link]("It is odd number");
16. }
17.
18. }
19. }
Output:
Enter a number:11
It is odd number
Output:
Enter a number:12
It is even number