Boolean type

Available

"Hi, Amigo. Let me tell you about a new data type. The boolean. Variables of this type can take only two values: true and false."

"How do we use it?"

"This type is implicitly used in many places. Just as any addition operation produces a number, the result of any comparison is a boolean. Here are some examples:"

Code Explanation
1
boolean m;
These two expressions are equivalent. The default value of a boolean variable is false.
2
boolean m = false;
3
if (a > b)
    System.out.println(a);
The result of the comparison (either true or false) will be assigned to the variable m. The condition is satisfied if the expression evaluates to true.
4
boolean m = (a > b);
if (m)
    System.out.println(a);
5
boolean m = (a > b);
if (m == true)
    System.out.println(a);
There is no need to compare a logical (boolean) variable with true or false. The result of the comparison will be a boolean that matches the other variable. For example, true == true evaluates to true; true == false evaluates to false.
6
boolean m = (a > b);
if (m)
    System.out.println(a);

"More examples:"

Code Explanation
1
public boolean isALessThenB (int a, int b)
{
    if (a < b)
        return true;
    else
        return false;
}
This method verifies that number a is less than number b.

Here are four equivalent comparisons. The last one is the most compact and correct. Always try to use compact notation.

2
public boolean isALessThenB (int a, int b)
{
   boolean m = (a < b);
    if (m)
        return true;
    else
        return false;
}
3
public boolean isALessThenB (int a, int b)
{
    boolean m = (a < b);
    return m;
}
4
public boolean isALessThenB (int a, int b)
{
    return a < b;
}
2
Task
Java Syntax,  level 4lesson 7
Locked
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and earn a little dark matter. What's more, you combat laziness.

"What if I want to write 0<a<b?"

"Java doesn't have a comparison operator that takes three operands. So, you would need to do it like this: (0<a) AND (a<b)."

"Do I write the word AND?"

"Wait. I'll explain that. Java has three logical operators: AND, OR and NOT. You can use them to construct conditions of varying complexity. You can use these operators only with boolean expressions. So, you can't write (a+1) AND (3), but (a>1)AND (a<3) is OK."

"The NOT operator is unary: it affects only the expression to the right. It's more like a minus sign before a negative number rather than a multiplication sign between two numbers."

"You can perform various operations on boolean (logical) variables."

"Like what?"

"Let's take a look:"

Logical operator Java notation Expression Result
AND && true && true true
true && false false
false && true false
false && false false
OR || true || true true
true || false true
false || true true
false || false false
NOT ! ! true false
! false true
Common combinations and expressions m && !m false
m || !m true
! (a && b) !a || !b
! (a || b) !a && !b

"Could you give me more examples?"

"Sure:"

Java notation Logical notation
(a<3) && (a>0) (a < 3) AND (a>0)
(a>10) || (a<100) (a>10) OR (a<100)
(a<b) && (!(c<=d)) (a<b) AND (NOT (c<=d))

"Now, do some tasks."

4
Task
Java Syntax,  level 4lesson 7
Locked
Labels and numbers
Not all labels are equally useful, but sometimes a lack of labels results in chaos. Let's do a task that dishes out labels: use the keyboard to enter an integer, and make the program indicate whether the number is negative or positive (or zero), and even or odd. Why do we need this? Just because, for practice. After all, we're here to learn!
8
Task
Java Syntax,  level 4lesson 7
Locked
Describing numbers
Suppose we need to separate numbers according to some criteria. For example, based on whether they are even or odd, or based on the number of digits. Have you got a picture of that in your mind? And now for the work: we'll write a program that describes the number entered by the user (it must be in the range from 1 to 999). The description will indicate whether the number is even or odd, and how many digits it has.
4
Task
Java Syntax,  level 4lesson 7
Locked
Positive number
At the "Optimist" school, the scale used to assess knowledge has neither an upper nor lower bound. This pleases young gifted people (wouldn't you be happy to receive a score of a bazillion for a presentation?). All scores above zero are considered good. Scores below zero are bad. Students must pass final exams in three subjects. We need to enter three scores from the keyboard, and display the number of positive scores.
4
Task
Java Syntax,  level 4lesson 7
Locked
Positive and negative numbers
Data analysis starts with a simple concept: we need to understand what data matters and should be used, and what data should be discarded. Let's solve a simple data analysis problem. We enter three integers from the keyboard, then we'll analyze them and display the number of positive and negative numbers in the source set.
Comments(61)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Agent SmithLevel 6 , Riga
Saturday, 17:29
Some useful hints and tips: 1) Someone might find this piece of code interesting in a certain exercise:
msg = "Negative ";
msg += (input % 2 == 0) ? "even number" : "odd number";
2) If you type sout and press tab in intelliJ it will auto type System.out.println(); for you. 3) If you want to make your own shortcuts for typing code, you can use Live templates
iradesLevel 8 , Moscow
29 July, 16:48
I noticed a few typos after the "More examples:" line in the name of the method "isALessThenB" If mods check this, please fix :)
Maryem VickersLevel 5 , HT...
23 July, 19:58
I've got a nice way of doing those *annoying* code entries XD
All done/ |:)
XD
Maryem VickersLevel 5 , HT...
23 July, 20:26
And then I made a song to remember that table of falses and trues... Mostly False == True
2 Trues are True,
A False and a True is False.
A True And A False is False,
No matter the Orderooe.
++ False and False is very False.
BUT
True OR True is, of course very true.
AND True OR False?! It is true!
AND False OR  True?! Funny order, but it is true...
! to care what or which Orderooe...

