CSharp - Write program to Working with Logical Boolean Values


In programming, you often work with logical values, which are the values of "yes" and "no." or true false.


The type for logical values is called bool in C#.

The value "yes" is written as true, and the value "no" is written as false.


using System;

class Program// w  ww. j  a  v a2  s. c o m
    static void Main(string[] args)
        // Two logical (Boolean) variables 
        bool goingToRain = true;
        bool iAmHungry = false;

        // Use exclamation mark to negate logical value 
        bool iAmNotHungry = !iAmHungry;

        // Output 
        Console.WriteLine("She loves me: " + goingToRain);
        Console.WriteLine("I am hungry: " + iAmHungry);
        Console.WriteLine("I am not hungry: " + iAmNotHungry);



You can use an exclamation mark to negate a logical value (to flip it from "yes" to "no" and back again).