Monday, February 23, 2009

Conditional Operator

The conditional operator is a ternary operator (it has three operands) and is used
to evaluate boolean expressions, much like an if statement except instead of
executing a block of code, if the test is true, a conditional operator will assign a
value to a variable. In other words, the basic purpose of the conditional operator is to decide
which of two values to assign to a variable. This operator is constructed using a ?
(question mark) and a : (colon). The parentheses are optional. Its structure is:

x = (boolean expression) ? value to assign if true : value to assign if false
Let's take a look at a conditional operator in code:

class Employee {
public static void main(String [] args) {
int pets = 3;
String status = (pets<4) sizeofyard =" 12;" numofpets =" 4;" status =" (pets<4)?"> 8)? "Pet limit on the edge"
:"too many pets";
System.out.println("Pet status is " + status);

}
remember that conditional operators are sometimes confused with assertion statements, so be
certain you can tell the difference.

Useful Links:-

Assignment Operators
Arithmetic Operators
Logical operators
Relational Operators
Bitwise Operator Intro
Bitwise Logical operator
Bitwise Left shift Operator
Bitwise Right shift Operator

Sponsored Links :-

Get Your SCJP preparation Kit free!!!!!!!
Get Your SCWCD preparation Kit free!!!
Are you Intelligent check here

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP