Ready to master programming decisions? In this unit, we'll explore... Show more
Sign up to see the contentIt's free!
Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy

2
0
Ella Nadine
11/30/2025
Computer Science / Programming
Selection statements Java programming
118
•
Nov 30, 2025
•
Ella Nadine
@nadenden
Ready to master programming decisions? In this unit, we'll explore... Show more











Selection statements are the decision-makers in your code. They allow programs to choose different paths based on whether certain conditions are true or false. Without selections, programs would just execute instructions in sequence without any ability to respond to different situations.
Think of selection statements as forks in a road – they direct your program which way to go based on the conditions you specify.

The Boolean data type is the foundation of all decision-making in programming. It can hold only two values: true or false. Boolean variables store the results of comparisons and logical operations.
Here's how to create Boolean variables:
boolean isRaining = true;
boolean hasHomework = false;
boolean isTeenager = (age >= 13 && age <= 19);
Quick Tip: Boolean values are perfect for representing conditions that are either "yes" or "no" - like whether a user is logged in, if a number is positive, or if homework is completed.
The value of a Boolean can come directly from assignments or from evaluating expressions like comparisons.

Comparison operators let you compare values and create Boolean results. These are the tools that help your program make decisions based on data.
| Operator | Name | Example | Result |
|---|---|---|---|
| < | less than | radius < 0 | false |
| <= | less than or equal to | radius <= 0 | false |
| > | greater than | radius > 0 | true |
| >= | greater than or equal to | radius >= 0 | true |
| == | equal to | radius == 0 | false |
| != | not equal to | radius != 0 | true |
Remember that == (double equals) is for comparing values, while = (single equals) is for assigning values. Mixing these up is one of the most common programming mistakes!

A one-way if statement executes code only when a condition is true. If the condition is false, the program simply skips over the code block and continues.
if (boolean-expression) {
statement(s); // Only runs if condition is true
}
The code inside the curly braces is called a "block" and runs as a single unit. When the condition evaluates to true, all statements inside this block will execute. If the condition is false, the program skips the entire block.
Remember: The parentheses around the boolean expression are required, but the curly braces are optional if there's only one statement inside the block.

If statements work like checkpoints in your code. When the program reaches an if statement, it evaluates the condition and makes a decision.
When the condition is true:
int test = 5;
if (test < 10) {
// This code runs since 5 < 10 is true
}
// Code continues here after the if block
When the condition is false:
int test = 5;
if (test > 10) {
// This code is skipped since 5 > 10 is false
}
// Program jumps directly here
For example, you could check if a circle's radius is valid before calculating its area:
if (radius >= 0) {
area = radius * radius * PI;
System.out.println("The area is " + area);
}

A two-way if statement provides two alternative paths for your code. It executes one block if the condition is true and a different block if the condition is false.
if (boolean-expression) {
statements-for-true-case;
} else {
statements-for-false-case;
}
This is perfect for binary decisions - like whether a number is positive or negative, or if a student passed or failed a test.
Pro Tip: Two-way if statements ensure that exactly one of the two code blocks will always execute - never both and never neither!

If-else statements are like forks in the road - your program must go one way or the other.
When the condition is true:
int test = 5;
if (test < 10) {
// This code runs
} else {
// This code is skipped
}
When the condition is false:
int test = 5;
if (test > 10) {
// This code is skipped
} else {
// This code runs
}
For example, validating a circle's radius with feedback:
if (radius >= 0) {
area = radius * radius * PI;
System.out.println("The area is " + area);
} else {
System.out.println("Negative input");
}

You can place if statements inside other if statements to create more complex decision paths. This is called nesting and allows you to create multi-level decision trees.
if (Boolean_expression1) {
// Executes when expression1 is true
if (Boolean_expression2) {
// Executes when both expressions are true
}
}
Nested if statements are helpful when a second decision depends on the outcome of the first decision. Think of it as asking a series of questions before taking action.
Make It Clear: When using nested if statements, proper indentation is crucial for readability. Each level of nesting should be clearly indented to show the structure of your code.

