program selection by the IF ELSE in java

if in the time before we try to discuss the switch-case, on this occasion we will try, to share a little knowledge about the control program that uses the IF ELSE.

before we implement in a program we should mngetahui Else if the syntax itself, for what, because then we will be able to better distinguish between the control program if else with other control programs at a glance only, or visually.

syntax is as follows:

if (kondisi)

{

pernyataan1;



}

else

{

pernyataan2;



}


and examples of implementation of the program the opportunity this time we are attempting to program the output range of values ​​is the letter.

import java.util.Scanner;

public class MyClass {
public static void main() {

int nilai;
char nilai_huruf;
Scanner input = new Scanner(System.in);
System.out.print(“masukan nilai angka:”);
System.out.print(“\n”);
nilai = input.nextInt();
if((nilai>0)&&(nilai<=20))
nilai_huruf = ‘E’;
else if((nilai>40)&&(nilai<=60))
nilai_huruf = ‘C’;
else if((nilai>60)&&(nilai<=80))
nilai_huruf = ‘B’;
else
nilai_huruf =’A';
System.out.print(“nilai_huruf:”);
System.out.print(nilai_huruf);
return 0;
}
}


0 comments:

Twitter Delicious Facebook Digg Favorites More