in , ,

C# Expressions, Statements and Blocks (With Examples)

csharp Expressions, Statements and Blocks_
csharp Expressions, Statements and Blocks_

C# Expressions, Statements and Blocks: In this tutorial, we will learn about C# expressions, C# statements, difference among expression and statements, and C# blocks.

Many times while reading a C# book or reading any article you may encounter a word named expression.. Also, you may begin considering what precisely is expression.

Expressions, statements and blocks are the building block of a C# program. We have been using them since our first “Hello World” program.


C# Expressions

An expression in C# is a combination of operands (variables, literals, strategy calls) and operators that can be assessed to a single value. To be exact, an expression should have at any rate one operand but might not have any operator.

Let’s look at the example below:

double temperature;
temperature = 42.05;

Here, 42.05 is an expression. Likewise, temperature = 42.05 is an expression as well.

int a, b, c, sum;
sum = a + b + c;

Here, a + b + c is an expression.

if (age>=18 && age<58)
	Console.WriteLine("Eligible to work");

Here, (age>=18 && age<58) is an expression that returns a boolean value. “Eligible to work” is likewise an expression.


C# Statements

A statement is a fundamental unit of execution of a program. A program comprises of different statements.

For instance:

int age = 21;
Int marks = 90;

In the above example, the two lines above are statements.

There are various types of statements in C#. In this tutorial, we’ll mainly focus on two of them:

  • Declaration Statement
  • Expression Statement

Declaration Statement

Declaration statements are used to declare and introduce variables.

For instance:

char ch;
int maxValue = 55;

Both singe ch; and int maxValue = 55; are declaration statements.


Expression Statement

An expression followed by a semicolon is called an expression statement.

For instance:

/* Assignment */
area = 3.14 * radius * radius;
/* Method call is an expression*/

System.Console.WriteLine("Hello");

Here, 3.14 * radius* radius is an expression and area= 3.14 * radius* radius; is an expression statement.

In like manner, System.Console.WriteLine(“Hello”); is both an expression and a statement.

Next to declaration and expression statement, there are:

• Selection Statements (if…else, switch)

• Iteration Statements (do, while, for, foreach)

• Jump Statements (break, continue, goto, return, yield)

• Exception Handling Statements (throw, try-catch, try-finally, try-catch-finally)

These statements will be talked about in later tutorial.

In the event that you want to learn more about statements, visit C# Statements ( C# reference)


C# Blocks

A block is a combination of zero or more statements that is enclosed inside curly brackets { }.

For instance:

Example 1: C# Blocks with statements

using System;

namespace Blocks
{
	class BlockExample
	{
		public static void Main(string[] args)
		{
			double temperature = 42.05;
			if (temperature > 32)
			{	// Start of block
				Console.WriteLine("Current temperature = {0}", temperature);
				Console.WriteLine("It's hot");
			}	// End of block
		}
	}
}

At the point when we run the program, the output will be:

Current temperature = 42.05
It's hot

Here, the two statements inside { }:

Console.WriteLine("Current temperature = {0}", temperature);

Furthermore,

Console.WriteLine("It's hot");

forms a block


Example 2: C# Blocks without statements

A block might not include any statements inside it as demonstrated in the beneath example.

using System;

namespace Blocks
{
	class BlockExample
	{
		public static void Main(string[] args)
		{
			double temperature = 42.05;
			if (temperature > 32)
			{	// Start of block
				// No statements
			}	// End of block
		}
	}
}

Here, the curly braces { } after if(temperature > 32) contains just comments and no statements.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

csharp Basic Input and Output

C# Basic Input and Output

HTML Link Colors

HTML Links – Different Colors