In: Computer Science
Above codes are written in C++ programming language:
cout is used to display the output in console in C++.
There are few C++ mainpulator functions used in the above code:
endl : A pre-defined function used to insert a new line. It works same as "\n"
boolapha : A pre-defined function used to displaye the output of boolean values in string : true or false
true is displayed when output is 1
false is displayed when output is 0
showpos : A pre-defined function used to display plus/positive sign "+" with boolean flag
+1 is displayed when output is 1
+0 is displayed when output is 0
a. cout << ("Jack" <= "John" && "Business" >= "Accounting") << endl;
OUTPUT : 0
Explanation:
To compare two strings , we compare each alphabet of string from left to right side one by one
1. If first alphabet of one string while comparing become greater or less than first alphabet of other string, we stop the comparison there and it becomes the final result.
Example : "Hello" and "Jam" are two strings to compare
We will start with first alphabet:
'H' and 'J' , as H< J, comparison will end here.
, Hello is less than Jam , and Jam will become greater string
2. If first or more alphabets of one string is equal to first or more alphabets of other string, then we move to the next succedding alphabet of both strings for comparing values.
Example : "Hello" and "Hi" are two strings to compare
We will start with first alphabet:
'H' and 'H' , as both are equal ,
We will move to next alphabet for comparison:
'e' and 'i' , as e< i , comparison will end here.
, Hello is less than Hi , and "Hi " will become greater string
3. If one string's length is greater than other string's length and alphabates are same for both string , except for the last alphabet for string which is having more length , then string with greater length will become the greater string
Example : "Hello" and "Helloy" are two strings to compare
We will start with first alphabet:
'H' and 'H' , as both are equal ,
We will move to next alphabet for comparison:
'e' and 'e' , as both are equal ,
Similarly till 'o', strings are equal
"Helloy" is string with greater length
Hence; "Hello" <"Helloy" and "Helloy" will become greater string
Note : Loop of comparing alphabet of both strings donot stop until , final result is achieved
Now, Firstly , we will compare strings:
"Jack" <= "John" --> True
Lets , start with first alphabets 'J' and 'J' -> both are equal , move to next alphabet
'a' and 'o' -> Since, "a" is smaller than 'o' , "a" <'o' ,
that means , "Jack" will become less than "John", hence , comparison is true
Similarly , "Business" >= "Accounting" -> False
Here, && -> Logical AND operator
if one value is False , when comparing with AND operator ; result will become False
("Jack" <= "John" && "Business" >= "Accounting") -->
True && False --> False
[if one value is False , when comparing with AND operator ; result will become False]
Boolean value of false is 0
Therefore, final result will be 0
___________________________________________
b. cout << boolalpha << ((10 <= 15 - 2) && (15 >= 20) || (15 - 2 != 20)) << endl;
OUTPUT : true
Here, && -> Logical AND operator
if one value is False , when comparing with AND operator ; result will become False
|| -> Logical OR operator
if one value is True , when comparing with OR operator ; result will become True
Firstly we compare the arithematic expressions:
10 <= 15 - 2 -> 10 <=13 -> True
15 >= 20 -> False
15 - 2 != 20 -> 13 != 20 -> True
Now : , we will start from left to right side for logical comparison
(10 <= 15 - 2) && (15 >= 20) -> True && False -> False
[if one value is False , when comparing with AND operator ; result will become False]
((10 <= 15 - 2) && (15 >= 20) || (15 - 2 != 20))
From previous equation :
False || (15 - 2 != 20) - > False || True -> True
[if one value is True , when comparing with OR operator ; result will become True]
Boolean value of True is 1
We have used boolalpha function here , so string value of boolean value will be displayed in result
Therefore, final result will be true
____________________________________________
c. cout << !(30 > 10) << endl;
OUTPUT : 0
Explanation:
! operator is used to Logical Not operator , It is used to complement the value :
if value is true , it will make it false
if value is false, it will convert it to true
Firstly we compare the arithematic expressions:
30 > 10 -> true
!(30 > 10) - > ! (true) -> false
Boolean value of false is 0
Therefore, final result will be 0
__________________________
d. cout << showpos << (6 <= 5 || 3 < 15) << endl;
OUTPUT : +1
Explanation:
Here, || -> Logical OR operator
if one value is True , when comparing with OR operator ; result will become True
Firstly we compare the arithematic expressions:
6<=5 -> False
3<15 -> True
False || True -> True [ if one value is True , when comparing with OR operator ; result will become True]
Boolean value of True is 1
We have used showpos function here , so plus sign will be appended in front of the result
Therefore, final result will be +1
____________________________________________
e. cout << boolalpha << ("bill" > "billy") << endl;
OUTPUT : false
Explanation:
To compare two strings , we compare each alphabet of string from left to right side one by one
1. If first alphabet of one string while comparing become greater or less than first alphabet of other string, we stop the comparison there and it becomes the final result.
Example : "Hello" and "Jam" are two strings to compare
We will start with first alphabet:
'H' and 'J' , as H< J, comparison will end here.
, Hello is less than Jam , and Jam will become greater string
2. If first or more alphabets of one string is equal to first or more alphabets of other string, then we move to the next succedding alphabet of both strings for comparing values.
Example : "Hello" and "Hi" are two strings to compare
We will start with first alphabet:
'H' and 'H' , as both are equal ,
We will move to next alphabet for comparison:
'e' and 'i' , as e< i , comparison will end here.
, Hello is less than Hi , and "Hi " will become greater string
3. If one string's length is greater than other string's length and alphabates are same for both string , except for the last alphabet for string which is having more length , then string with greater length will become the greater string
Example : "Hello" and "Helloy" are two strings to compare
We will start with first alphabet:
'H' and 'H' , as both are equal ,
We will move to next alphabet for comparison:
'e' and 'e' , as both are equal ,
Similarly till 'o', strings are equal
"Helloy" is string with greater length
Hence; "Hello" <"Helloy" and "Helloy" will become greater string
Note : Loop of comparing alphabet of both strings donot stop until , final result is achieved
Now, Firstly , we will compare strings:
"bill" > "billy" -> false
Here, "billy" is word with greater length
and both the string have same alphabets except last alphabet for string with larger length
So, "billy" is the greater string
which , makes this arithematic expression , "bill" > "billy" -> false
Boolean value of false is 0
We have used boolalpha function here , so string value of boolean value will be displayed in result
Therefore, final result will be false