#3.1 Java Tutorial | If Else | Selection Statement

Описание к видео #3.1 Java Tutorial | If Else | Selection Statement

In this lecture we are discussing:
1)Conditional Statements
2)Use of if (check single statements)
3)Use of if and else (for checking complementary condition)
4)Use of if , else if and else (more than two condition)
5)Use of curly braces is required or not

#1
Conditional Statements: If we say about conditional statements it means we have checking
condition and basis of this condition decide whether statement execute next line of code or not.

e.g check n=5 is even or odd
for checking such kind of condition we required conditional statements

we have three conditional statements:
a) if, else and else if
b)ternary operator
c)switch

Note: in this section we are only discussing about if and else statements.

#2
Use of if (Single condition)

e.g
check n is odd
for that we use if() statement
if(n%2!=0)
System.out.println("Odd");

e.g
check n is equal to zero
if(n==0)
System.out.println("Equal to zero");

#3
Use of if and else

e.g
if you want to check complimentary condition
check n is even or odd
if(n%2==0)
System.out.println("Even");
else
System.out.println("Odd");

e.g
check n is equal to zero or not
if(n!=0)
System.out.println("Not a zero");
else
System.out.println("zero");

#4
use if, else if and else
if you want to check multiple condition

e.g
check whether number is zero , less than zero or greater than zero
if(n==0)
System.out.println("Equal to zero");
else if(n grater-then 0)
System.out.println("Greater than zero");
else
System.out.println("less than zero");

#5
Use of curly braces required or not

case 1:
Suppose after if, else if or else we have single statement then
we do not require braces but you can use
e.g
if(n%2==0)
System.out.println("Even"); //correct way

if(n%2==0)
{
System.out.println("Even); //correct way
}

case 2:
if we have more than one statement after if , else if or else
we need to put curly braces
Correct way:

if(n%2==0)
{int a=5;
int b=6;
System.out.println(a+b);
System.out.println(a-b);
}

Incorrect way:
if(n%2==0)
int a=5;
int b=6;
System.out.println(a+b);
System.out.println(a-b);

Note: in such cases either you get error or you get unexpected answer

Recommended:
Always use curly braces either you have one statement or multiple statements;





Instagram :   / navinreddyofficial  
Linkedin :   / navinreddy20  
Discord :   / discord  

More Learning :

Java - https://bit.ly/3xleOA2
Python :- https://bit.ly/3H0DYHx
Django :- https://bit.ly/3awMaD8
Spring Boot :- https://bit.ly/3aucCgB
Spring Framework :- https://bit.ly/3GRfxwe

Servlet & JSP :- https://bit.ly/3mh5CGz
Hibernate Tutorial :- https://bit.ly/3NWAKah
Rest API | Web Service Tutorial :- https://bit.ly/38RJCiy

Git :- https://bit.ly/3NUHB3V
JavaScript :- https://bit.ly/3mkcFys
Kotlin :- https://bit.ly/3GR2DOG


Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
http://www.telusko.com/contactus

Комментарии

Информация по комментариям в разработке