0% found this document useful (0 votes)
26 views9 pages

Decisions

If/else statements allow code to execute conditionally based on whether an expression is true or false. An if statement executes code if the condition is true, while an else statement executes code if the condition is false. Else if statements allow testing multiple conditions sequentially. Switch statements allow executing different code blocks based on the value of an expression and use case labels. Boolean expressions return true or false and use relational, logical, and ternary operators to compare values and control program flow.

Uploaded by

Krinal kathiriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Decisions

If/else statements allow code to execute conditionally based on whether an expression is true or false. An if statement executes code if the condition is true, while an else statement executes code if the condition is false. Else if statements allow testing multiple conditions sequentially. Switch statements allow executing different code blocks based on the value of an expression and use case labels. Boolean expressions return true or false and use relational, logical, and ternary operators to compare values and control program flow.

Uploaded by

Krinal kathiriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

The topic our group chose is Decisions.

If Statement

In Java, decision making statements are one of the


fundamentals used to control the flow of the Java
code. It helps to decipher when to execute a
statement when given a set of conditions.
Syntax for if statement:
if (condition) {
// block of code to be executed if the condition is true

When using an if statement, it signifies that that block of code will execute if the specified
condition is true. It is always written as lowercase “if”.

Example:

int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");

In the example above, we created two variables x and y, and are testing whether x is greater
than y. If (x>y) is true, our output will print out “x is greater than y”. Being that x is 20 and y is 18,
we know 20>18 is true so it will print the message onto the screen.

If statements can’t execute if the statement is false. Therefore we would need an else statement
below to tell the program what to print when the statement is false. In the next section below you
will see the syntax for if/else statements and how they work.
Comparing Values
There are many ways to compare two Strings in Java:

● Using == operator

● Using equals() method

● Using compareTo() method

● Using compareToIgnoreCase() method

● Using compare() method

Examples using following comparing values:

1) Using == operator

Output: a and b are equal

2) Using equals() method


Output: str1 and str2 are not equal
3) Using compareTo() method

Output: str1 comes before str2


4) Using compareToIgnoreCase() method

Output: str1 comes before str2


5) Using compare() method
Output: num1 is greater than num2

Multiple Alternatives

IF/ELSE (Evelyn)
Previously in the if statement, if the condition of the if statement is true, then the block of code inside the
if statement would execute. This would work only for conditions that are true. But how about if the
conditions are false. That’s where the else statement comes in, the code inside the else statement will be
executed when the condition is false.

Syntax for if else statement:


if(condition) {
//code executes if condition is true
} else{
//code executes if the condition is false
}

Example:
public class Main {
public static void main(String[] args) {
int age = 35;
//condition is age>=18
if (age >= 18) {
//code executes if condition is true
System.out.println(" You are an adult!");
} else{
//code executes if the condition is false
System.out.println("You’re still a child!");
}
}
}

In the example, the age input is 35 and in the if/else statement, the condition of the if is that if age is
greater than or equal to 18 it prints out “You are an adult!” if not then it prints out “You're still a child!”.
If/ Else If/ Else
Instead of testing one condition, the else if is allowed to test multiple conditions in a sequence starting
from the if, where each condition is tested only if the previous condition(s) were not true. Once the
condition is true, the statement for that condition would be executed.

Syntax for else if statement:


if(condition) {
//code executes if condition is true
} else if(condition2){
//code executes if the previous condition
//is false and this is true
} else{
//code executes if all the conditions are false
}

Example:
public class Main {
public static void main(String[] args) {
String day = "Monday";
//condition -> age>=18
if (day == "Friday") {
//code executes if condition is true
System.out.println("Time for the weekend!");
} else if(day == "Wednesday"){
//code executes if the previous condition is false and this
//is false and this is true
System.out.println("You're half-way through the week");
} else {
//code executes if all the conditions were false
System.out.println("The week just started!");
}
}
}

In the example, the string variable day is assigned the value Monday. Then the if/else if/ else statement is
used to test the value of day against three conditions. The first condition is testing if the value is Friday,
then the program would print “Time for the weekend”. The next condition is if the value is Wednesday,
then it would print “You’re halfway through the week”. If none of the conditions are true, then it would
print “The week just started”.
Switch statement

