Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

C# if statement


In this tutorial, we are going to learn if statement in C# language. The if statement is a single purpose statement only for true condition.


In C# programming, the if statement is used to test the condition. if the condition is true, the block of code is executed. The block of code is the place inside two { } curly braces after if condition.

In other words, in C# programming, the if condition is used for one state which is true. In C#, if the condition is true, it executes the code of block and if the condition is false, the compiler exit the if block and executes the code after if statement. C programming is the base of all programming. We already learned if statement in C programming. In C# language, if you want to execute only one code based on condition true, then you can use if statement. if statement depends on boolean expression followed by one or more statements.

 

Syntax of if statement in C# language -


if(boolean expression) {
   Statement (Code to be executed when the expressin is true);
}


Let's understand if statement in C# programming.


using System;

namespace IfStatementProgram{
        class IfStatement{
    static void Main(string[] args) {
        int num = 45;
        if (num < 67) {
     /*if the expression is true, execute the if block */
     Console.WriteLine("Boolean exp, True , 1");
            Console.WriteLine("Value of num is less than 67");
         }
         
         /*Always executes whether the expression is true or not | out of if block */
         num++; 
         Console.WriteLine("Now, value of num is "+num);
         Console.ReadLine();
      }
   }
}

Compile and execute the code.

Output -

Boolean exp, True , 1
Value of num is less than 67
Now, value of num is 46

In the above C# example, we declared a variable with a value. We created an expression. if the condition is true, compiler executes the if block code. The outside code always executes, whether if boolean is true or not.
In C#, we can modify the program.

 

 

Let's create another example if the expression is false. Make boolean expression false in C# code.
Let's have a look.


using System;

namespace IfStatementProgram{

   class IfStatement{
   
      static void Main(string[] args) {
        
         int num = 16;
         if (num < 10) {
     /*if the expression is true, execute the if block */
     Console.WriteLine("Boolean exp, True , 1");
            Console.WriteLine("Value of num is less than 67");
         }
         
         /*Always executes whether the expression is true or not | out of if block */
         num++; 
         Console.WriteLine("Now, value of num is "+num);
         Console.ReadLine();
      }
   }
}

Execute this code and get result as output - 

Now, value of num is 17 

In the above C# if statement example, we created a false condition. When the program is executed, the compiler checks the condition but the condition is made false. The compiler exits the if block and executes the outside code (The code after if block, after {} close).

 

Conclusion - The if statement is used only for one state. We can not use if the condition for two ore more statements to be executed. In C# programming, we use if-else statement for both true and false.
In another tutorial, we will discuss C# if-else statement.


Please Share

Recommended Posts:-