EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Scala if else

By Shalu SharmaShalu Sharma

Home » Software Development » Software Development Tutorials » Scala Tutorial » Scala if else

Scala if else

Introduction to  Scala if else

If else statements are the block of code which decides that the specific piece of code should be executed or not based on the value provided. It is similar like java and other languages available. We can say it is a decision maker for our logic and the part of code will only be executed if the condition is satisfied. So if and else are the decision making statement n object oriented programming which are very important when it comes to apply our logic in practice scenario.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

if(your condition here){
// here the piece of code you want o execute
}else{
// code to be executed.
}

If the condition you have provided is computed as TRUE than the code will execute if not then it will not enter into the block.

In the example above if block will only be executed if the result compute out to be TRUE. In programming language this IF statement s followed by the ELSE statement as well. If you want to execute some code if the result is not TRUE then this else lock will be executed. This is not mandatory to provide else statement with IF block, it’s optional or we can say based on the requirement or logic we want to implement.

Flowchart of Scala if else

Below you can find the flowchart for IF and ELSE statement. First it will enter to code and check the condition provided into the IF statement. In programming language IF and ELSE statement depends upon the Boolean value i.e. TRUE and FALSE.

So after entering it will calculate the if condition result if it results out to be trues then the code inside IF block will be come into consideration. If it calculates False then the code other code will be executed. But if we have provided else statement, then code inside it will be executed.

Popular Course in this category
Scala Programming Training (3 Courses,1Project)3 Online Courses | 9+ Hours | Verifiable Certificate of Completion | Lifetime Validity | 1 Project
4.5 (5,543 ratings)
Course Price

View Course

Related Courses
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)All in One Software Development Bundle (600+ Courses, 50+ projects)

Flowchart of Scala if else

In Scala we have various kind of IF statement;

  • IF-ELSE Statement
  • Nested IF-ELSE
  • IF-ELSE-IF-ELSE

But for now, we will focus more on the IF ELSE Statement. In order to use IF ELSE we need to understand the IF statement first because the ELSE statement has to contain IF, but IF can execute independently.

1. IF-ELSE Statement: In this case we will have If statement followed by ELSE statement. If the condition provided is true then if the block will be executed, if false then Else block will be executed. Else will always execute in the case of false or when the condition is not satisfied.

syntax:

if(){
// if logic
}else{
// else logic
}

Example for beginners:

Code:

object Main extends App{
// Your code here!
vari = 10
if(i == 10){
println("only executed when the condition is TRUE :: Equal")
}else{
println("not equal")
}
}

2. Nested IF-ElSE: In this we have if-else statement inside if or an else statement depends upon the logic. We are doing nesting of statement inside an already present statement.

Syntax:

if(condition){
// if true (nesting)
if(){
}else{
}
}else{
// if false (nesting)
if(){
}else{
}
}

3. IF-ELSE-IF-ELSE: In this case we provide multiple if conditions. If any of the conditions is true then the rest condition will be skipped and the program will execute normally.

Syntax:

if(condition_1){
}else if(condition_2){
}else if(condition_3){
}else{
}

How Does if else Statement Works in Scala?

In Scala also these are the decision-making statements. Which allows us to control our flow of the program by the condition provided? They are depending upon the Boolean values to execute code. If true then a particular block of code will be executed. In this else block is not always mandatory it just depends upon the business requirement. Keep in mind that we need to implement IF statement in order to work with IF ELSE. This is mandate.

They are responsible to control the flow of the program.

Examples to Implement of Scala if else

Below are the examples of Scala if else:

Example #1 – IF-ELSE Statement

This is a simple IF ELSE statement Based on the TRUE and FALSE value.

Code:

object Main extends App{
//your code
println("Example to show if else statement flow.")
println("------------------------------------------------------")
var x = 90
var y = 100
if(x > y){
println("If block executed.")
println("x is greater." + "x is  >> " + x + " , y is >> " + y  )
}else{
println("else block executed.")
println("y is greater. " + "x is  >> " + x + " , y is >> " + y  )
}
}

Output:

Scala if else Example 1

Example #2 – Nested IF Else Statement

It contains the nesting of statements.

Code:

object Main extends App{
println("Example to show nested if else statement flow.")
println("------------------------------------------------------")
var x = 90
var y = 100
if(x > y){
println("If block executed.")
var z = 150
println("nested if statment!")
if(x > z ){
println("------------------------------------------------------")
println("x is greater than z.")
println("x is >> "+ x +" ,z is >>" +z)
println("------------------------------------------------------")
}else{
println("------------------------------------------------------")
println("x is not greater than z.")
println("x is >> "+ x +" ,z is >>" +z)
println("------------------------------------------------------")
}
}else{
println("else block executed.")
var z = 150
if(y > z ){
println("------------------------------------------------------")
println("y is greater than z.")
println("y is >> "+ y +" ,z is >>" +z)
println("------------------------------------------------------")
}else{
println("------------------------------------------------------")
println("y is not greater than z.")
println("y is >> "+ y +" ,z is >>" +z)
println("------------------------------------------------------")
}
}
}

Output:

Nested IF Else Statement Example 2

Scala if else Example 3

Example #3 – IF ELSE IF Ladder

It contains the ladder of IF-ELSE statements multiple times.

Code:

object Main extends App{
println("Example to show if else if ladder statement flow.")
println("------------------------------------------------------")
var x = 90
var y = 90
if(x > y){
println("X is greater than Y.")
}else if(x == y ) {
println("X is equal to Y.")
}else{
println("Y is greater than X.")
}
}

Output:

IF ELSE IF Ladder Example 4

Conclusion

IF ELSE are decision making control statement and they work similarly to any other language available only the syntax can vary from one to another. Their result depends upon the True and False value in order to make a decision. They provide us a way to control our program flow and help building logic.

Recommended Articles

This is a guide to Scala if else. Here we discuss a brief overview of Scala if else and its different examples along with code Implementation. You can also go through our other suggested articles to learn more –

  1. Kotlin vs Scala
  2. Scala Data Types
  3. Scala for Loop
  4. Scala High Order Functions

Scala Programming Training (3 Courses)

3 Online Courses

9+ Hours

Verifiable Certificate of Completion

Lifetime Validity

1 Project

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Scala Tutorial
  • Control Flow
    • Scala if else
    • Break in Scala
    • Scala for Loop
    • Scala foreach
    • Scala While Loops
  • Basics
    • What is Scala?
    • Uses of Scala
    • Scala Versions
    • Scala Data Types
    • Scala Companion Object
    • Scala Operators
    • Scala: The Functional Approach
    • Scala Object
    • Scala Implicit
    • Scala IDE
  • Advanced
    • Constructors in Scala
    • Scala Class Constructor
    • Scala Abstract Class
    • Scala String
    • Scala Collections
    • Scala High Order Functions
    • Scala Date Function
    • Scala Stream
    • Scala Class
    • Scala Function
    • Scala Closure
    • Scala Range
    • Scala Either
    • Scala Random
    • Scala Tuples
    • Scala Partial Function
    • Scala Split
    • Scala Finds
    • Scala List
    • Scala Trait
    • Scala Try Catch
    • Scala Regex
    • Scala Vector
    • Scala Seq
    • Scala Byte
    • Scala ListBuffer
    • Scala ArrayBuffer
    • Scala flatMap
    • Scala HashMap
    • Scala Map Function
    • Scala SortBy
    • Scala Some
    • Scala Generic
    • Scala getOrElse
    • Scala REPL
    • Scala Logging
    • Scala Lambda
    • Scala Queue
    • Scala Set
    • Scala Singleton
    • Scala groupBy
    • Scala reduce
    • Scala filter
    • Scala Option
    • Scala Native
    • Scala DataFrame
    • Scala Anonymous Function
    • Scala Read File
    • Scala Write to File
    • Scala Pattern Matching
    • Scala ClassTag
    • Scala Synchronized
    • Scala zipwithindex
    • Scala Interview Questions

Related Courses

Scala Programming Training Course

Programming Language Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Scala Programming Training (3 Courses) Learn More