Saif Report if
Saif Report if
if and else
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. Console.WriteLine("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. Console.WriteLine("It is even number");
10. }
11. else
12. {
13. Console.WriteLine("It is odd number");
14. }
15.
16. }
17. }
Output:
It is odd number
1. using System;
2. public class IfExample
3. {
4. public static void Main(string[] args)
5. {
6. Console.WriteLine("Enter a number:");
7. int num = Convert.ToInt32(Console.ReadLine());
8.
9. if (num % 2 == 0)
10. {
11. Console.WriteLine("It is even number");
12. }
13. else
14. {
15. Console.WriteLine("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