Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

16.1.3Declaring a Boolean Variable

A variable is referred to as Boolean if it can hold a value that is either true or false. To declare a Boolean variable, you can use the bool keyword. Here is an example:

using namespace System;

Int main()

{

bool drinkingUnderAge;

return 0;

}

Alternatively, you can declare a Boolean variable using the Boolean data type. The Boolean data type is part of the System namespace. Here is an example:

using namespace System;

Int main()

{

bool drinkingUnderAge;

Boolean TheFloorIsCoveredWithCarpet;

return 0;

}

To display the value of a Boolean variable on the console, you can type its name in the parentheses of the Write() or the WriteLine() methods of the Console class. After the variable has been declared, the compiler initializes it with a false value. This can be demonstrated in the following program:

using namespace System;

Int main()

{

bool drinkingUnderAge;

Console::WriteLine(L"Drinking Under Age: ");

Console::WriteLine(drinkingUnderAge);

return 0;

}

This would produce:

Drinking Under Age: False

Press any key to continue . . .

As you can see, by default, a Boolean variable is initialized with false when it is declared. You can initialize the variable with true or false to make sure you know its initial value. Here is an example:

using namespace System;

Int main()

{

bool drinkingUnderAge = true;

Console::WriteLine(L"Drinking Under Age: ");

Console::WriteLine(drinkingUnderAge);

return 0;

}

At any time and when you judge it necessary, you can change the value of the Boolean variable by assigning it a true or false value. Here is an example:

using namespace System;

Int main()

{

bool drinkingUnderAge = true;

Console::WriteLine(L"Drinking Under Age: ");

Console::WriteLine(drinkingUnderAge);

drinkingUnderAge = false;

Console::WriteLine(L"Drinking Under Age: ");

Console::WriteLine(drinkingUnderAge);

return 0;

}

This would produce:

Drinking Under Age: True

Drinking Under Age: False

Press any key to continue . . .

16.1.4Retrieving the Value of a Boolean Variable

As reviewed for the other data types in Lesson 8, you can request the value of a Boolean variable from the user. In this case, the user must type either True (or true) or False (or false) and you can retrieve it using the Read() or the ReadLine() methods of the Console class. Here is an example:

using namespace System;

Int main()

{

bool drivingUnderAge = false;

Console::WriteLine(L"Were you driving under age?");

Console::Write(L"If Yes, enter True. Otherwise enter False: ");

drivingUnderAge = bool::Parse(Console::ReadLine());

Console::WriteLine(L"\nWas Driving Under Age: "

" 0}\n", drivingUnderAge);

return 0;

}

Here is an example of running the program:

Were you driving under age?

If Yes, enter True. Otherwise enter False: true

Was Driving Under Age: True

Press any key to continue . . .

In Windows programming, to expect the user to enter a specify a value of being true or false, the operating system provides a control named CheckBox.