Learn Pain Less

HomeOur TeamContact
Java
Why we use Conditional code and its different types
Pawneshwer Gupta
Pawneshwer Gupta
February 14, 2015
1 min
Why we use Conditional code and its different types

Conditional code will help our app to prevent from sudden crash. By using conditions, we can fully customize our program. We can perform many task from same code using conditions.

How conditions help our app to prevent from crash???

Our app mostly crashes by giving Exceptions. And we can double check that exception before execution of our app to prevent from crash. For example, if we are performing action on any variable’s value, but that variable is null then we will get exception “Null Pointer Exception”. To prevent from that null Pointer exception, we can perform action within conditions. We will first check if that variable is not null then perform our action, else skip exception.

conditional statement examples:

class Test{
String test;
void split(){
System.out.println(test.subString(0,5));
}
}

If we run this code then it will produce ”NullPointer Exception”. Because string test is null. And there is an another exception that is ”IndexOutOfBound Exception”. Because length of string test is 0 and we are splitting that string from 0 to 5 and 5th index is not available.

So we can write conditions as below:

class Test{
String test;
void split()
{
if(test != null) //this code will check test is null or not.
{
if(test.lenght() > 5) // this will check test greater than 5 or not
{
System.out.println(test.subString(0,5));
}
else
{
System.out.println("lenght of string is less than 5");
}
}
else
{
System.out.println("String is null");
}
}
}

Types of flow control statement

  1. If statement
  2. if-else statement
  3. Switch statement

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

startcontidions

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

Working with Files, Folders java
Working with Files, Folders java
March 25, 2015
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media