filmov
tv
#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)
e.g
check n is equal to zero
if(n==0)
#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)
else
e.g
check n is equal to zero or not
if(n!=0)
else
#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)
else if(n grater-then 0)
else
#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)
if(n%2==0)
{
}
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;
}
Incorrect way:
if(n%2==0)
int a=5;
int b=6;
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;
More Learning :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
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)
e.g
check n is equal to zero
if(n==0)
#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)
else
e.g
check n is equal to zero or not
if(n!=0)
else
#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)
else if(n grater-then 0)
else
#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)
if(n%2==0)
{
}
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;
}
Incorrect way:
if(n%2==0)
int a=5;
int b=6;
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;
More Learning :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Комментарии