02.1 PB-Java-Conditional-Statements

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 46

Conditional Statements

Logical Expression and Conditional Statement if-else

SoftUni Team
SoftUni Global
https://softuni.org
Table of Contents

1. Review
2. Logical Expressions
▪ Comparison Operators
3. Conditional Statements
4. Rounding and Formatting
5. Debugging
6. Sequence of Conditions
7. Variable Scope 2
Previous Lesson Review
Review
1. What is the type of the variable: ... number = "1000";

char int

String
double

4
Review
2. What is the type of the variable: ... number = 1000;

int String

char
double

5
Review

3. When we join two texts (strings), this operation is called:

Join
Concatenate

Glue Merge

6
Review
4. What will be printed on the console if we run the following
command: System.out.println(10 % 3);

1
10

0 3
7
Review
5. What value does the variable result hold:

7
int a = 5;
int b = 2;
2.5
double result = a / b;
1
2.0

8
Review

6. What would be the result if we try to execute the following


command: System.out.println(1 + 1 + "4" + 2 + 1);

Compile
243
time error

9 2421

9
==
Logical Expressions
Comparison Operators
Comparison Operators

Operator Notation Applicable for

Equals ==
Not equals !=
Greater than > numbers, symbols,
other comparable
Greater than or equals >= types
Less than <
Less than or equals <=

11
Comparing Values: Numbers
▪ In programming we can compare values
▪ The result of the logical expressions is either true or false
int a = 5;
int b = 10;
System.out.println(a < b); // true
System.out.println(a > 0); // true
System.out.println(a > 100); // false
System.out.println(a < a); // false
System.out.println(a <= 5); // true
System.out.println(b == 2 * a); // true
12
Comparing Values: Strings
▪ Comparing text (strings) == by address in memory
String a = "Examplе";
String b = a; The memory address
System.out.println(a == b); // true is the same

Scanner scanner = new Scanner(System.in);


String a = scanner.nextLine(); Enter the
String b = scanner.nextLine(); same value
System.out.println(a == b); // false
13
Comparing Values: String.equals(…)

▪ We compare String variables using the equals method


▪ Compare texts with equals by value:

Scanner scanner = new Scanner(System.in);


String a = scanner.nextLine(); Enter the same
String b = scanner.nextLine(); value
System.out.println(a.equals(b)); // true

14
Boolean Variable
▪ boolean – a keyword that initializes a Boolean variable

▪ The only two values for it are true or false


boolean isValid = true;

▪ We can have a condition, that will return true or false


boolean isPositive = a > 0;

15
Boolean Variable – Example

int a = 5;
boolean isPositive = a > 0;
System.out.println(isPositive); // true

int a = -5;
boolean isPositive = a > 0;
System.out.println(isPositive); // false

16
Conditional Statements
Simple Conditions
Simple Conditions
▪ We often check conditions and perform actions
according to the result
Condition
(Boolean expression)
Executable code if the
if (...) { condition is true
// code to execute
}

▪ The result is true or false

18
Excellent Result – Problem
▪ Write a program that:
▪ Reads a score (number) entered by the user
▪ Checks if it is excellent
▪ Prints "Excellent" on the console, if the grade is greater or equal
to 5
▪ Example:
4 no output

5 Excellent
19
Read input

grade >= 5 No output


false

true

Print ‘Excellent’

Test your solution: https://judge.softuni.org/Contests/Compete/Index/3449#0 20


Simple Conditions: if-else
▪ If the condition is false, we can perform other actions by
using the else construction

if (...) {
// code to execute
} else { Code to execute if the
// code to execute condition is false
}

21
Block of Code (1)
▪ Curly braces { } enter a block (group of commands)
String color = "red";
if (color.equals("red")) {
System.out.println("tomato");
} else {
System.out.println("banana");
}
System.out.println("bye");
Always executes - not part of
the if / else construct
22
Block of Code (2)

▪ If we include brackets, the corresponding block is


executed
String color = "red"; The rows in the block
if (color.equals("red")) { are executed
System.out.println("tomato");
System.out.println("It is red.");
} else {
System.out.println("banana");
System.out.println("bye");
}

23
Greater Number – Problem
▪ Write a program that:
▪ Reads two integers
▪ Prints "Greater number: "
▪ Prints the greater number, on the console
▪ Example:

5 7
8 7
8 3
24
Read input

num1 > num2 Print num2


false

true

Print num1

Test your solution: https://judge.softuni.org/Contests/Compete/Index/3449#1 25


