In this blog, you will learn about the conditions in C# such as If/else if/ for loop/ for each/ while/ do while.
By reading this blog, you will learn the points given below.
- If Statement/ else if Statement/ sample program.
- Switch Statement/ sample program.
- For loop/ for each/ sample program.
- While/ Do while/ sample program.
- If statement
If statement is nothing but a statement that checks the condition whether the number the user has given is true or false. Subsequently, if the statement is true then it will execute the function or if the condition is false then it goes into else block.
Else Statement
Else checks the condition whether or not the use haas given a false number, then it goes to else block and then executes the function.
Let's see a sample program.
Code
- namespace First_Project
- {
- class Program
- {
-
-
- static void Main()
- {
- string Name = "Prem";
- Console.WriteLine("Enter your name");
-
- if (Name == "Prem")
- {
- Console.WriteLine("DotNetDeveloper");
- }
- else
- {
- Console.WriteLine("sharepoint developer");
- }
-
-
- }
- }
- }
Output
Entered name is Prem. We will get .NET Developer as an output, as shown below.
Output
If we will enter another name, then it will go to else block.
Switch Statement
Multiple if statements can be replaced with the Switch statement.
Code
- namespace Switch_Statement
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Please enter a number");
- int MyNumber = int.Parse(Console.ReadLine());
- if (MyNumber == 10)
- {
- Console.WriteLine("Your Number is 10");
- }
- else if(MyNumber==20)
- {
- Console.WriteLine("Your Number is 20");
- }
- else if (MyNumber == 30)
- {
- Console.WriteLine("Your Number is 30");
- }
- else
- {
- Console.WriteLine("Your number is not 10,20 & 30");
- }
- }
- }
- }
Output
Output
If the entered number is 40, we will get the output given below.
For loop
For loop is very similar to while loop. In a while loop, we do the initialization at one place, check the condition at another place and modify the variable at the another place, whereas for loop has all of these in one place.
Code
- static void Main(string[] args)
- {
- int[] Numbers = new int[3];
-
- Numbers[0] = 101;
- Numbers[1] = 102;
- Numbers[2] = 103;
-
- for (int j = 0; j < Numbers.Length; j++)
- {
- Console.WriteLine(Numbers[j]);
- }
-
-
- }
For each loop
It is basically used for the collection of the numbers, data types and the strings.
Code
- static void Main(string[] args)
- {
- int[] Numbers = new int[3];
-
- Numbers[0] = 101;
- Numbers[1] = 102;
- Numbers[2] = 103;
-
- foreach(int k in Numbers)
- {
- Console.WriteLine(k);
- }
-
-
- }
Output
While Loop
While loop checks the condition first. If the condition is true, the statements within the loop are executed.
Code
- static void Main(string[] args)
- {
- Console.WriteLine("Please enter Your Target?");
- int Your Target = int.Parse(Console.ReadLine());
-
- int start = 0;
- while (start <= Your Target)
- {
- Console.Write(start + " ");
- start = start + 2;
- }
-
-
- }
Output
Do While
A Do loop checks its condition at the end of the loop. This means that Do loop is guaranteed to execute at least one time. Do loops are used to present a menu to the user.
Code
- static void Main(string[] args)
- {
- string YourChoice="";
- do{
- Console.WriteLine("Please enter Your Target?");
- int YourTarget = int.Parse(Console.ReadLine());
-
- int start = 0;
- while (start <= YourTarget)
- {
- Console.Write(start + " ");
- start = start + 2;
- }
-
-
- do
- {
- Console.WriteLine("Do you want to continue - Yes or No?");
- YourChoice = Console.ReadLine().ToUpper();
- if (YourChoice != "YES" && YourChoice != "NO")
- {
- Console.WriteLine("Invalid choice, please enter Yes or No");
- }
- } while (YourChoice != "YES" && YourChoice != "NO");
- }while(YourChoice=="YES");
- }
Output
User enter target as 10.
If the user entered Yes:
Thanks for reading my blog.