Recent

6/recent/ticker-posts

Header Ads Widget

IF else Example C sharp

IF else in C sharp

if else statement is used in c sharp to check condition . In If statement we can check the condition and return true statement and if the statement became false it goes into else part but if you don't want to show false it is not necessary to write else part 


Here we are taken one string variable and which is taken by the user using Conole,Readline() function. and in 1st if statement if statement is blank it return and display the "You typed in an empty string" but if string has some statement or word it count the number of character from the string (Here  input.length method return the number of character) . Here I used multiple else if for check the multiple statement. Remember that when the condition meet the statement checking process break and goes to out of if else statement. If statement does not match the condition it goes to else part and return it


Copy and paste the  bellow code
using System;

namespace tutorials
{
   class ifelseexc
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Type in a string");
         string input;
         input = Console.ReadLine();
         if (input == "")
         {
            Console.WriteLine("You typed in an empty string");
         }
         else if (input.Length < 5)
         {
            Console.WriteLine("The string had less than 5 characters");
         }
         else if (input.Length < 10)
         {
            Console.WriteLine("The string had at least 5 but less than 10 characters");
         }
         else
        {
         Console.WriteLine("The string was " + input);
        }
      }
   }
}


Post a Comment

0 Comments