4. Decision making statements: if else - ZolotovaNatalia/JavaCourse_2017 GitHub Wiki
In order to execute a specific part of code you might want some conditions to be fulfilled. For example, if a person is more then 18 years old, he can receive an access to some information, otherwise a message "You are too young to see that". In this case you use a conditional word if. Such situations are very common in real life, that is why in programming languages if-else statement has been developed.
Let's see how it is used in Java:
if(condition is true){
statement
}else {
another statement
}
Condition in braces must be a boolean expression. Boolean expressions are expressions that return a boolean value true or false.
Here is a table with boolean expressions:
Operator | Name of operator | Meanning | Example |
&& | and | True if and only if both sides are true | warm && sunny |
|| | or | True if either side is true (or if both are true) | blue || red |
! | not | Changes true to false, and false to true | !tired |
These boolean expressions are often used to create a combination of several condition that should be satisfied. Boolean condition can be expressed with mathematical operators:
Operator | Name of operator |
---|---|
< | less than |
<= | less than or equal to |
== | equal to |
!= | not equal to |
>= | greater than or equal to |
> | greater than |
For example, if(a > b), if(age > 18)
. If you want to check the equality condition, use sign ==. For example, if(a == b), if(age == 18)
.
Let's practice in Java.
- Compare two int values using mathematical operators
int a = 1;
int b = 5;
if(a > b){
System.out.println("a > b");
}else if(a < b){
System.out.println("a < b");
}else {
System.out.println("a == b");
}
if(a >= b){
System.out.println("a >= b");
}else {
System.out.println("a < b");
}
// boolean expression !=
if(a != b){
System.out.println("a != b");
}
//boolean expression ==
if(a == b){
System.out.println("a == b");
}
- Compare String variables:
String string1 = "text1";
String string2 = "text2";
System.out.println("string1 has length = " + string1.length());
System.out.println("string2 has length = " + string2.length());
if(string1.length() > string2.length()){
System.out.println("string1 is longer then string2");
}else {
System.out.println("string1 is shorter then string2");
}
if(string1.equals(string2)){
System.out.println("string1 equals to string2");
}else {
System.out.println("string1 and string2 are different");
}
- Boolean expressions: &&, ||:
int a = 1;
int b = 2;
int c = 3;
int d = 4;
// && expression
if((a > b) && (c > d)){
System.out.println("Both conditions are true: a > b and c > d");
}else {
System.out.println("One of the conditions is false: a > b or c > d");
}
// || expression
if((a > b) || (c > d)){
System.out.println("One of the conditions is true: a > b or c > d");
}else {
System.out.println("Both conditions are false: a > b and c > d");
}
- Compare boolean values:
boolean a = true;
if(a){
System.out.println("a is true");
}else {
System.out.println("a is false");
}
boolean b = false;
if(!b){
System.out.println("b is false");
}else {
System.out.println("b is true");
}
if(a == b){
System.out.println("a == b");
}else {
System.out.println("a != b");
}
- Conditions in array loops:
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for(int i = 0; i < a.length; i++){
if(a[i] > 4){
System.out.println("a[" + i + "] = " + a[i]);
}
}
String[] stringArray = {"a", "ab", "abc", "abcd"};
System.out.println("old stringArray = " + Arrays.toString(stringArray));
int j = 0;
while(j < stringArray.length){
if(!stringArray[j].equals("abcd")){
stringArray[j] = "abcd";
}
j++;
}
System.out.println("new stringArray = " + Arrays.toString(stringArray));
- Write a Java program to find the maximum and minimum value of an array.
- Write a Java program to find the index of an array element.
- Write a Java program to test the equality of two arrays.
- Write a Java program to check if an array of integers without 0 and -1.