If statements android studio

If statements android studio

Introduction to Nested If Statements: Android Studio Crash Course (Free Tutorial)

Is your code cluttered? You know what they say: tidy code, tidy mind! With this article, you’ll learn how to simplify your code. Specifically, we’re going to compare two ways of checking the value of an integer: using several if statements vs using one if statement.

If you need an introduction to coding and want to learn for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

To follow along with this example in Android Studio, go into Project view. Then go to app > java > (topmost) com.example.zebra.demo > MainActivity.

Firstly we have to set up some variables. On a line underneath setContentView(R.layout.activity_main); , declare an integer number , and set it equal to 98 .

Below that, declare the string message , and set it equal to «This number is » . This is part of the message that we want to display on the screen.

Next create an if statement:

Within this if statement, we’ll be checking if our integer is greater than 95. In the parentheses, write number > 95 .

As well, we need to code the other part of the message that will be displayed on the screen if our number is greater than 95. Within the curly brackets of the if statement, let’s add the following:

Читайте также:  Андроиды от 4000 тысяч

Still within the if statement, let’s build another if statement. We’ll use this nested if statement to check if our integer is less than 100.

If our integer is less than 100, let’s have the message “This number is greater than 95 and lower than 100.” appear. For this to happen, add the following line within the curly brackets of your second if statement:

To have our message actually appear on the screen, we will need to use the Toast utility. Below your if statements, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete this code:

Within the parentheses of this code are the parameters. Change the quotation marks to message .

If you run the emulator and zoom in, you will see the text “This number is greater than 95 and less than 100.” appear on the screen, exactly as expected.

But there’s a faster way of doing this operation without using the nested if statement. Cut number from your nested if statement, and place it into the parentheses of the first if statement. Use the AND operator (&&) to separate the two parameters in the parentheses:

Also add and is less than 100. after greater than 95 . Now you can delete the nested if statement. As such, with only one if statement, we can check if our integer is between 95 and 100!

Run the emulator again to see the same message appear on the screen. Although most of the time you will need if statements, you were able to write this program in fewer lines of code that are easier to understand.

Читайте также:  Mobile window для андроид

Источник

Android Studio Shortcuts and Tricks to Speed ​​Up Your Productivity

In this article; I will talk about some shortcuts that we need while developing app on Android Studio and that speed up our processes by providing a practical flow for mobile developers,.
I actively use many of these shortcuts to make things a little easier and faster, and I see a gorgeous benefit. 👌🏻

There are probably those who know and use some of them. I tried to bring together the most neededs and the bests to benefit everyone. 😊

Auto Fix / Auto Edit / Auto Apply Suggestions :

Extract Method (Extract the Selected Block as a Separate Method) : 👌🏻

  • Windows : CTRL + ALT + M
  • Mac : Command + Option + M

Duplicate Current Line or Selection Completely:

Remove Current Line Completely :

Redoing (Move Forward) : ♻️ 🔁

  • Windows : CTRL + Shift + Z
  • Mac : Command + Shift + Z

When we undo too much while undoing the transactions; You can use it when we need to take a steps forward back to its original state. It is life saver. 👻

Undo as everyone knows CTRL + Z ( Mac : Command + Z)

Enclose Statements ( Include Expressions / Add Statements ) : 🚀

  • Windows : CTRL + ALT + T
  • Mac : Command + Option + T

It allows to quickly add and enclose the appropriate expression for the cursor location. If-else, try-catch, region or loop etc.

Источник

Android Studio Shortcuts and Tricks to Speed ​​Up Your Productivity

In this article; I will talk about some shortcuts that we need while developing app on Android Studio and that speed up our processes by providing a practical flow for mobile developers,.
I actively use many of these shortcuts to make things a little easier and faster, and I see a gorgeous benefit. 👌🏻

Читайте также:  Троян сканер для андроид

There are probably those who know and use some of them. I tried to bring together the most neededs and the bests to benefit everyone. 😊

Auto Fix / Auto Edit / Auto Apply Suggestions :

Extract Method (Extract the Selected Block as a Separate Method) : 👌🏻

  • Windows : CTRL + ALT + M
  • Mac : Command + Option + M

Duplicate Current Line or Selection Completely:

Remove Current Line Completely :

Redoing (Move Forward) : ♻️ 🔁

  • Windows : CTRL + Shift + Z
  • Mac : Command + Shift + Z

When we undo too much while undoing the transactions; You can use it when we need to take a steps forward back to its original state. It is life saver. 👻

Undo as everyone knows CTRL + Z ( Mac : Command + Z)

Enclose Statements ( Include Expressions / Add Statements ) : 🚀

  • Windows : CTRL + ALT + T
  • Mac : Command + Option + T

It allows to quickly add and enclose the appropriate expression for the cursor location. If-else, try-catch, region or loop etc.

Источник

Оцените статью