ඔන්න ඉතින් යාලුවනේ අදත් මම ඔයාලට තවත් වැදගත් වෙන පාඩමක් අරගෙන අවා අද මම ඔයාලට කියල දෙන්න යන්නේ java වල අපිට හම්බවෙන if ststements ගැන පොඩි විස්තරයක්.
එහෙමනම් ඉතින් අපි බලමු මොනවාද මේ if ststements කියල.
if statements ගත්තහම අපිට වර්ග කීපයකට බෙදන්න පුළුවන් ඒවා තමයි,
- The if Statement
- The if...else Statement
- The if...else if...else Statement
- Nested if...else Statement
අපි මුලින් ම බලමු The if Statement එක ගැන,
මේකේ ඉතාමත් සරල එකක් මේකෙදි වෙන්නේ Boolean expression එක true වෙනවනම් statement එක run වෙන එකයි.
බලන්නකෝ syntax එක,
if(Boolean_expression)
{
//Statements will execute if the Boolean expression is true
}
එහෙනම් ඉතින් කස්ටියත් එක්ක මේ example එක කරලම බලන්නකෝ.
public class Test {
public static void main(String args[]) {
int x = 20;
if (x < 30) {
System.out.print("This is if statement");
}
}
}
මේකේ result එක විදියට එන්න ඕනේ ඔයාලට This is if statement කියල. එක එන්නේ අපි ලියල තියන Boolean expression එක true නිසා if එක ඇතුලේ තියන statement එක run වෙනවා.
අපි එහෙනම් දැන් බලමු The if...else Statement එක ගැන,
මේකෙදි වෙන්නෙත් if එකේ උන දේම තමයි අලුත් දෙකට තියෙන්නේ අපි ලියන Boolean expression එක false උනොත් එක මොකද වෙන්නේ කියල else statement එකේ තියන එක run වෙන එකයි.
බලන්නකෝ syntax එක,
if(Boolean_expression){
//Executes when the Boolean expression is true
}else{
//Executes when the Boolean expression is false
}
මේකත් කරලම බලන්නකෝ එහෙනම්,
public class Test {
public static void main(String args[]) {
int x = 50;
if (x < 30) {
System.out.print("This is if statement");
} else {
System.out.print("This is else statement");
}
}
}
මේකේ result එක විදියට එන්න ඕනේ ඔයාලට This is else statement කියල. එක එන්නේ අපි ලියල තියන Boolean expression එක false නිසා else එක ඇතුලේ තියන statement එක run වෙනවා.
ඊටපස්සේ අපිට තියෙන්නේ The if...else if...else Statement එක,
මේකෙදි වෙන්නේ multiple if statements මේකෙදි අපිට conditions ගොඩක් check කරන්න පුළුවන්. මේක කරනකොට ඔයාල දැනගන්න ඕනේ කරුණු ටිකකුත් තියනවා මේ ටිකත් ඔලුවට දාගන්නකෝ
- An if can have zero or one else's and it must come after any else if's.
- An if can have zero to many else if's and they must come before the else.
- Once an else if succeeds, none of the remaining else if's or else's will be tested.
බලන්නකෝ syntax එක
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
//Executes when the Boolean expression 3 is true
}else {
//Executes when the none of the above condition is true.
}
මේක කරලම බලන්නකෝ එහෙනම් තවත් හොදට තේරෙයි එතකොට ඔයාලට
public class Test {
public static void main(String args[]) {
int x = 30;
if (x == 10) {
System.out.print("Value of X is 10");
} else if (x == 20) {
System.out.print("Value of X is 20");
} else if (x == 30) {
System.out.print("Value of X is 30");
} else {
System.out.print("This is else statement");
}
}
}
මේකේ result එක විදියට එන්න ඕනේ ඔයාලට Value of X is 30 කියල. එක එන්නේ අපි ලියල තියන Boolean expression එක true වෙන්නේ 3 වෙනි if statement එකේ නිසා එකේ තියන ස්ට්ස්ටෙමෙන්ට් එක run වෙනවා.
ඊටපස්සේ අපිට කරන්න තියෙන්නේ Nested if...else Statement එක ගැන,
මේකෙදි වෙන්නේ එක if statement එකක් ඇතුලේ තවත් if statement ලියන එකයි.
බලන්නකෝ syntax එක
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}
}
මේක කරලම බලන්නකෝ එහෙනම්
public class Test {
public static void main(String args[]) {
int x = 50;
int y = 20;
if (x == 50) {
if (y == 20) {
System.out.print("X = 50 and Y = 20");
}
}
}
}
ඔයාලට result එක විදියට එන්න ඕනේ X = 50 and Y = 20 කියල.
මම තව වැදගත් වෙන දෙයක් ඔයාලට කියල දෙන්නයි මේ හදන්නේ ඒ තමයි java වල අපිට හම්බවෙන switch statemets ගැන.
මේකෙදි කරන්නේත් if එකෙන් කරන දේම තමයි ඔයාලට ලේසියෙන් කරගන්න පුළුවන් අපි if else if කියා කියා ගොඩාක් දෙනවා වෙනුවට case කියල දෙන එක විතරයි කරන්න තියෙන්නේ බලාගන්නකෝ syntax එක
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
ඒ වගේම ඔයාල switch එක use කරනකොට දැනගන්න ඕනේ වැදගත්ම කාරුණු ටිකක් තියනවා මේ ටික හොදට ඔලුවට දාගන්න.
- The variable used in a switch statement can only be a byte, short, int, or char.
- You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
- The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
- Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
- A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
එහෙනම් මේක කරලම බලන්නකෝ,
public class Test {
public static void main(String args[]) {
char grade = 'C';
switch (grade) {
case 'A':
System.out.println("Distinction Pass!");
break;
case 'B':
System.out.println("Very Good Pass");
case 'C':
System.out.println("Credit Pass");
break;
case 'S':
System.out.println("Ordinary Pass");
case 'F':
System.out.println("Fail, Better try again");
break;
default:
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}
result එක එන්නේ,
Credit Pass
Your grade is C
එහෙනම් ඉතින් අදට යන්න වෙලාව හරි අද අපි මේ කරපු දේවල් ඉස්සරහට ගොඩාක් වැදගත් වෙන්වා. මේවා හොදට බලාගන්න
එහෙනම් ඉතින් මම ගියා.... ටටා :D හික් හික්
මතක ඇතුව කොමෙන්ටුවකුත් දාලම යන්න යාලුවනේ :)
No comments:
Post a Comment