Even or Odd – Problem
▪ Write a program that:
▪ Checks whether a number is even or odd
▪ If it is even print "even"
▪ If it is odd print "odd"
▪ Example:
4 еven

7 odd
26
Even or Odd – Solution

Scanner scanner = new Scanner(System.in);


int num = Integer.parseInt(scanner.nextLine());
if (num % 2 == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}

Test your solution: https://judge.softuni.org/Contests/Compete/Index/3449#2 27


Rounding and Formatting
Rounding Numbers
▪ In programming we can round floating-point numbers
▪ Round to the next (bigger) integer:
double up = Math.ceil(23.45); // 24.0

▪ Round to previous (smaller) integer:


double down = Math.floor(45.67); // 45.0

▪ Finding the absolute value


int example1 = Math.Abs(-50); // 50
int example2 = Math.Abs(50); // 50
29
Rounding and Formatting

▪ Format to 2 decimal places:


System.out.printf("%.2f", 123.456); // 123.46

Number of symbols
after the decimal point

System.out.printf("%.2f", 123.9); // 123.90

System.out.printf("%.3f", 123.9); // 123.900

30
Debugging
Simple Debugging Operations
Debugging
▪ Debugging == the process of monitoring the
implementation of the program
▪ This allows us to detect bugs

Breakpoint

32
Debugging in IntelliJ IDEA
▪ Pressing [Shift + F9] will start the program in debug mode
▪ We can move on to the next step with [F8]
▪ [Ctrl + F8] adds a
breakpoint
▪ We can reach them
directly using [F9]

33
Sequence of Conditions
Complex Conditional Statements
Sequence of Conditions
▪ The if/else - if/else… is a series of conditions
if (...)
// code to execute
else if (...)
// code to execute
else if (...)
// code

▪ If one condition is true, the program does not proceed to


check the other conditions
35
Sequence of Conditions – Problem
▪ The program checks the first condition,
determines that it is true, and ends
int a = 7;
if (a > 4)
System.out.println("Bigger than 4");
else if (a > 5) The result will
be only "Bigger
System.out.println("Bigger than 5"); than 4"
else
System.out.println("Equal to 7");
36
Sequence of Conditions - Problem
▪ The program checks each condition, determines if
it is true, and closes
int a = 7;
if (a > 4) The result is:
"Bigger than 4"
System.out.println("Bigger than 4"); "Bigger than 5"
if (a > 5) "Equal to 7"
System.out.println("Bigger than 5");
if (a == 7)
System.out.println("Equal to 7");
37
Variable Scope
Range of Use for the Variables
Variable Scope
▪ Variable scope == the range in which it can be used
▪ Example: the variable salary exits only in the code block of
the if-construct

String currentDay = "Monday";


if (currentDay.equals("Monday")) {
double salary =
Double.parseDouble(scanner.nextLine());
} // Error
System.out.println(salary);
39
Conditional Statements
Solving Problems in Class (Lab)
Area of Figures – Problem
▪ Write a program that:
▪ Reads a geometric shape
("square", "rectangle", "circle" or "triangle")
▪ Calculates the area according to the type of figure
▪ Example: square
25
5

rectangle
7 17.5
2.5
41
Area of Figures – Solution
String shape = scanner.nextLine();
double area = 0.0;
if (shape.equals("square")) {
double side = Double.parseDouble(scanner.nextLine());
area = side * side;
} else if (shape.equals("rectangle")) {
double sideA = Double.parseDouble(scanner.nextLine());
double sideB = Double.parseDouble(scanner.nextLine());
area = sideA * sideB;
} // TODO: add more conditions
System.out.println(area);
Test your solution: https://judge.softuni.org/Contests/Compete/Index/3449#0 42
What Have We Learned Today?

▪▪ Comparison
… operators: >, <, ==, !=, …
▪▪ Conditional
… statement constructions:
if and if-else
▪ …
▪ Rounding and formatting: %.2f
▪ Sequence of conditions: if-else-if-else-

▪ Debugging: breakpoints and watches
▪ Variable scope: inside the {} block
43
Questions?

© SoftUni - https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
License

▪ This course (presentations, examples, demonstration code,


exercises, homework, videos and other assets) is copyrighted
▪ Unauthorized copying, distribution or use is illegal
▪ © Softuni Global – https://softuni.org

45
Training in SoftUni Global
▪ Softuni Global – High-Quality Education, Profession,
and Job for Software Developers
▪ softuni.org
▪ SoftUni Global@ Facebook
▪ facebook.com/softuni.global
▪ SoftUni Global Reddit
▪ r/softuni

You might also like