Today we are going to use syntax for Nested-if statement in this case you can use it in any other way that allows you to write the main purpose of your program or portion of a program. If we want to use any other loop inside the code to control its destiny, because it is important to determine and terminate loop repetition at specific time. First of all we look at a syntax of Nested-if statements as this example.
if ( grade >= 90 )
System.out.println("A");
else if ( grade >= 80 )
System.out.println("B");
else if ( grade >= 50 )
System.out.println("C");
else
System.out.println("failed");
System.out.println("A");
else if ( grade >= 80 )
System.out.println("B");
else if ( grade >= 50 )
System.out.println("C");
else
System.out.println("failed");
Easy and enjoyable! We used Standard outputs to show results by (System.out), Also there is another option by using GUI outputs or input, however. To make it clear let's write it as a completed code. The program asks the user to input grade and displays results in Standard input and output ( Standard input or output it also named Console ).
import java.util.*; // import statement
public class You // calss name
{
public static void main(String []args)
{
int grade; // Variable named grade, type of Integer
Scanner scan = new Scanner(System.in); // Scanner object and make it input by System.in
System.out.print("Enter your grade "); // Number entered by user
grade = scan.nextInt(); // Jump to next after number entered
if ( grade >= 90 )
System.out.println("A");
else if ( grade >= 80 )
System.out.println("B"); // Nested-if statement with results
else if ( grade >= 50 )
System.out.println("C");
else
System.out.println("failed");
}
}
It is now with all values that give the user result about the grade as you can see evaluating which grade get its result with relational operators ( bigger than or equals ) we can call this part "( grade >=)" boolean expression in all uses. Know our program is sucssisfuly displays its data but, what if the user have entered impossible value or more numbers?. We can determine this executing cycle with possible numbers that only display the correct results otherwise do not display values. Simply let's use while statement to control the loop or in other words number of grades that program only contain.
import java.util.*; // import statement
public class You // calss name
{
public static void main(String []args)
{
int grade; // Variable named grade, type of Integer
Scanner scan = new Scanner(System.in); // Scanner object and make it input by System.in
System.out.print("Enter your grade "); // Number entered by user
grade = scan.nextInt(); // Jumpby next after number entered
while ( grade > 100 ) // while statement if the grade bigger than 100
{
System.out.println("Invalid number "); // Show output Invalid grade number
System.out.println("Enter your grade again "); // Enter again
grade = scan.nextInt();
}
if ( grade >= 90 )
System.out.println("A");
else if ( grade >= 80 )
System.out.println("B"); // Nested-if statement with results
else if ( grade >= 50 )
System.out.println("C");
else
System.out.println("failed");
}
}
0 comments:
Post a Comment