|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
. . . . . |
Java For Kids tutorial 3Java Operators
Operators are special symbols used to combine variables and/or values.
Here is a simple program that uses these operators:
void main() {
int a = 1;
int b = 2;
int c = a + b ;
printLine(c) ;
}
What should you get printed out?
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com Java Relational OperatorsRelational Operators compare two values to see if they are equal or one is greater or less than the other one.
Using the Java relational Operators.
The main use for the above relational operators are in CONDITIONAL phrases.
Or to put it simply in "What if " situations.
Lets say you were writing a program for bus fares.
And you wanted to know the correct fare based on age. How would you describe this problem ?
Here is a way : IF the passenger's age is greater than 18, THEN the price is normal Fare. OTHERWISE The price is half fare . Lets write that same algorithm (what is written above in plain english) in Java. It is a CONDITIONAL phrase.
If(age > 18)
{
price = 80 ;
}
else
{
price = 40 ;
}
OK lets write our Java program to calculate bus fares.
void main() {
// 1. declare variables
int fare = 80 ;
int age ;
// 2. get the age
printLine("What is your age please?");
// 3. the user enters their age
age = readInt() ;
// 4. check if over 18
if(age>18)
{
printLine("The fare will be " +
fare + ", thank you.");
}
// 5. the IF statement is false
else
{
printLine("The fare will be " +
fare/2 + ", thanks kid");
}
}
Notes on the bus fare java program
Both set of statements, ( if or else ) cannot both be processed.
Either one of them will be carried out.
It is a case of EITHER OR.
if( this statement is true)
{
// perform the actions inside these curly brackets
}
else
{
// if the statement after the IF is false or untrue then
// perform the actions inside these brackets here
}
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Forum | Games | free backgrounds | Resume | Flash Animations | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | store | advertisers | . . |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||