Sunday, July 15, 2012

IF ELSE or Condition in Java

Like other programming languages Java also provide conditional structures like IF, IF ELSE, IF ELSE IF or SWITCH. They all are similar to C++ in syntax. Here is a simple example on IF ELSE structure.

public class IfElse
{
public static void main(String[] Args)
{
int marks = 82;
if(marks > 80)
{
System.out.println("A+ Grade");
}

if(marks < 80)
{
System.out.println("Grade is not A+");
}
}
}

Here I have declared a variable "marks" which will have the value of marks achieved by some student. Now see the structure of the IF-ELSE statement. First we have "if" and then in () we have a condition. If this condition is true the next block of code in {} will get execute. And if that condition is false the block of code in {} next to else will get executed. Here we have marks = 82 so the first block of the code will execute and "A+ Grade" will be displayed on the console.
So this was our today's lesson. If else is very basic but very useful structure. So understand it and have lot of practice. We will use it a lot in our next codes. So stay with us for next lesson.

No comments:

Post a Comment