Skip to main content

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.
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, Sharing is Caring :)

#RathrolaPremKumar (MVP)

Comments

Popular posts from this blog

ERROR: Cannot resolve the collation conflict between SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS_KS_WS in the equal to operation

ERROR: Cannot resolve the collation conflict between SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS_KS_WS in the equal to operation
dont get panic if u get the following error while in your joinings, there is a simple way to solve this.
it is because of collation settings on two columns we are doing joinings. it is because of collation settings on two columns we are doing joinings
First step is to figure out what are two colaltion that cause for conflicts. Let us assume that collation has not been set at the column level and instead at db level, then we have to execute two simple statements as below.

Statements: Select DATABASEPROPERTYYEX('DB1',N'Collation') Select DATABASEPROPERTYYEX('DB2',N'Collation')
One more thing, make a note that if you are on sharepoint you will get error as below
Latin_General_CI_AS_KS_WS. if you are on most any other database and use the dafault settings you may get this SQL_Latin_General_CP1_CI_AS.
Now we have to do some…

How To Create The Data Adapter Object Using ADO.NET by #RathrolaPremKumar

By reading this article, you will learn key points in ADO.NET, as given below. What is Data Adapter?Primary Data Adapters for the databases.Data Adapter properties.Methods used by Data Adapter.How to create Data Adapter with an example? Let’s start our demo with a Data Adapter introduction. Actually, we use Data Adapter object to establish the connection to the data source and manage the movement of date to and from the database. What is Data Adapter? A data adapter aobject serves as a bridge between a data set object and Data Source such as a database to retrieve and save the data. Data adapter contains a set of database commands and a database connection, which we use to fill a dataset object and update the Data Source. Primary data adapters for databases? .NET makes two primary data adapters available for use with the databases. Other data adapters can also be integrated with Visual Studio .NET. Primary Data Adapters are mentioned below. OleDbData Adapter, which is suitable for use wi…

Work 9 to 6 for Salary and 6 to 9 for Career

How much time one must spend in office, the subject line of the Post is something I have heard from a client several years back and believe the best of all choices. She was in executive rank and was highly successful in both office & home fronts.................................................................... She said following “Souvik, you get a salary to do 40 hours of work per week. You must come on time at 9 am and work efficiently to plan & finish it by 6 pm. Whatever you are doing during this time, you must try to do it in the best possible way. You should plan so that you do not sign up for more work than what you can deliver. Also manage your priorities & timing to avoid work fluctuations as much as practically possible.”  I nodded head as what she said is something everybody knows. She continued. “However, 9-6 work will only help you “earn” your salary. It will not be good enough to offer a sustained growth, especially in later part of your career. If you really w…