Reading:  

Quick introduction to Java Programming language - Part 1


Conditional Statements

It is also known as Condition Statements. These statements are used to execute some instruction at specific condition.

Types of Condition Statements are:

 

  • if
  • else
  • else if
  • switch

 

The “if”statement:

This statement consists of an expression which results in Boolean value true/false.

Syntax:

if(condition)
{
   //statements
}

Example:

public class IfDemo
{
	public static void  main(String[] args) 
		{
		int i=0;
		System.out.println("Before increment i value is: "+i);
		if(i>=0)
		   {
			i++;
		 }
		System.out.println("After increment i value is: "+i);
	}
}

Output:

 

 

The “if...else”Statement:

This statement is used to execute false part when the Boolean expression is false.

Syntax:                                                                                                                                     

if(condition)
{
        //statements if condition is true
}
else
{
//statements if condition is false
}

Example

public class IfElseDemo
{
    public static void  main(String[] args) 
   {
        int i=0;
       System.out.println("Main starts");
	if(i>=1)
	   {
		System.out.println("if part:condition is true");
	   }
	else
	  {
		System.out.println("Else part: condition is false");
	  }
	System.out.println("Main Ends");
   }
}

Output

 

 

The “else if” Statement:

This statement is used to put more than one condition.The if statement can be followed by an else if...else statement if necessary,

Syntax:

If(condition)
{
       //statements
}
else
{
        //statements
}
else if
{
       //statement
}

Example: 

public class  Account 
{
	public static void  main(String[] args) 
		{
		int testMarks = 91;
		int rank =0;

		if (testMarks>= 90) {
			rank = 1;
		} else if (testMarks>= 80) {
			rank = 2;
		 } else if (testMarks>= 70) {
			rank = 3;
		 } else {
			rank = 11;
		 }
		System.out.println("Rank is: " + rank);
	   }
}

 Output

The switch Statement:

It is used to evaluate the list of values.

  • To come out from each case use break, if there is no break also no error in java.
  • If necessary then we can add break depends on requirement.
  • There is no order for case you can write default at the beginning only and default is optional.
  • Case values must be unique get the value from expected and matched with all cases if match execute the statement or else execute default.

Syntax:

switch(variable)
{
case value1:
statements;
break;
case value2:
statements;
break;
                     .
                     .
                     .
default:
statement;
break;
}

 

Example

public class SwitchDemo
 {
	public static void  main(String[] args) 
	{
		int n=2;

		switch(n)
			  {
		case 1 :
		System.out.println("Sunday"); 
		break;
		case 2 :
			System.out.println("Monday"); 
			break;
		case 3 :
		System.out.println("Tuesday");
		break;
		case 4 :
		System.out.println("Wednesday");
		break;
		case 5 :
		System.out.println("Thursday");
		break;
		case 6 :
		System.out.println("Friday");
			break;
		case 7 :
			System.out.println("Saturday");
			break;
		default :
		System.out.println("Invalid day");
			  }
		System.out.println("day is: " + n);
	}
}

 Output 

 

 

 

 

 

 

 

 

 

 

 

 

 

Description

This tutorial is targetted for beginers seeking a quick get on with guide on Java programming language. 3 parts include 

  1. Java basics
  2. Object oriented programming
  3. Advance concepts

Each part is subdivided in multiple chapters.

Java basics has the following chapters

  1. Introduction
  2. Environment Setup
  3. Basic Syntax
  4. Objects and Classes
  5. Basic Data Types
  6. Variable Types
  7. Modifier Types
  8. Basic Operators
  9. Loops
  10. Decision Making
  11. Numbers Class
  12. Character and String Class
  13. Arrays
  14. Date and Time
  15. Regular Expression
  16. Methods
  17. Streams, Files and I/O
  18. Exceptions Handling

Thanks for reading and as always, your feedback is very important to us. Let us know how we can improve and if you found any issues with this write up, send us a correction.



Audience

Beginners or students seeking a refresher on Java language

Author: Subject Coach
Added on: 9th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!