Here's a simple example using nested if statements:
public class Test {
public static void main(String args[]) {
int x = 30;
int y = 10;
if (x == 30) {
if (y == 10) {
System.out.println("X = 30 and Y = 10");
}
}
}
}
And a more practical example that finds the largest of three numbers:
double n1 = -1.0, n2 = 4.5, n3 = -5.3;
double largestNumber = 0;
if (n1 >= n2)
if (n1 >= n3)
largestNumber = n1;
if (n3 >= n1)
if (n3 >= n2)
largestNumber = n3;
if (n2 >= n1)
if (n2 >= n3)
largestNumber = n2;

Nested if-else statements create multiple decision paths. The clearest way to write complex conditions is using the else if structure:
if (condition1) {
// Executes when condition1 is true
} else if (condition2) {
// Executes when condition1 is false but condition2 is true
} else if (condition3) {
// Executes when both condition1 and condition2 are false but condition3 is true
} else {
// Executes when all conditions are false
}
This structure is perfect for categorizing values into multiple ranges, like assigning letter grades based on scores or determining weight categories from BMI values.
Think Logically: When using multiple else-if branches, arrange your conditions from most specific to most general for the clearest code.
Our AI companion is specifically built for the needs of students. Based on the millions of content pieces we have on the platform we can provide truly meaningful and relevant answers to students. But its not only about answers, the companion is even more about guiding students through their daily learning challenges, with personalised study plans, quizzes or content pieces in the chat and 100% personalisation based on the students skills and developments.
You can download the app in the Google Play Store and in the Apple App Store.
That's right! Enjoy free access to study content, connect with fellow students, and get instant help – all at your fingertips.
App Store
Google Play
The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.
Stefan S
iOS user
This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.
Samantha Klich
Android user
Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.
Anna
iOS user
I think it’s very much worth it and you’ll end up using it a lot once you get the hang of it and even after looking at others notes you can still ask your Artificial intelligence buddy the question and ask to simplify it if you still don’t get it!!! In the end I think it’s worth it 😊👍 ⚠️Also DID I MENTION ITS FREEE YOU DON’T HAVE TO PAY FOR ANYTHING AND STILL GET YOUR GRADES IN PERFECTLY❗️❗️⚠️
Thomas R
iOS user
Knowunity is the BEST app I’ve used in a minute. This is not an ai review or anything this is genuinely coming from a 7th grade student (I know 2011 im young) but dude this app is a 10/10 i have maintained a 3.8 gpa and have plenty of time for gaming. I love it and my mom is just happy I got good grades
Brad T
Android user
Not only did it help me find the answer but it also showed me alternative ways to solve it. I was horrible in math and science but now I have an a in both subjects. Thanks for the help🤍🤍
David K
iOS user
The app's just great! All I have to do is enter the topic in the search bar and I get the response real fast. I don't have to watch 10 YouTube videos to understand something, so I'm saving my time. Highly recommended!
Sudenaz Ocak
Android user
In school I was really bad at maths but thanks to the app, I am doing better now. I am so grateful that you made the app.
Greenlight Bonnie
Android user
I found this app a couple years ago and it has only gotten better since then. I really love it because it can help with written questions and photo questions. Also, it can find study guides that other people have made as well as flashcard sets and practice tests. The free version is also amazing for students who might not be able to afford it. Would 100% recommend
Aubrey
iOS user
Best app if you're in Highschool or Junior high. I have been using this app for 2 school years and it's the best, it's good if you don't have anyone to help you with school work.😋🩷🎀
Marco B
iOS user
THE QUIZES AND FLASHCARDS ARE SO USEFUL AND I LOVE THE SCHOOLGPT. IT ALSO IS LITREALLY LIKE CHATGPT BUT SMARTER!! HELPED ME WITH MY MASCARA PROBLEMS TOO!! AS WELL AS MY REAL SUBJECTS ! DUHHH 😍😁😲🤑💗✨🎀😮
Elisha
iOS user
This app is phenomenal down to the correct info and the various topics you can study! I greatly recommend it for people who struggle with procrastination and those who need homework help. It has been perfectly accurate for world 1 history as far as I’ve seen! Geometry too!
Paul T
iOS user
The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.
Stefan S
iOS user
This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.
Samantha Klich
Android user
Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.
Anna
iOS user
I think it’s very much worth it and you’ll end up using it a lot once you get the hang of it and even after looking at others notes you can still ask your Artificial intelligence buddy the question and ask to simplify it if you still don’t get it!!! In the end I think it’s worth it 😊👍 ⚠️Also DID I MENTION ITS FREEE YOU DON’T HAVE TO PAY FOR ANYTHING AND STILL GET YOUR GRADES IN PERFECTLY❗️❗️⚠️
Thomas R
iOS user
Knowunity is the BEST app I’ve used in a minute. This is not an ai review or anything this is genuinely coming from a 7th grade student (I know 2011 im young) but dude this app is a 10/10 i have maintained a 3.8 gpa and have plenty of time for gaming. I love it and my mom is just happy I got good grades
Brad T
Android user
Not only did it help me find the answer but it also showed me alternative ways to solve it. I was horrible in math and science but now I have an a in both subjects. Thanks for the help🤍🤍
David K
iOS user
The app's just great! All I have to do is enter the topic in the search bar and I get the response real fast. I don't have to watch 10 YouTube videos to understand something, so I'm saving my time. Highly recommended!
Sudenaz Ocak
Android user
In school I was really bad at maths but thanks to the app, I am doing better now. I am so grateful that you made the app.
Greenlight Bonnie
Android user
I found this app a couple years ago and it has only gotten better since then. I really love it because it can help with written questions and photo questions. Also, it can find study guides that other people have made as well as flashcard sets and practice tests. The free version is also amazing for students who might not be able to afford it. Would 100% recommend
Aubrey
iOS user
Best app if you're in Highschool or Junior high. I have been using this app for 2 school years and it's the best, it's good if you don't have anyone to help you with school work.😋🩷🎀
Marco B
iOS user
THE QUIZES AND FLASHCARDS ARE SO USEFUL AND I LOVE THE SCHOOLGPT. IT ALSO IS LITREALLY LIKE CHATGPT BUT SMARTER!! HELPED ME WITH MY MASCARA PROBLEMS TOO!! AS WELL AS MY REAL SUBJECTS ! DUHHH 😍😁😲🤑💗✨🎀😮
Elisha
iOS user
This app is phenomenal down to the correct info and the various topics you can study! I greatly recommend it for people who struggle with procrastination and those who need homework help. It has been perfectly accurate for world 1 history as far as I’ve seen! Geometry too!
Paul T
iOS user
Ella Nadine
@nadenden
Ready to master programming decisions? In this unit, we'll explore how Java makes choices using selection statements like if, if-else, and nested conditions. These powerful tools let your programs make decisions based on different conditions, forming the backbone of program... Show more

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Selection statements are the decision-makers in your code. They allow programs to choose different paths based on whether certain conditions are true or false. Without selections, programs would just execute instructions in sequence without any ability to respond to different situations.
Think of selection statements as forks in a road – they direct your program which way to go based on the conditions you specify.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
The Boolean data type is the foundation of all decision-making in programming. It can hold only two values: true or false. Boolean variables store the results of comparisons and logical operations.
Here's how to create Boolean variables:
boolean isRaining = true;
boolean hasHomework = false;
boolean isTeenager = (age >= 13 && age <= 19);
Quick Tip: Boolean values are perfect for representing conditions that are either "yes" or "no" - like whether a user is logged in, if a number is positive, or if homework is completed.
The value of a Boolean can come directly from assignments or from evaluating expressions like comparisons.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Comparison operators let you compare values and create Boolean results. These are the tools that help your program make decisions based on data.
| Operator | Name | Example | Result |
|---|---|---|---|
| < | less than | radius < 0 | false |
| <= | less than or equal to | radius <= 0 | false |
| > | greater than | radius > 0 | true |
| >= | greater than or equal to | radius >= 0 | true |
| == | equal to | radius == 0 | false |
| != | not equal to | radius != 0 | true |
Remember that == (double equals) is for comparing values, while = (single equals) is for assigning values. Mixing these up is one of the most common programming mistakes!

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
A one-way if statement executes code only when a condition is true. If the condition is false, the program simply skips over the code block and continues.
if (boolean-expression) {
statement(s); // Only runs if condition is true
}
The code inside the curly braces is called a "block" and runs as a single unit. When the condition evaluates to true, all statements inside this block will execute. If the condition is false, the program skips the entire block.
Remember: The parentheses around the boolean expression are required, but the curly braces are optional if there's only one statement inside the block.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
If statements work like checkpoints in your code. When the program reaches an if statement, it evaluates the condition and makes a decision.
When the condition is true:
int test = 5;
if (test < 10) {
// This code runs since 5 < 10 is true
}
// Code continues here after the if block
When the condition is false:
int test = 5;
if (test > 10) {
// This code is skipped since 5 > 10 is false
}
// Program jumps directly here
For example, you could check if a circle's radius is valid before calculating its area:
if (radius >= 0) {
area = radius * radius * PI;
System.out.println("The area is " + area);
}

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
A two-way if statement provides two alternative paths for your code. It executes one block if the condition is true and a different block if the condition is false.
if (boolean-expression) {
statements-for-true-case;
} else {
statements-for-false-case;
}
This is perfect for binary decisions - like whether a number is positive or negative, or if a student passed or failed a test.
Pro Tip: Two-way if statements ensure that exactly one of the two code blocks will always execute - never both and never neither!

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
If-else statements are like forks in the road - your program must go one way or the other.
When the condition is true:
int test = 5;
if (test < 10) {
// This code runs
} else {
// This code is skipped
}
When the condition is false:
int test = 5;
if (test > 10) {
// This code is skipped
} else {
// This code runs
}
For example, validating a circle's radius with feedback:
if (radius >= 0) {
area = radius * radius * PI;
System.out.println("The area is " + area);
} else {
System.out.println("Negative input");
}

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
You can place if statements inside other if statements to create more complex decision paths. This is called nesting and allows you to create multi-level decision trees.
if (Boolean_expression1) {
// Executes when expression1 is true
if (Boolean_expression2) {
// Executes when both expressions are true
}
}
Nested if statements are helpful when a second decision depends on the outcome of the first decision. Think of it as asking a series of questions before taking action.
Make It Clear: When using nested if statements, proper indentation is crucial for readability. Each level of nesting should be clearly indented to show the structure of your code.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Here's a simple example using nested if statements:
public class Test {
public static void main(String args[]) {
int x = 30;
int y = 10;
if (x == 30) {
if (y == 10) {
System.out.println("X = 30 and Y = 10");
}
}
}
}
And a more practical example that finds the largest of three numbers:
double n1 = -1.0, n2 = 4.5, n3 = -5.3;
double largestNumber = 0;
if (n1 >= n2)
if (n1 >= n3)
largestNumber = n1;
if (n3 >= n1)
if (n3 >= n2)
largestNumber = n3;
if (n2 >= n1)
if (n2 >= n3)
largestNumber = n2;

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Nested if-else statements create multiple decision paths. The clearest way to write complex conditions is using the else if structure:
if (condition1) {
// Executes when condition1 is true
} else if (condition2) {
// Executes when condition1 is false but condition2 is true
} else if (condition3) {
// Executes when both condition1 and condition2 are false but condition3 is true
} else {
// Executes when all conditions are false
}
This structure is perfect for categorizing values into multiple ranges, like assigning letter grades based on scores or determining weight categories from BMI values.
Think Logically: When using multiple else-if branches, arrange your conditions from most specific to most general for the clearest code.
Our AI companion is specifically built for the needs of students. Based on the millions of content pieces we have on the platform we can provide truly meaningful and relevant answers to students. But its not only about answers, the companion is even more about guiding students through their daily learning challenges, with personalised study plans, quizzes or content pieces in the chat and 100% personalisation based on the students skills and developments.
You can download the app in the Google Play Store and in the Apple App Store.
That's right! Enjoy free access to study content, connect with fellow students, and get instant help – all at your fingertips.
2
Smart Tools NEW
Transform this note into: ✓ 50+ Practice Questions ✓ Interactive Flashcards ✓ Full Mock Exam ✓ Essay Outlines
App Store
Google Play
The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.
Stefan S
iOS user
This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.
Samantha Klich
Android user
Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.
Anna
iOS user
I think it’s very much worth it and you’ll end up using it a lot once you get the hang of it and even after looking at others notes you can still ask your Artificial intelligence buddy the question and ask to simplify it if you still don’t get it!!! In the end I think it’s worth it 😊👍 ⚠️Also DID I MENTION ITS FREEE YOU DON’T HAVE TO PAY FOR ANYTHING AND STILL GET YOUR GRADES IN PERFECTLY❗️❗️⚠️
Thomas R
iOS user
Knowunity is the BEST app I’ve used in a minute. This is not an ai review or anything this is genuinely coming from a 7th grade student (I know 2011 im young) but dude this app is a 10/10 i have maintained a 3.8 gpa and have plenty of time for gaming. I love it and my mom is just happy I got good grades
Brad T
Android user
Not only did it help me find the answer but it also showed me alternative ways to solve it. I was horrible in math and science but now I have an a in both subjects. Thanks for the help🤍🤍
David K
iOS user
The app's just great! All I have to do is enter the topic in the search bar and I get the response real fast. I don't have to watch 10 YouTube videos to understand something, so I'm saving my time. Highly recommended!
Sudenaz Ocak
Android user
In school I was really bad at maths but thanks to the app, I am doing better now. I am so grateful that you made the app.
Greenlight Bonnie
Android user
I found this app a couple years ago and it has only gotten better since then. I really love it because it can help with written questions and photo questions. Also, it can find study guides that other people have made as well as flashcard sets and practice tests. The free version is also amazing for students who might not be able to afford it. Would 100% recommend
Aubrey
iOS user
Best app if you're in Highschool or Junior high. I have been using this app for 2 school years and it's the best, it's good if you don't have anyone to help you with school work.😋🩷🎀
Marco B
iOS user
THE QUIZES AND FLASHCARDS ARE SO USEFUL AND I LOVE THE SCHOOLGPT. IT ALSO IS LITREALLY LIKE CHATGPT BUT SMARTER!! HELPED ME WITH MY MASCARA PROBLEMS TOO!! AS WELL AS MY REAL SUBJECTS ! DUHHH 😍😁😲🤑💗✨🎀😮
Elisha
iOS user
This app is phenomenal down to the correct info and the various topics you can study! I greatly recommend it for people who struggle with procrastination and those who need homework help. It has been perfectly accurate for world 1 history as far as I’ve seen! Geometry too!
Paul T
iOS user
The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.
Stefan S
iOS user
This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.
Samantha Klich
Android user
Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.
Anna
iOS user
I think it’s very much worth it and you’ll end up using it a lot once you get the hang of it and even after looking at others notes you can still ask your Artificial intelligence buddy the question and ask to simplify it if you still don’t get it!!! In the end I think it’s worth it 😊👍 ⚠️Also DID I MENTION ITS FREEE YOU DON’T HAVE TO PAY FOR ANYTHING AND STILL GET YOUR GRADES IN PERFECTLY❗️❗️⚠️
Thomas R
iOS user
Knowunity is the BEST app I’ve used in a minute. This is not an ai review or anything this is genuinely coming from a 7th grade student (I know 2011 im young) but dude this app is a 10/10 i have maintained a 3.8 gpa and have plenty of time for gaming. I love it and my mom is just happy I got good grades
Brad T
Android user
Not only did it help me find the answer but it also showed me alternative ways to solve it. I was horrible in math and science but now I have an a in both subjects. Thanks for the help🤍🤍
David K
iOS user
The app's just great! All I have to do is enter the topic in the search bar and I get the response real fast. I don't have to watch 10 YouTube videos to understand something, so I'm saving my time. Highly recommended!
Sudenaz Ocak
Android user
In school I was really bad at maths but thanks to the app, I am doing better now. I am so grateful that you made the app.
Greenlight Bonnie
Android user
I found this app a couple years ago and it has only gotten better since then. I really love it because it can help with written questions and photo questions. Also, it can find study guides that other people have made as well as flashcard sets and practice tests. The free version is also amazing for students who might not be able to afford it. Would 100% recommend
Aubrey
iOS user
Best app if you're in Highschool or Junior high. I have been using this app for 2 school years and it's the best, it's good if you don't have anyone to help you with school work.😋🩷🎀
Marco B
iOS user
THE QUIZES AND FLASHCARDS ARE SO USEFUL AND I LOVE THE SCHOOLGPT. IT ALSO IS LITREALLY LIKE CHATGPT BUT SMARTER!! HELPED ME WITH MY MASCARA PROBLEMS TOO!! AS WELL AS MY REAL SUBJECTS ! DUHHH 😍😁😲🤑💗✨🎀😮
Elisha
iOS user
This app is phenomenal down to the correct info and the various topics you can study! I greatly recommend it for people who struggle with procrastination and those who need homework help. It has been perfectly accurate for world 1 history as far as I’ve seen! Geometry too!
Paul T
iOS user