Java – Conditional Statements

by seetaram on July 2, 2011

Java being a programming language provides conditional statements to handle execution of a set of statements based on the conditions.

  1. if
  2. if-else
  3. switch

See example code below:

package com.selftechy.learnjava;

public class TestCase {
	public static int tcCount;
	
	public int stepcount;
	public String status;

	public int getStepCount(){
		return stepcount;
	}
	
	public String getStatus(){
		return status;
	}
}

 

package com.selftechy.learnjava;

public class MainMethod {

	/**
	 * Author - Seetaram Hegde
	 */
	public static int testPasscount=0;
	public static int testFailcount=0;
	public static int testBlockedcount=0;

	public static void main(String[] args) {
		TestCase tc_one = new TestCase();
		TestCase tc_two = new TestCase();
		TestCase tc_three = new TestCase();
		TestCase tc_four = new TestCase();
		TestCase tc_five = new TestCase();
		TestCase.tcCount=5;
		
		tc_one.stepcount=10;
		tc_two.stepcount=5;
		tc_one.status="pass";
		tc_two.status="fail";
		tc_three.status="blocked";
		tc_four.status="fail";
		tc_five.status="pass";
		
		if (tc_one.status.equalsIgnoreCase("pass"))
			System.out.println("First test case is pass");
		else if (tc_one.status.equalsIgnoreCase("fail"))
			System.out.println("First test case is fail");
		else if (tc_one.status.equalsIgnoreCase("blocked"))
			System.out.println("First test case is blocked");

		if (tc_two.status.equalsIgnoreCase("pass"))
			System.out.println("Second test case is pass");
		else if (tc_two.status.equalsIgnoreCase("fail"))
			System.out.println("Second test case is fail");
		else if (tc_two.status.equalsIgnoreCase("blocked"))
			System.out.println("Second test case is blocked");
		
	}
}

Execution of the above code gives the output as follows:

First test case is pass
Second test case is fail

If statement is used to compare two values and executes the statements inside the block depending on the condition.  If the condition is true then the block inside if will be executed else the block of code inside the else will be executed.

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email