0% found this document useful (0 votes)
2 views

Saif Report if

The document provides an overview of the if and if-else statements in C# programming, detailing their syntax and usage. It includes examples demonstrating how to implement these statements, including user input handling. The content is prepared by Saif Masoud Yaseen under the supervision of Dr. Emad Majed at the Duhok Polytechnic University for the academic year 2022-2023.

Uploaded by

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

Saif Report if

The document provides an overview of the if and if-else statements in C# programming, detailing their syntax and usage. It includes examples demonstrating how to implement these statements, including user input handling. The content is prepared by Saif Masoud Yaseen under the supervision of Dr. Emad Majed at the Duhok Polytechnic University for the academic year 2022-2023.

Uploaded by

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

Kurdistan Reginal Government

Iraq Duhok polytechnic


univversity Tecnhnical
institute of Akre Dept :It

if and else

Supervisor: Dr.emad 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. 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

C# If-else Example: with input from user


In this example, we are getting input from the user using Console.ReadLine() 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. 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

You might also like