02 Conditional Statements
02 Conditional Statements
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);
10
1
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
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
14
Boolean Variable
▪ boolean – a keyword that initializes a Boolean variable
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
}
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
true
Print ‘Excellent’
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");
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
true
Print num1
7 odd
26
Even or Odd – Solution
Number of symbols
after the decimal point
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
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
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