SlideShare a Scribd company logo
How to Replace Switch with When
in Kotlin?
As a beginner, you might look for how the Kotlin switch
statement works. If your background is working in C, Java, etc.
then switching to Kotlin works best. There is no switch statement
available in Kotlin; instead, it replaces the switch with the when
expression.
The switch is used as a two-state user interface element and
work as a toggle between ON and OFF like button. One can add a
switch into our application layout by using its switch object. By
default, the switch is off. Moving on with Kotlin, then When is
replace the Switch operator. A specific block of code needs to
execute when the condition fulfilled. One can take help from
Kotlin App Developers and develop android application.
In Kotlin switch case with string, when is mainly used in two
ways:
● When as a statement
● When as an expression
When’ is an expression compared with other branches. If it used
as a statement, the branch values compared sequentially with
the argument and execute the corresponding branch where the
condition matches. Here in this blog, we’ll look at how switch
replaces the when.
Let’s start with understanding Switch in Kotlin Android and then
we’ll check how the when keyword makes our code easier and
more understandable.
Switch
Suppose you have used a conditional statement where the
condition used in the conditional statement applies to a similar
type of data. Then instead of that use vast code for the
conditional statement. Use the switch to avoid using too many
conditional statements in our code. Let’s study with the help of
an example.
Let’s consider one example where we have to return emoji based
on input spring.
But if we rewrite the code using the When statement in Kotlin,
then it provides better readability than if/else. In the above code,
if/else statement replaced with the when statement.
Replace Switch Case Kotlin with When
Similar to if/else, the switch is one of the ancient ways to deal
with conditional execution. Whereas in Kotlin, we can replace
switch case and if/else with when similarly, we implement the
conditional execution. Now the switch can replace with the when
statement also to if/else. Make the code more readable,
consistent and adapt quickly with new developers.
Return values with When Block
Till now, you have seen the ability of when the block in Kotlin
switch case with spring replaces conditional statements. But well,
it can do more than this. Let’s say, it can return the values from
the conditional blocks after you execute the necessary steps.
Arbitrary Condition with When
We can also use more than one checks to execute a block when
the operator
There are several causes, but only one case that matches up will
work as a respective block of code that executed. The comma
added in the above example act as an OR operator.
Using When without an Argument
We’ve used ‘when’ with an argument. But when the real
potential of when is we use when without an argument.
Understand how it looks.
Later, it acts as an if/else block with class-level variable
conditions.
Now, Compare the instance with When
We can check whether the given operator is an instance of a
particular type at runtime by using the ‘is’ operator. The operator
‘is’ works similar to the instanceof operator in Java. Now use the
‘is’ and ‘!is’ operator inside when block. Get to know how Kotlin
switch case with string works with an example.
Main points about Kotlin when
Java uses Switch conditional statements that operated with
primitive data types, string class, and Kotlin switch enums.
instead, Kotlin allows when to be used with any user-defined
types. It makes it developer-friendly.
Unlike Java, Kotlin’s when can use with dynamic value like return
type of functions.
Also, check whether the given argument in collections uses the
‘in’ operator, which acts as contains.
The else is mandatory if you’re using when as an expression.
Example of a wide range of utility functions
In Kotlin, few simple utility functions are used for a simple task.
Let’s say if you want to get a minimum or maximum value in the
range. Similarly, the average value of the sum of range, total
count or value of the range, etc.
Check below Kotlin Functions
● count()
● average()
● min()
● max()
● sum()
Summary
In this blog, we have seen how powerful and useful the kotlin
when the expression is. If you’re using Kotlin, you find yourself
using the when expression all the time. Connect with us if you’re
seeking to Hire Kotlin App Developers and switch to Kotlin when.
Global Network
USA
4811 N Harding Ave,
Chicago IL 60625
P: +1 (903) 200-8801
CANADA
1445 Rennie St Oshawa,
Ontario, L1K 0N9,CANADA
P: (416) 272-0981
INDIA
609/610,City Center Science
City Road
Ahmedabad - 380060
P: +919586777575
UK
6 Hastings Avenue
Ilford , IG6 1DZ
P: +44 (798) 560 0352
Web & Mobile App Development Company
https://www.concettolabs.com

More Related Content

What's hot

Type Safety in an Unsafe World With Kotlin
Type Safety in an Unsafe World With KotlinType Safety in an Unsafe World With Kotlin
Type Safety in an Unsafe World With Kotlin
Postman
 
Python Lambda Function
Python Lambda FunctionPython Lambda Function
Python Lambda Function
Md Soyaib
 
Rockstar Night - Spring, migrating to functional configuration
Rockstar Night - Spring, migrating to functional configurationRockstar Night - Spring, migrating to functional configuration
Rockstar Night - Spring, migrating to functional configuration
Nicolas Fränkel
 
Sample ui string queries sheet1
Sample ui string queries sheet1Sample ui string queries sheet1
Sample ui string queries sheet1David Sommer
 
Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017
Peter Thomas
 
Coding in kotlin
Coding in kotlinCoding in kotlin
Coding in kotlin
Debmalya Jash
 
React with Ref
React with RefReact with Ref
React with Ref
Jack Forbes
 
JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel StreamTengwen Wang
 
Java 8 parallel stream
Java 8 parallel streamJava 8 parallel stream
Java 8 parallel stream
Yung Chieh Tsai
 
Angular 2
Angular 2Angular 2
Angular 2
Pramod Raghav
 
Karate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter ThomasKarate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter Thomas
intuit_india
 

What's hot (12)

Type Safety in an Unsafe World With Kotlin
Type Safety in an Unsafe World With KotlinType Safety in an Unsafe World With Kotlin
Type Safety in an Unsafe World With Kotlin
 
Python Lambda Function
Python Lambda FunctionPython Lambda Function
Python Lambda Function
 
Rockstar Night - Spring, migrating to functional configuration
Rockstar Night - Spring, migrating to functional configurationRockstar Night - Spring, migrating to functional configuration
Rockstar Night - Spring, migrating to functional configuration
 
Sample ui string queries sheet1
Sample ui string queries sheet1Sample ui string queries sheet1
Sample ui string queries sheet1
 
Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017Karate - MoT Dallas 26-Oct-2017
Karate - MoT Dallas 26-Oct-2017
 
Coding in kotlin
Coding in kotlinCoding in kotlin
Coding in kotlin
 
React with Ref
React with RefReact with Ref
React with Ref
 
JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel Stream
 
WSO2 WSF/Jython
WSO2 WSF/JythonWSO2 WSF/Jython
WSO2 WSF/Jython
 
Java 8 parallel stream
Java 8 parallel streamJava 8 parallel stream
Java 8 parallel stream
 
Angular 2
Angular 2Angular 2
Angular 2
 
Karate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter ThomasKarate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter Thomas
 

Similar to How to replace switch with when in kotlin

Basics of kotlin ASJ
Basics of kotlin ASJBasics of kotlin ASJ
Basics of kotlin ASJ
DSCBVRITH
 
Day 2 Compose Camp.pptx
Day 2 Compose Camp.pptxDay 2 Compose Camp.pptx
Day 2 Compose Camp.pptx
ShayantaniKar
 
C#/.NET Little Pitfalls
C#/.NET Little PitfallsC#/.NET Little Pitfalls
C#/.NET Little Pitfalls
BlackRabbitCoder
 
9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin
Zoran Stanimirovic
 
Kotlin
KotlinKotlin
Kotlin
Ravi Pawar
 
Kotlin in action
Kotlin in actionKotlin in action
Kotlin in action
Mahdi Hasanzadeh
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1
Techglyphs
 
From Java to Kotlin
From Java to KotlinFrom Java to Kotlin
From Java to Kotlin
João Ferrão
 
Kotlin generics
Kotlin genericsKotlin generics
Kotlin generics
LinusMuema
 
Type conversions
Type conversionsType conversions
Type conversions
sanya6900
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
Geekyants
 
Something for Nothing
Something for NothingSomething for Nothing
Something for Nothing
Kevlin Henney
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
Syed Awais Mazhar Bukhari
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Adam Magaña
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
Aditya Kumar Rajan
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and loopingaprilyyy
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
Prasanna Kumar SM
 
Loop structures
Loop structuresLoop structures
Loop structures
tazeem sana
 

Similar to How to replace switch with when in kotlin (20)

Basics of kotlin ASJ
Basics of kotlin ASJBasics of kotlin ASJ
Basics of kotlin ASJ
 
Day 2 Compose Camp.pptx
Day 2 Compose Camp.pptxDay 2 Compose Camp.pptx
Day 2 Compose Camp.pptx
 
C#/.NET Little Pitfalls
C#/.NET Little PitfallsC#/.NET Little Pitfalls
C#/.NET Little Pitfalls
 
9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin9054799 dzone-refcard267-kotlin
9054799 dzone-refcard267-kotlin
 
Kotlin
KotlinKotlin
Kotlin
 
Kotlin in action
Kotlin in actionKotlin in action
Kotlin in action
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1
 
From Java to Kotlin
From Java to KotlinFrom Java to Kotlin
From Java to Kotlin
 
Kotlin generics
Kotlin genericsKotlin generics
Kotlin generics
 
Type conversions
Type conversionsType conversions
Type conversions
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
 
Something for Nothing
Something for NothingSomething for Nothing
Something for Nothing
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android Development
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Final requirement
Final requirementFinal requirement
Final requirement
 

More from Concetto Labs

Which are The Top 6 Microsoft PowerApps Use Cases with Examples_.pdf
Which are The Top 6 Microsoft PowerApps Use Cases with Examples_.pdfWhich are The Top 6 Microsoft PowerApps Use Cases with Examples_.pdf
Which are The Top 6 Microsoft PowerApps Use Cases with Examples_.pdf
Concetto Labs
 
Best Outsourcing Quality Assurance Service Tips for Boosting your Business.pptx
Best Outsourcing Quality Assurance Service Tips for Boosting your Business.pptxBest Outsourcing Quality Assurance Service Tips for Boosting your Business.pptx
Best Outsourcing Quality Assurance Service Tips for Boosting your Business.pptx
Concetto Labs
 
Guide on Developing Mood Tracker App.pptx
Guide on Developing Mood Tracker App.pptxGuide on Developing Mood Tracker App.pptx
Guide on Developing Mood Tracker App.pptx
Concetto Labs
 
Which are the Skills_Tools required for Becoming a Mobile QA Engineer_.pptx
Which are the Skills_Tools required for Becoming a Mobile QA Engineer_.pptxWhich are the Skills_Tools required for Becoming a Mobile QA Engineer_.pptx
Which are the Skills_Tools required for Becoming a Mobile QA Engineer_.pptx
Concetto Labs
 
Complete Guide_ Mobile App Navigation Designs.pptx
Complete Guide_ Mobile App Navigation Designs.pptxComplete Guide_ Mobile App Navigation Designs.pptx
Complete Guide_ Mobile App Navigation Designs.pptx
Concetto Labs
 
The Top 15 Mobile App Development Trends to Watch for in 2022.pptx
The Top 15 Mobile App Development Trends to Watch for in 2022.pptxThe Top 15 Mobile App Development Trends to Watch for in 2022.pptx
The Top 15 Mobile App Development Trends to Watch for in 2022.pptx
Concetto Labs
 
How Does Metaverse Affect E-Commerce Business_.pptx
How Does Metaverse Affect E-Commerce Business_.pptxHow Does Metaverse Affect E-Commerce Business_.pptx
How Does Metaverse Affect E-Commerce Business_.pptx
Concetto Labs
 
Smart Building Technology_ Concept, Features, and Application.pdf
Smart Building Technology_ Concept, Features, and Application.pdfSmart Building Technology_ Concept, Features, and Application.pdf
Smart Building Technology_ Concept, Features, and Application.pdf
Concetto Labs
 
How to Develop Your Own Music Streaming App with Unique Features_.pdf
How to Develop Your Own Music Streaming App with Unique Features_.pdfHow to Develop Your Own Music Streaming App with Unique Features_.pdf
How to Develop Your Own Music Streaming App with Unique Features_.pdf
Concetto Labs
 
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
Concetto Labs
 
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
Concetto Labs
 
Developing a Healthcare Mobile App_ Types, Examples, and Features.pdf
Developing a Healthcare Mobile App_ Types, Examples, and Features.pdfDeveloping a Healthcare Mobile App_ Types, Examples, and Features.pdf
Developing a Healthcare Mobile App_ Types, Examples, and Features.pdf
Concetto Labs
 
Laravel 9_ Everything You Need To Know About The Latest Major Release.pdf
Laravel 9_ Everything You Need To Know About The Latest Major Release.pdfLaravel 9_ Everything You Need To Know About The Latest Major Release.pdf
Laravel 9_ Everything You Need To Know About The Latest Major Release.pdf
Concetto Labs
 
The Complete Guide to API Development in 2022.pdf
The Complete Guide to API Development in 2022.pdfThe Complete Guide to API Development in 2022.pdf
The Complete Guide to API Development in 2022.pdf
Concetto Labs
 
What Is Mobile Device Testing and Best Practices For Mobile Testing_.pdf
What Is Mobile Device Testing and Best Practices For Mobile Testing_.pdfWhat Is Mobile Device Testing and Best Practices For Mobile Testing_.pdf
What Is Mobile Device Testing and Best Practices For Mobile Testing_.pdf
Concetto Labs
 
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pdf
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pdf9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pdf
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pdf
Concetto Labs
 
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pptx
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pptx9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pptx
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pptx
Concetto Labs
 
How To Develop A Logistics & Transportation Mobile App_.pptx
How To Develop A Logistics & Transportation Mobile App_.pptxHow To Develop A Logistics & Transportation Mobile App_.pptx
How To Develop A Logistics & Transportation Mobile App_.pptx
Concetto Labs
 
13 Android New Features You Must Try Out Today.pptx
13 Android New Features You Must Try Out Today.pptx13 Android New Features You Must Try Out Today.pptx
13 Android New Features You Must Try Out Today.pptx
Concetto Labs
 
Get Started with ReactJS 18 Development Services_ New Features and Updates.pptx
Get Started with ReactJS 18 Development Services_ New Features and Updates.pptxGet Started with ReactJS 18 Development Services_ New Features and Updates.pptx
Get Started with ReactJS 18 Development Services_ New Features and Updates.pptx
Concetto Labs
 

More from Concetto Labs (20)

Which are The Top 6 Microsoft PowerApps Use Cases with Examples_.pdf
Which are The Top 6 Microsoft PowerApps Use Cases with Examples_.pdfWhich are The Top 6 Microsoft PowerApps Use Cases with Examples_.pdf
Which are The Top 6 Microsoft PowerApps Use Cases with Examples_.pdf
 
Best Outsourcing Quality Assurance Service Tips for Boosting your Business.pptx
Best Outsourcing Quality Assurance Service Tips for Boosting your Business.pptxBest Outsourcing Quality Assurance Service Tips for Boosting your Business.pptx
Best Outsourcing Quality Assurance Service Tips for Boosting your Business.pptx
 
Guide on Developing Mood Tracker App.pptx
Guide on Developing Mood Tracker App.pptxGuide on Developing Mood Tracker App.pptx
Guide on Developing Mood Tracker App.pptx
 
Which are the Skills_Tools required for Becoming a Mobile QA Engineer_.pptx
Which are the Skills_Tools required for Becoming a Mobile QA Engineer_.pptxWhich are the Skills_Tools required for Becoming a Mobile QA Engineer_.pptx
Which are the Skills_Tools required for Becoming a Mobile QA Engineer_.pptx
 
Complete Guide_ Mobile App Navigation Designs.pptx
Complete Guide_ Mobile App Navigation Designs.pptxComplete Guide_ Mobile App Navigation Designs.pptx
Complete Guide_ Mobile App Navigation Designs.pptx
 
The Top 15 Mobile App Development Trends to Watch for in 2022.pptx
The Top 15 Mobile App Development Trends to Watch for in 2022.pptxThe Top 15 Mobile App Development Trends to Watch for in 2022.pptx
The Top 15 Mobile App Development Trends to Watch for in 2022.pptx
 
How Does Metaverse Affect E-Commerce Business_.pptx
How Does Metaverse Affect E-Commerce Business_.pptxHow Does Metaverse Affect E-Commerce Business_.pptx
How Does Metaverse Affect E-Commerce Business_.pptx
 
Smart Building Technology_ Concept, Features, and Application.pdf
Smart Building Technology_ Concept, Features, and Application.pdfSmart Building Technology_ Concept, Features, and Application.pdf
Smart Building Technology_ Concept, Features, and Application.pdf
 
How to Develop Your Own Music Streaming App with Unique Features_.pdf
How to Develop Your Own Music Streaming App with Unique Features_.pdfHow to Develop Your Own Music Streaming App with Unique Features_.pdf
How to Develop Your Own Music Streaming App with Unique Features_.pdf
 
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
 
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
What are the Cost and Key Features of On-Demand Car Wash App Development Serv...
 
Developing a Healthcare Mobile App_ Types, Examples, and Features.pdf
Developing a Healthcare Mobile App_ Types, Examples, and Features.pdfDeveloping a Healthcare Mobile App_ Types, Examples, and Features.pdf
Developing a Healthcare Mobile App_ Types, Examples, and Features.pdf
 
Laravel 9_ Everything You Need To Know About The Latest Major Release.pdf
Laravel 9_ Everything You Need To Know About The Latest Major Release.pdfLaravel 9_ Everything You Need To Know About The Latest Major Release.pdf
Laravel 9_ Everything You Need To Know About The Latest Major Release.pdf
 
The Complete Guide to API Development in 2022.pdf
The Complete Guide to API Development in 2022.pdfThe Complete Guide to API Development in 2022.pdf
The Complete Guide to API Development in 2022.pdf
 
What Is Mobile Device Testing and Best Practices For Mobile Testing_.pdf
What Is Mobile Device Testing and Best Practices For Mobile Testing_.pdfWhat Is Mobile Device Testing and Best Practices For Mobile Testing_.pdf
What Is Mobile Device Testing and Best Practices For Mobile Testing_.pdf
 
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pdf
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pdf9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pdf
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pdf
 
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pptx
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pptx9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pptx
9 Best Design Patterns in iOS to Develop Powerful iPhone Apps.pptx
 
How To Develop A Logistics & Transportation Mobile App_.pptx
How To Develop A Logistics & Transportation Mobile App_.pptxHow To Develop A Logistics & Transportation Mobile App_.pptx
How To Develop A Logistics & Transportation Mobile App_.pptx
 
13 Android New Features You Must Try Out Today.pptx
13 Android New Features You Must Try Out Today.pptx13 Android New Features You Must Try Out Today.pptx
13 Android New Features You Must Try Out Today.pptx
 
Get Started with ReactJS 18 Development Services_ New Features and Updates.pptx
Get Started with ReactJS 18 Development Services_ New Features and Updates.pptxGet Started with ReactJS 18 Development Services_ New Features and Updates.pptx
Get Started with ReactJS 18 Development Services_ New Features and Updates.pptx
 

Recently uploaded

Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 

Recently uploaded (20)

Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 

How to replace switch with when in kotlin

  • 1. How to Replace Switch with When in Kotlin?
  • 2. As a beginner, you might look for how the Kotlin switch statement works. If your background is working in C, Java, etc. then switching to Kotlin works best. There is no switch statement available in Kotlin; instead, it replaces the switch with the when expression. The switch is used as a two-state user interface element and work as a toggle between ON and OFF like button. One can add a switch into our application layout by using its switch object. By default, the switch is off. Moving on with Kotlin, then When is replace the Switch operator. A specific block of code needs to execute when the condition fulfilled. One can take help from Kotlin App Developers and develop android application.
  • 3. In Kotlin switch case with string, when is mainly used in two ways: ● When as a statement ● When as an expression When’ is an expression compared with other branches. If it used as a statement, the branch values compared sequentially with the argument and execute the corresponding branch where the condition matches. Here in this blog, we’ll look at how switch replaces the when. Let’s start with understanding Switch in Kotlin Android and then we’ll check how the when keyword makes our code easier and more understandable.
  • 4. Switch Suppose you have used a conditional statement where the condition used in the conditional statement applies to a similar type of data. Then instead of that use vast code for the conditional statement. Use the switch to avoid using too many conditional statements in our code. Let’s study with the help of an example. Let’s consider one example where we have to return emoji based on input spring.
  • 5. But if we rewrite the code using the When statement in Kotlin, then it provides better readability than if/else. In the above code, if/else statement replaced with the when statement.
  • 6.
  • 7. Replace Switch Case Kotlin with When
  • 8. Similar to if/else, the switch is one of the ancient ways to deal with conditional execution. Whereas in Kotlin, we can replace switch case and if/else with when similarly, we implement the conditional execution. Now the switch can replace with the when statement also to if/else. Make the code more readable, consistent and adapt quickly with new developers.
  • 9. Return values with When Block Till now, you have seen the ability of when the block in Kotlin switch case with spring replaces conditional statements. But well, it can do more than this. Let’s say, it can return the values from the conditional blocks after you execute the necessary steps.
  • 10. Arbitrary Condition with When We can also use more than one checks to execute a block when the operator There are several causes, but only one case that matches up will work as a respective block of code that executed. The comma added in the above example act as an OR operator.
  • 11. Using When without an Argument We’ve used ‘when’ with an argument. But when the real potential of when is we use when without an argument. Understand how it looks. Later, it acts as an if/else block with class-level variable conditions.
  • 12. Now, Compare the instance with When We can check whether the given operator is an instance of a particular type at runtime by using the ‘is’ operator. The operator ‘is’ works similar to the instanceof operator in Java. Now use the ‘is’ and ‘!is’ operator inside when block. Get to know how Kotlin switch case with string works with an example.
  • 13. Main points about Kotlin when Java uses Switch conditional statements that operated with primitive data types, string class, and Kotlin switch enums. instead, Kotlin allows when to be used with any user-defined types. It makes it developer-friendly. Unlike Java, Kotlin’s when can use with dynamic value like return type of functions. Also, check whether the given argument in collections uses the ‘in’ operator, which acts as contains. The else is mandatory if you’re using when as an expression.
  • 14. Example of a wide range of utility functions In Kotlin, few simple utility functions are used for a simple task. Let’s say if you want to get a minimum or maximum value in the range. Similarly, the average value of the sum of range, total count or value of the range, etc. Check below Kotlin Functions ● count() ● average() ● min() ● max() ● sum()
  • 15. Summary In this blog, we have seen how powerful and useful the kotlin when the expression is. If you’re using Kotlin, you find yourself using the when expression all the time. Connect with us if you’re seeking to Hire Kotlin App Developers and switch to Kotlin when.
  • 16. Global Network USA 4811 N Harding Ave, Chicago IL 60625 P: +1 (903) 200-8801 CANADA 1445 Rennie St Oshawa, Ontario, L1K 0N9,CANADA P: (416) 272-0981 INDIA 609/610,City Center Science City Road Ahmedabad - 380060 P: +919586777575 UK 6 Hastings Avenue Ilford , IG6 1DZ P: +44 (798) 560 0352
  • 17. Web & Mobile App Development Company https://www.concettolabs.com