C#
C# Variables & Operators with Example
C# Variables A variable is a name given to a storage area that is used to store values of various data...
Flow control and conditional statements are available in any programming language to alter the flow of a program.
For example, if someone wants to execute only a particular set of statements based on some certain logic, then Flow control, and conditional statements will be useful.
You will get a better understanding as we go through the various statements which are available in C#.
Please note that all the code below is made to the Program.cs file.
In this tutorial, you will learn-
The if statement is used to evaluate a boolean expression before executing a set of statements. If an expression evaluates to true, then it will run one set of statements else it will run another set of statements.
In our example below, a comparison is made for a variable called value. If the value of the variable is less than 10, then it will run one statement, or else it will run on another statement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Int32 value = 11;
if(value<10)
{
Console.WriteLine("Value is less than 10");
}
else
{
Console.WriteLine("Value is greater than 10");
}
Console.ReadKey();
}
}
}If the above code is entered properly and the program is executed successfully, the following output will be displayed.
We can clearly see that the 'if' statement was evaluated to false. Hence the message "Value is greater than 10" was sent to the console.
The switch statement is an enhancement to the 'if' statement. If you have multiple expressions that need to be evaluated in one shot, then writing multiple 'if' statements becomes an issue.
The switch statement is used to evaluate an expression and run different statements based on the result of the expression. If one condition does not evaluate to true, the switch statement will then move to the next condition and so forth.
Let's see, how this works with the below example. Here, we are again comparing the value of a variable called 'value.' We then check if the value is equal to 1, or 2, or something totally different.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Int32 value=11;
switch(value)
{
case 1: Console.WriteLine("Value is 1");
break;
case 2: Console.WriteLine("Value is 2");
break;
default: Console.WriteLine("value is different");
break;
}
}
}
}If the above code is entered properly and the program is executed successfully, the following output will be displayed. The output prints the default value "Value is different", since no condition is satisfied.
The while loop is used for iterative purposes. Suppose if you want to repeat a certain set of statements for a particular number of times, then while loop is used.
In our example below, we use the while statement to display the value of a variable 'i'. The while statement is used to display the value 3 times.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Int32 value=3,i=0;
while(i<value)
{
Console.WriteLine(i);
i=i+1;
}
Console.ReadKey();
}
}
}If the above code is entered properly and the program is executed successfully, the following output will be displayed.
Here you can see that the while statement is executed 3 times and incremented at the same time. And each time, it displayed the current value of the variable 'i'.
The 'for' loop is also used for iterative purposes. Suppose if you want to repeat a certain set of statements for a particular number of times, then forloop is used.
In our example below, we use the 'for' statement to display the value of a variable 'i'. The 'for' statement is used to display the value 3 times.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
for(Int32 i=0;i<3;i++)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
}
}If the above code is entered properly and the program is executed successfully, the following output will be displayed.
Here you can see that the 'for' statement is executed 3 times. And each time, it displayed the current value of the variable 'i'.
C# Variables A variable is a name given to a storage area that is used to store values of various data...
C# is one of the languages provided by Microsoft to work with .Net. This language encompasses a...
$20.20 $9.99 for today 4.5 (114 ratings) Key Highlights of C# Tutorial PDF 243+ pages eBook...
C# Enumeration An enumeration is used in any programming language to define a constant set of...
So far we have seen how to work with C# to create console based applications. But in a real-life...
C-SHARP (C#) is a general-purpose, multi-paradigm programming language developed by Microsoft that runs...