switch vs. if-else in Java

preview_player
Показать описание
Java Programming: switch vs if-else in Java Programming
Topics Discussed:
1. switch vs if-else.
2. The break keyword.

Music:
Axol x Alex Skrindo - You [NCS Release]

#JavaByNeso #JavaProgramming #switchStatement #breakKeyword
Рекомендации по теме
Комментарии
Автор

So easily and at the same time concisely explained! Thank you

Olesia-sc
Автор

import java.util.Scanner;

public class Switchassignment {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter your digit: ");
int i1= input.nextInt();
int i2 = i1%2;
switch(i2) {
case (1):
System.out.println("Your digit is Odd = " + i1);
break;
case (0):
System.out.println("your digit is evem = " + i1);
break;
default:
System.out.println("you type wrong digit");

}
}
}



I don't have any idea about coding but because of NESO ACADEMY i fell in love with coding thank you sir💖

girishsuraskar
Автор

Very clear and well put together. Thank you!

thekadend
Автор

Oh man if only yanderedev knew about this video. He'd lose his job. Thanks, I was looking for a way to not have to nest if statements :)

bubbly
Автор

I continuously watching your videos from the year 2017...
It's too great...
I learned a lot from your videos..
I have a question to ask you...
Till now you uploaded only for C Programming concepts
When will you upload data structures concepts?
Plz reply me if you see this
Thank you neso academy

manikumartharmaraj
Автор

I use the condition (ternary) operator because switch cant test values on every case. Adding a switch for every value to test floods the code.
Use this instead:
value1 <test> value2 ? ( ifTrue ) : ( ifFalse ) ;
It seems faster than if-else.
Of course for options and lists and stuff i still use switch.
But the video is useful too!

I do recommend to use if-else for calling "break;" in "for" and "while" loops.

mystcat
Автор

Very good explanation but as per I know switch case bit faster and directly it will jump to the case it will not execute every case as you said even if you not put break statement until and unless case satisfies every line verifies in if else

anjanaproperties
Автор

great vid, easy to follow and to-the-point.

tryrexm
Автор

import java.util.Scanner;

class Check {
public static void main(String[] args) {

System.out.print("Enter an integer: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

switch (n % 2) {
case 0 : System.out.println(n + " is an even number.");
break;
default : System.out.println(n + " is an odd number.");
break;
}
}
}

suswithcherry
Автор

But why use switch case is I can use 2 pages of if else statements in my game

realdragon
Автор

Hi want to ask that is it switch case is more effective compare to if else ?

lohchiyung
Автор

Habibi I can sense your accent shurka for the video :)

OK-rieu