C# > Operators

Logical operator in C#

How to use logical operator in C#?


Logical operators:

Logical operators are used to perform logical functions. There are many logical operators such as if, else, AND, switch etc are the different perators which are used to find whether the function is true or false.   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace example
{
    public class Program
    {
        public static void Main()
        {
            int age;
            string gender;

            Console.Write("Enter your age: ");
            age = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter your gender (male/female): ");
            gender = Console.ReadLine();

            if (age > 15 && age < 18)
            {
                if (gender == "male")
                {
                    Console.WriteLine("You are a teenage boy.");
                }
                else
                {
                    Console.WriteLine("You are a teenage girl.");
                }
            }
            else
            {
                Console.WriteLine("You are not a teenager.");
            }
        }
    }
}

In the above code snippet we are giving age as integer and string as gender, There is console.write prints enter your age we are converting the age to 32 bit integer. The next line of console.write prints the gender male or female and reads the male or female.  if (age >15 && age <18.   In this condition  If the age is 15 or below 18, It executes to prints the male or female, If gender is male it prints as you are a teenager and if gender is female it executes in to else and executes as you are a teenage girl. if (age>15 && age<18).    If the age is less than 15 and greater than 18 it executes to else statement  and prints the value as you are not a teenager.    

output

 Views: 4875 | Post Order: 8



Write for us






Hosting Recommendations