Friday, January 25, 2013

Java Control Structure Part 1-1


Do you want to know the secrets of Java Programming and use it for the advancement of your career in IT? Do you have problems in your Java course? Here is an instant solution for your problem. Avail the Java training package of Prof Erwin Globio and get free tips about Java Programming Secrets. For more free information about Java and Java Training visit the following sites: http://erwinglobio.sulit.com.ph/ http://erwinglobio.multiply.com/ http://erwinglobio.wordpress.com/. You may contact the Java Trainer at 09393741359 or 09323956678. Call now and be one of the Java Experts.

Control Structures

There are three ways to process a computer program. 

  1. In a sequence (executing statements in order)

  1. By making a selection or choice (branching) based on certain conditions (conditional statements)

  1. Repeating statements a certain number of times (looping)

Conditional statements can be created using the word if.  A condition is met if it evaluates to true and then certain statements are executed.  If it evaluates to false other statements are executed.


Relational Operators

In Java, a condition is represented by a logical (Boolean) expression (an expression that has a value of true or false when evaluated).  These expressions are created using relational operators (a binary operator consisting of two operands) and can be used to make comparisons.  Some relational operators in Java include:

==        equal to          
!=         not equal to
<          less than
<=        less than or equal to
>          greater than
>=        greater than or equal to

Relational Operators and Primitive Data Types

Relational operators can be used with integral and floating-point primitive data types.  For char values, whether an expression evaluates to true or false depends on the collating sequence of the Unicode character set.  When Java evaluates a logical expression, it returns the Boolean value true if the expression evaluates to true and false otherwise.


Comparing Strings

In Java, strings are compared character by character, starting with the first character and using the collating sequence. The character-by-character comparison continues until one of three conditions is met: a mismatch is found, the last characters have been compared and are equal, or one string is exhausted.

If two strings of unequal length are compared, and they are equal until the last character of the shorter string, then the shorter string is evaluated as less than the larger string. 

The method compareTo in the class String can be used to compare objects of the class.  This expression returns an integer value as follows:

Str1.compareTo(str2) = integer value less than 0 if string str1 less than string str2
Str1.compareTo(str2) = 0 if string str1 equal to string str2
            Str1.compareTo(str2) = integer value greater than 0 if string str1 greater than string str2

The method equals can be used to compare two strings and determine if they are equal.

Logical (Boolean) Operators and Logical Expressions

Logical (Boolean) operators enable you to combine logical expressions.  The three logical (Boolean) operators in Java are:

!           not       (unary)
&&      and      (binary)
||           or         (binary)

Logical operators take only logical values as operands and yield only logical values as results.

When you use the not operator, !true is false and !false is true.  Putting ! in front of a logical expression reverses the value of that logical expression.

&& and || are used to evaluate a combination of expressions.  An expression using && only returns true if ALL expressions are true.  || returns true as long as one expression in the combination is true.


Order of Precedence

Logical expressions have the following order of precedence when evaluating expressions:

Operators                                            Precedence
!  +  -  (unary operators)                      first
*  /  %                                                  second
+  -                                                       third
<  <=  >=  >                                         fourth
==  !=                                                  fifth
&&                                                      sixth
||                                                           seventh
= (assignment operator)                      last

Relational and logical operators are evaluated from left to right; their associativity is from left to right.


Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
 
 
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
 
 
Call Now:
 
SMART: 09393741359
SUN: 09323956678

No comments:

Post a Comment