AND of COURSE FALSE OR FALSE?! IT MUST BE FALSE!
YOU JUST GAVE ME THE ANSWER!

OK then, CodeGym is the BEST.
! True?! NO WAY. FALSE.
! False?! I guess so.
Well then, that must be that, no needa worry about ORDEREST!!!
XD quite funny. (P.S. ORDEREST also, not just means ORDER but also sounds like ALL-THE-REST! XD)
Aleksandar NeshkovskiLevel 6 , Skopje
22 May, 12:01
Thinking is so 21st century...
Maryem VickersLevel 5 , HT...
27 July, 19:42
XD IT ISSS SECRET CODE GYM CENTRE SHOULDN'T... Well I guess everyone has to think, shouldn't they?! I mean how can we even live without... XD
David WilliamsLevel 8 , Chicago
4 March, 14:57
These tasks do not require complicated solutions. Just stick to the basics! use what codeGym has taught you thus far! I assure you it can be done using only if/else with boolean expressions. Using OOP/Arrays/Loops at this level of experience is unnecessary and I believe harmful to your future learnings. All the tasks are somewhat similar: 1. Read in Integer from keyboard. 2. Parse the integer(s) and store in SEPARATE variables. 3. Complete if else logic using the input integers. Timely route, but it reinforces what we're learning at the moment. I hope this helps!
bobLevel 8 , Milan
20 May, 21:23
why do you consider harmful? just wondering...
Agent SmithLevel 6 , Riga
Friday, 19:34
Well, you are right ofc, but nothing can stop you from implementing several solutions. I think it might even be a better approach to think how can one problem be solved differently and then compare and decide which one is better.
Randy WilliamsLevel 5 , Seattle
21 February, 01:17
then or than?
Gellert VargaLevel 11 , Szekesfehervar
13 March, 12:00
isALessThanB(int a, int b)...
M TillLevel 7 , San Jose
25 July, 23:46
Good catch, I didn't even notice the mistake at first xD
Maryem VickersLevel 5 , HT...
28 July, 18:37
Haaah that is a funny one XD I mean then means, well,at the moment then... ugh... At that time, that's the meaning. Than is different as we now know. XD
Timothy StricklandLevel 9 , Fairfax
19 February, 03:57
I love this course, coding using the concepts I learn is now becoming second nature, I can build if and else if statements to solve math problems in a matter of a couple of minutes!
Maryem VickersLevel 5 , HT...
28 July, 18:52
Yayyy you are lucky XD I havent got it yet... even though I did Java before XD
Stormy MorningLevel 6 , Draper
18 February, 19:24
i did all of these using only if statements so its totally possible without array's or loops. I know there are more advanced ways to solve these but honestly i don't know them yet and if figure using only what i know now helps reinforce existing concepts
Thomas SixberryLevel 16 , Rochester Hills
22 January, 17:12
OMG! The last lesson I wrote 'even' instead of 'positive' and spent like 15 minutes scratching my head. LOL
Geoff GreenLevel 10 , Calgary
29 January, 05:21
haha me too, but it was a quick uh oh.
Maryem VickersLevel 5 , HT...
24 July, 20:13
XD OwO I think codegym GOTTA do something about these kinda stuff.... even then...
Maryem VickersLevel 5 , HT...
24 July, 20:13
XD
MichaelLevel 7 , Dresden
27 October 2019, 10:41
Did anyone find a solution without using arrays and loops? I wonder, whether it is possible to navigate this task with the instruments codegym has given us so far. :-(
Linnert LoobuyckLevel 5 , Gent
11 December 2019, 13:23
I tried to create some classes. I created 2 extra classes. First class named counter, second class named checker. in the counter class i created a variable "count" , which i used in my main class to count the number of positive numbers. in the checker class i created the if statement that adds 1 to the count variable, if the entered number is a positive one. in your main class you call the checker method on your 3 entered integers. Then, to have output, you print your variable count. This worked fine for me, and the exercise accepted my solution. Hint: to call the right variable in the checker method, i revised Lesson 4.1 (especially the exercise "price of apples" This is my first time to write a hint, i hope it's helpful. Goodluck!
ChristopherLevel 10
14 December 2019, 15:40
clunky, but yes. define a count variable and then check each number with an if statement. those that are positive add one to your count variable.
26 December 2019, 10:41
i did by using only if n else like a stupid!
Vicky MillinshipLevel 8 , Walton le Dale
11 January, 16:17
I'm kind of having an easier/harder time in that my boyfriend is a software developer so I have him to ask. I kept on saying I thought this could be done with an array and he just kept on trying to show me really difficult, fancy ways of doing stuff...kind of wish I could reverify with something else using if statements only.
Brad HawkLevel 8 , Everett
9 April, 18:15
This could definitely be done with an array and a loop. Personally, I think the code looks better and is easier to read than a bunch of if statements, but you can accomplish it either way