Home  >  Article  >  Backend Development  >  C# if Statement

C# if Statement

PHPz
PHPzOriginal
2024-09-03 15:09:21789browse
  • C# is a very powerful object-oriented programming language of.Net Framework. The .Net framework is an extensive, revolutionary platform using which the following applications, such as Windows applications, Web applications, Web Services, etc., can be easily developed.
  • There are multiple languages supported by this framework, such as C#, C++, Visual Basic, etc. Therefore, the applications developed using this framework are supported by multiple platforms. For example, C# is one of the very popular languages of this framework.
  • C# is simple yet very powerful. It was created by Microsoft, and using it, we can create different types of applications based on our requirements, such as web applications, console applications, windows applications.

Let us understand the basic structure of the C# program before we jump to the if statement.

To print C# if Statement as output.

using System;   //declaring namespace
class Example1   //declaring class
{
static void Main(string[] args) {     //declaring class method
Console.WriteLine("C# IF STATEMENT");    //print
}
}

Output:

C# if Statement

This article basically focuses on the C# IF statement, so let us get on with it step by step.

The “if” Statement in C#

  • Several decision-making statements are available in C# where certain logical conditions are required to flow a program continuously. The decision-making statements included in C# are – if statement, if-else statement, switch statement, and ternary operator.
  • The “if” condition or the if-else condition takes up a boolean expression as its parameter and evaluates it. Only if the condition being evaluated is true, the block of a statement under if the statement is executed. In case the condition is false, the if block will be skipped.

C# if Statement in detail

The conditional if statement accepts a boolean expression or a condition inside brackets or as a parameter which is followed by a single line or multi-line block of code. During the runtime, when the program has been executed, the condition inside the brackets is evaluated. If this boolean expression results in true, then the code block following the if statement will be executed.

Consider the following example where the if condition contains true as an expression.

The syntax of the if the statement is –

if(a conditional statement or boolean expression)
{
// the block of code to be executed if the expression results into true
}

Let us understand this further with an example.

Consider –

using System;
class Ex2
{
static void Main(string[] args)
{
{
if(true)
Console.WriteLine("True Condition: We are inside the for loop");
if(false)
Console.WriteLine("False Condition: We will not be able to enter inside the for loop");
}
}
}
  • As explained above, if the statement contains a condition, that would result in true or false. The execution of the code associated with the if loop depends on this boolean expression. Consider the example with the problem statement given below for further clarification –
  • Problem Statement: Ravi’s age (R_age) is 15 years. Amar’s age(A_age) is 12 years. Print if Ravi is elder or younger or equal to Amar.

For Example –

using System;
class Ex3
{
static void Main(string[] args)
{
int R_age = 15, A_age = 12;
if ( R_age > A_age)
Console.WriteLine("Ravi is elder to Amar");
if (R_age < A_age)
Console.WriteLine("Ravi is younger than Amar");
if (R_age == A_age)
Console.WriteLine("Ravi is of the same age as Amar");
}
} 

Output –

C# if Statement

Note that the boolean expression in the first ‘if’ statement is given as a parameter evaluates to be true as Ravi’s age(15) is greater than Amar’s age(12). As only one if statement holds true, only the first block will be executed associated with the first if condition.

if-else Statement

The second type of conditional statement provided by C# is the if-else statement. The second part of the code, which needs to execute if the condition holds false, can be kept inside the else block. The else block cannot exist independently. This means that the else statement must follow an if-statement or else if statement. An else statement can only be used once in an if-else statement chain.

The syntax of the if-else statement is –

if(a conditional statement or boolean expression)
{
// the block of code to be executed if the expression results into true
}
else
{
// executes when “if” exp is false
}
  • As observed, the else statement does not contain any boolean expression. The block of code following the else statement is always executed whenever the condition is given in the ‘if’ brackets evaluates to be false.
  • We will consider the example of Ravi and Amar’s age as our problem statement for further clarification –

For Example –

using System;
class Ex4
{
static void Main(string[] args)
{
int R_age = 12, A_age = 15;
if ( R_age > A_age)
Console.WriteLine("Ravi is elder to Amar");
else
Console.WriteLine("Ravi and Amar are of the same age");
}
}

Output:

C# if Statement

By now, you must have noticed that the boolean expression in the first ‘if’ statement given as a parameter evaluates to be false as Ravi’s age(12) is less than Amar’s age(15). Like the if statement holds false, the second block, i.e. the code block associated with the else condition, will be executed.

else if Statement

The second type of conditional statement provided by C# is an else if statement. If the given conditions to be checked are more than one, then the else-if conditions come into the picture.

Consider –

using System;
class Ex5
{
static void Main(string[] args)
{
int R_age = 12, A_age = 15;
if ( R_age > A_age)
Console.WriteLine("Ravi is elder");
else if (R_age < A_age)
Console.WriteLine("Ravi is younger");
else
Console.WriteLine("Ravi is of the same age as Amar");
}
}

Output:

C# if Statement

Nested If

Nested if the statement is an if statement within an if statement.

For Example –

using System;
class Ex6
{
static void Main(string[] args)
{
int R_age = 12, A_age = 15;
if(R_age != A_age) //yields true as 12 is not equal to 15
{
if( R_age < A_age) //enters inside this
Console.WriteLine("Ravi is younger");
else
Console.WriteLine("Ravi is elder");
}
}
}

Output:

C# if Statement

Conclusion

The if-else or else-if statement evaluates the boolean expression and, based on the result, controls the flow of the program.

The above is the detailed content of C# if Statement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Operator Precedence in C#Next article:Operator Precedence in C#