C# if-else Statement :

if Statement :

The main format of the conditional statements if is as follows:

Syntax :

if (Boolean expression)
{
Body of the conditional statement;
}


The Boolean expression can be a Boolean variable or Boolean logical expression. Boolean expressions cannot be integer (unlike other programming languages like C and C++).

The body of the statement is the part locked between the curly brackets ( { } ). It may consist of one or more operations (statements). When there are several operations, we have a complex block operator.

Example :

  1. /* Description: simple if program */
  2. using System;
  3. namespace DemoProgramming
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int myNumber = 5, OtherNumber = 4;
  10. if (myNumber == OtherNumber)
  11. {
  12. Console.WriteLine("This is same number.");
  13. }
  14. //Black Screen Cause The Condition is not true (5==4)
  15. Console.ReadKey();
  16. }
  17. }
  18. }

Output :

C Sharp Languagesimple if program

if-else Statement :

In C#, as in most of the programming languages there is a conditional statement with else clause: the if-else statement. Its format is the following:

Syntax :

if (Boolean expression)
{
Body of the conditional statement;
}
else
{
Body of the else statement;
}

Example :

  1. /* Description: simple if-else program */
  2. using System;
  3. namespace DemoProgramming
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int myNumber = 5;
  10. if (myNumber == 5)
  11. {
  12. Console.WriteLine("This is same number.");
  13. }
  14. else
  15. {
  16. Console.WriteLine("This is not same number.");
  17. }
  18. Console.ReadKey();
  19. }
  20. }
  21. }

Output :

C Sharp Language simple if-else program

Next Update

Loading...

Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels