JAVA if and if-else statemen


Normally a JAVA program executes step by step from first statement to last statement from top to bottom. But sometimes in programming its needed to control the flow of program execution depending on some type of condition or situation.
For example a student is considered as passed if gets 60 marks out of 100. Otherwise he is considered as failed. Here we have to dealt with the condition like this

if student-marks are greater then 60 he is added in the list of passed student
otherwise (else) added in the list of failed.

in above example we have to implement a condition for separating the passed and failed students.In such situations we have to control the normal flow of the program.

For above case JAVA uses if statement or if else statement for implementation
if statement executes a particular statement or group of statements if a particular condition is satisfied that is it evaluates to true.
If statement.
If statement test a condition specified inside it and executes a statement if it evaluate to true otherwise control is passed to next statement in the program.

If  statements tell the program to execute a certain section of code only if a particular condition is true.
A general syntax of the if statement is as below

 if (someExpression)
 statement1


For example

static int marks=60;
If (marks==60)
System.out.println(“You done it, Good Job”);






Above if-statement produce results as
You done it, Good Job
Because the condition inside the if statement evaluates to true.
The experession inside parenthesis () of if-statement is called boolean expression. A boolean expression is one which either evalutes to true or false.

Equal and Not Equal Comparisons

Another condition to check in a program is equality. Is a variable equal to a
specific value? Is one variable equal to the value of another? These questions
can be answered with the == operator, as in the following statements:

if (answer == rightAnswer) {
studentGrade = studentGrade + 10;
}
if (studentGrade == 100) {
System.out.println(“Show off!”);
}
You also can test inequality, whether something is not equal to something
else, with the != operator, as follows:
if (answer != rightAnswer) {
score = score - 5;
}

You can use the == and != operators with every type of variable except for
strings, because strings are objects.


if-else Statements


There are times when you want to do something if a condition is true and
something else if the condition is false. You can do this by using the else
statement in addition to the if statement, as in the following example:


             if(marks==60)
            System.out.println("You have done it");
            else
            System.out.println("You are failed");








int answer = 17;
int correctAnswer = 13;
if (answer == correctAnswer) {
score += 10;
System.out.println(“That’s right. You get 10 points”);
} else {
score -= 5;
System.out.println(“Sorry, that’s wrong. You lose 5 points”);
}

The else statement does not have a condition listed alongside it, unlike the
if statement. The else statement is matched with the if statement that
immediately precedes it. You also can use else to chain several if statements
together, as in the following example:

if (grade == ‘A’) {
System.out.println(“You got an A. Great job!”);
} else if (grade == ‘B’) {
System.out.println(“You got a B. Good work!”);
} else if (grade == ‘C’) {
System.out.println(“You got a C. What went wrong?”);
} else {
System.out.println(“You got an F. You’ll do well in Congress!”);
}

By putting together several different if and else statements in this way,
you can handle a variety of conditions. The preceding example sends a specific
message to A students, B students, C students, and future legislators.