// If else in Java code
import java.util.Scanner;
class IfElse
{
public static void main(String[] args)
{
int marksObtained, passingMarks; passingMarks = 40;
Scanner input = new Scanner(System.in); System.out.println("Input marks scored by you"); marksObtained = input.nextInt();
if (marksObtained >= passingMarks)
{
System.out.println("You passed the exam.");
}
else
{
System.out.println("Unfortunately you failed to pass the exam.");
}
}
}
Step-by-Step Explanation:
1. Import Scanner class:
import java.util.Scanner;
This class imports the Scanner from the java.util package, which helps read the input of the user.
2. Class Declaration:
class IfElse {
The class IfElse is declared here. This class holds the logic to determine whether a student has passed or failed based on their marks.
3. Main Method Declaration:
public static void main(String[] args) {
The main method is the entry point of the Java program, where the program execution starts.
4. Declaring and Initializing Variables:
int marksObtained, passingMarks;
passingMarks = 40;
- The marksObtained variable is declared to store the marks scored by the student.
- The passingMarks variable is set to 40, representing the minimum marks required to pass the exam.
5. Creating a Scanner Object:
Scanner input = new Scanner(System.in);
A Scanner object is created named input to scan for user input in the console.
6. Asking for Input from the User:
System.out.println(“Input marks scored by you”);
This statement prints the message “Input marks scored by you” and asks the user for inputting marks.
7. User Input Scan
marksObtained = input.nextInt();
The program reads the integer input given by the user (the marks scored by the student) and stores it in the marksObtained variable.
8. If-Else Conditional Check:
if (marksObtained >= passingMarks) {
System.out.println(“You passed the exam.”);
} else {
System.out.println(“Unfortunately you failed to pass the exam.”);
}
- The program checks if the marks scored by the student are greater than or equal to the passing marks (40).
- If marksObtained >= passingMarks evaluates to true, the program prints “You passed the exam”.
- If marksObtained < passingMarks is false, the program prints “Unfortunately you failed to pass the exam”.
Example Execution:
# Example 1: Student Passed the Exam
Input:
Input marks scored by you
50
In this example, the student has input 50 as his marks, which is greater than or equal to the passing marks (40).
Since marksObtained >= passingMarks is true, the program will print:
# Example 2: Student Failed the Exam
Input:
Input marks scored by you
30
Here, the student had inputted 30 as marks, which is less than the passing marks 40.
Since marksObtained >= passingMarks is false, the program will print:
Output:
Unfortunately you failed to pass the exam.
Summary of Output:
1. For input 50 (marks obtained):
You passed the exam.
2. For input 30 (marks obtained):
Unfortunately, you failed to pass the exam.
Conclusion:
- The program checks if the student has passed or failed the exam based on their inputted marks.
- If the students’ marks are greater than or equal to the passing marks (40), it prints “You passed the exam.”
- If the marks of the student are less than passing marks, then it prints “Unfortunately you failed to pass the exam.”