Basic Operators Absolutely Necessary For Beginners In C#

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.

  1. If Statement/ else if Statement/ sample program.
  2. Switch Statement/ sample program.
  3. For loop/ for each/ sample program.
  4. While/ Do while/ sample program.
  5. 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 

  1. namespace First_Project  
  2. {  
  3.     class Program  
  4.     {  
  5.   
  6.   
  7.         static void Main()  
  8.         {  
  9.             string Name = "Prem";  
  10.             Console.WriteLine("Enter your name");  
  11.   
  12.             if (Name == "Prem")  
  13.             {  
  14.                 Console.WriteLine("DotNetDeveloper");  
  15.             }  
  16.             else  
  17.             {  
  18.                 Console.WriteLine("sharepoint developer");  
  19.             }  
  20.   
  21.   
  22.         }  
  23.     }  
  24. }   

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 

  1. namespace Switch_Statement  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("Please enter a number");  
  8.             int MyNumber = int.Parse(Console.ReadLine());  
  9.             if (MyNumber == 10)  
  10.             {  
  11.                 Console.WriteLine("Your Number is 10");  
  12.             }  
  13.             else if(MyNumber==20)  
  14.             {  
  15.                 Console.WriteLine("Your Number is 20");  
  16.             }  
  17.             else if (MyNumber == 30)  
  18.             {  
  19.                 Console.WriteLine("Your Number is 30");  
  20.             }  
  21.             else  
  22.             {  
  23.                 Console.WriteLine("Your number is not 10,20 & 30");  
  24.             }  
  25.         }  
  26.     }  
  27. }   

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 

  1. static void Main(string[] args)  
  2.         {  
  3.             int[] Numbers = new int[3];  
  4.   
  5.             Numbers[0] = 101;  
  6.             Numbers[1] = 102;  
  7.             Numbers[2] = 103;  
  8.   
  9.             for (int j = 0; j < Numbers.Length; j++)   
  10.             {  
  11.                 Console.WriteLine(Numbers[j]);  
  12.             }  
  13.   
  14.              
  15.         }  

For each loop

It is basically used for the collection of the numbers, data types and the strings.

Code 

  1. static void Main(string[] args)  
  2.         {  
  3.             int[] Numbers = new int[3];  
  4.   
  5.             Numbers[0] = 101;  
  6.             Numbers[1] = 102;  
  7.             Numbers[2] = 103;  
  8.   
  9.             foreach(int k in Numbers)  
  10.             {  
  11.                 Console.WriteLine(k);  
  12.             }  
  13.   
  14.              
  15.         }   

Output



While Loop

While loop checks the condition first. If the condition is true, the statements within the loop are executed.

Code 

  1. static void Main(string[] args)  
  2.         {  
  3.             Console.WriteLine("Please enter Your Target?");  
  4.             int Your Target = int.Parse(Console.ReadLine());  
  5.   
  6.             int start = 0;  
  7.             while (start <= Your Target)  
  8.             {  
  9.                 Console.Write(start + " ");  
  10.                 start = start + 2;  
  11.             }  
  12.   
  13.              
  14.         }  

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 

  1. static void Main(string[] args)  
  2.         {  
  3.              string YourChoice="";  
  4.             do{  
  5.             Console.WriteLine("Please enter Your Target?");  
  6.             int YourTarget = int.Parse(Console.ReadLine());  
  7.   
  8.             int start = 0;  
  9.             while (start <= YourTarget)  
  10.             {  
  11.                 Console.Write(start + " ");  
  12.                 start = start + 2;  
  13.             }  
  14.   
  15.   
  16.             do  
  17.             {  
  18.                 Console.WriteLine("Do you want to continue - Yes or No?");  
  19.                 YourChoice = Console.ReadLine().ToUpper();  
  20.                 if (YourChoice != "YES" && YourChoice != "NO")  
  21.                 {  
  22.                     Console.WriteLine("Invalid choice, please enter Yes or No");  
  23.                 }  
  24.             } while (YourChoice != "YES" && YourChoice != "NO");  
  25.            }while(YourChoice=="YES");  
  26.         }   

Output

User enter target as 10.


If the user entered  Yes:


Thanks for reading my blog.

Ebook Download
View all
Learn
View all