Java, Android software development [Part-29] :: Let's know details about if, if else, if-else-if ladder and switch (Part 2)

in #technology7 years ago

java if-else- if ladder Let's know about the statistic.
Ladder words mean stairs or ladders It's just like a ladder or a ladder. Let's first look at its syntax ...

if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
}
else if (condition3) {
// code to be executed if condition3 is true
}
...
else {
// code to be executed if all the conditions are false
}
It is a condition that, firstly, the number 1 will cure the condition if it is true, then it will execute and lie to check the condition of 2 numbers. If the execution is false then the next condition will be gone. In this way, if going through else if you see that the variable's value does not fall under any condition, then else the execution will be executed. I hope you can understand the following flowchart and program.

Flowchart

Let's go to a little programming.
public class IfElseIfExample {
public static void main (string [] args) {
int marks = 65 ;

if (marks < 50 ) {  
    System.out.println ( "fail" );  
}  
else  if (marks> = 50  && marks < 60 ) {  
    System.out.println ( "D grade" );  
}  
else  if (marks> = 60  && marks < 70 ) {  
    System.out.println ( "C grade" );  
}  
else  if (marks> = 70  && marks < 80 ) {  
    System.out.println ( "B grade" );  
}  
else  if (marks> = 80  && marks < 90 ) {  
    System.out.println ( "A grade" );  
} else  if (marks> = 90  && marks < 100 ) {  
    System.out.println ( "A + grade" );  
} else {  
    System.out.println ( "Invalid!" );  
}  

}
}
In this program, the variable number of the integer type is given 65. Then if-else -If the condition is first conditioned through the ladder status, if the value of the mark is below 50, then print will fail, if between mid-50 and 60 then the D grade, if between 60 and 70 is C grade, thus The rest. And if any of these conditions does not match the value of the mark then I will print the else statement by executing Ivalid. Since the value of mark in this program is 65, it will print c-grade.

Let's take a look at the concept of a little switch
The switch status will be lit by the lamp as it flips on the switch. This is a type of status that will work just like a switch. We can compare it with the options button. Whatever type of switch we will switch, it will check the cases inside it, whether it matches any case. If it matches, then execute it. And after every case you have to get out of the switch by pressing the brake. Otherwise, we will show you what happens next. So let's take a look at the switch's syntax

switch {expression} {
case value1:
// code to be executed;
break ; // Optional
case value2:
// code to be executed;
break ; // Optional
...

default :
code to be executed if all cases are not matched;
}
Let's see a little more flowchart

I hope you understand. Still seeing the following program can understand ...

public class SwitchExample {
public static void main (string [] args) {
int number = 20 ;
switch (number) {
case 10 : System.out.println ( "10" ); break ;
case 20 : system.out.println ( "20" ); break ;
case 30 : system.out.println ( "30" ); break ;
default : System.out.println ( "Not in 10, 20 or 30" );
}
}
}
We first took a variable named number and initialized it to 20.

This time I switched the number using the switch, which is the number of times that number matches the case. The one that matches will break through execution. If you do not match anything, then the default statement will be executed ie Not in 10, 20 or 30 .

Since the number of numbers was 20, we will print 20 here.

And if we do not use the break statement after the case then you can understand what will happen if you see the program below.

public class SwitchExample2 {
public static void main (string [] args) {
int number = 20 ;
switch (number) {
case 10 : System.out.println ( "10" );
case 20 : system.out.println ( "20" );
case 30 : system.out.println ( "30" );
default : System.out.println ( "Not in 10, 20 or 30" );
}
}
}
Since we did not use the brake, we would print 20,30 and Not in 10, 20 or 30 here. Q: Why did not you print 10? Because we've switched to 20 So since 20 matches, so the program thought that after 20, the rest of the statements were true, so all printed. If he broke, he would have gone out of the switch. But not given. That means, if you leave the punk horse, he will run. So switch the course to use the brakes.

Sort:  

This post recieved an upvote from minnowpond. If you would like to recieve upvotes from minnowpond on all your posts, simply FOLLOW @minnowpond

thank's for your post , the if condition is the more important in java and many languages we need it , I want to ask you if you know is there a each if like PHP in Java ? I have followed you .

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 61521.43
ETH 3387.82
USDT 1.00
SBD 2.49