With the switch statement there can be various possible values that can
be executed if it is true based on the expression expressed in the switch
statement. The switch statement consists of a switch block and in the
switch block there can be one or more case labels plus a default one if
none of the cases are true. The switch statement works with the byte,
short, char and int primitive data types

Syntax:
switch(expression) {
case 1:
//code
break;
case 2:
//code
break;
default:
//code
break;
}

Example:
public class Main {
public static void main(String[] args) {
int flowers = 3;
String flowerString;
switch (flowers){
case 1: flowerString = "tulips";
break;
case 2: flowerString = "rose";
break;
case 3: flowerString = "lily";
break;
case 4: flowerString = "peony";
break;
case 5: flowerString = "orchid";
break;
default: flowerString = "Invalid flower number";
break;
}
System.out.println(flowerString);
}
}

The program declares an integer variable called flowers and initializes to a value of 3. It then declares a
String variable called flowerString that will be used to store the name of a flower based on the value of
flowers. The switch statement is used to determine which flower name is assigned to the flowerString
based on the value of flowers. The switch statement checks the value of flowers and executes the code
block corresponding to the case that matches the value. In this program flower has a value of 3 and the
code block for case 3 will be executed, in this case it assigns the value lily to flowerString. If for any
reason flowers do not match up to any of the case values, the code block for the default case will be
executed.
Using Boolean Expressions - Ziad
Boolean expressions are expressions that evaluate to a Boolean (True or False). They are a very
important part of programming since they determine which statements are executed based on a
given condition.

Example:
if(Boolean expression){
System.out.println("True");
}
In order to create boolean expressions and compare different data types, we use operators. There
are three types of operators that can be used to create boolean expressions.

Relational Operators
Relational Operators are operators that check the relation between two operands. There are 6
relational operators and they are:

1. == is the equality operator returns true if the two operands are equal or false if they’re
not.
2. != is the inequality operator returns false if the two operands are equal or false if they’re
not.
3. < is the less than operator returns true if the first operand is less than the second operand
or false if it’s greater or equal to.
4. > is the greater than operator returns true if the first operand is greater than the second
operand or false if it’s less or equal to.
5. <= is the less than or equal to operator returns true if the first operand is less than or equal
to the second operand or false if it’s greater.
6. >= is the greater than or equal to operator returns true if the first operand is greater than
or equal to the second operand or false if it’s less.
Example:
int x = 5;
int y = 6;
if(x <= y){
System.out.println("true");
}
Output: True
Explanation: x(5) is less than or equal to y(6), so the output is true.
Logical Operators
Logical operators are operators that combine or alter the final result of a boolean expression or
value. There are 3 logical operators and they are:

1. && is the AND operator and it outputs true if both expressions are true, or false one of
the expressions is false.
2. || is the OR operator and it outputs true if one of the expressions is true, or false if both
expressions are false.
3. ! is the NOT operator. You put it before an expression to get the inverse of the output and
it outputs false if the expression is true, or true if the expression is false.

Example:
boolean x = true;
boolean y = false;
if(x && !(y)){
System.out.println("true");
}
Output: true
Explanation: x is true and the inverse of y is true, so the output is true.

Conditional Operator

The conditional operator (?) also (known as the Ternary operator) is an operator that checks a
condition and executes based on the output of the condition. It’s the same as an if-else statement,
but rather more compact. It’s used for quick assignments rather than executing code blocks.
Notice: we use the the (?) sign after the condition and we separate the two outputs with a (:).

Example:
boolean condition = true;
String result = (condition) ? "True" : "False";
System.out.println(result);

Output: true
Explanation: Just like an if-else statement if condition is true it executes the part before the colon
and if its false it executes after the colon.
Boolean expressions are essential in Java programming as they allow us to make decisions based
on whether certain conditions are met or not. By using relational, logical, and conditional
operators, we can create complex boolean expressions that can be used to control the flow of a
program.

DECISIONS

You might also like