Hello everyone,
welcome to my first java tutorial, this tutorial is aimed towards beginners but of course anyone is welcome

..in this tutorial i will explain to you what are the java control statements and how to use them..
Normally, statements in a program are executed one after the other in the order in which they are written. This process is called sequential execution. Various Java statements, which we will soon discuss, enable the programmer to specify that the next statement to execute is not necessarily the next one in sequence. This is called transfer of control and is done using control statements.
control statements include if-else statements, switch statements ,while loops, do-while loops, and for loops.. in this tutorial we will be looking at if-else statements.
sometimes when we want to solve a problem, decision making is needed, lets say we want to write a program to notify the user whether or not to wear a jacket depending whether its a rainy day or not.. this will be done using if-else statement
CODE
pseudo code:
if it is raining
then wear a jacket
else
then a jacket is not needed
as you may have guessed, the compiler first looks at the first statement and checks the condition, if the condition is fulfilled then the next block of statements will be executed and the user will get a notification to wear a jacket, else if the condition of the first statement is not fulfilled, then the compiler will jump on to the block of the next statement which is in this case the else statement and the user will get a notification that a jacket is not needed...so as you see here, our algorithm makes a decision whether to execute a statement or not by using if-else statements..
if-else structure looks like this:
CODE
if( condition to be checked )
statement1
else
statement2
so our java code would look like this:
CODE
boolean raining = true;
if( raining == true )
System.out.println( "wear a jacket" );
else
System.out.println( "jacket not needed" );
but what if we want to execute more than one command if either conditions are true??.. in that case we will have to use braces
CODE
if( condition to be checked )
{
block of commands
}
else
{
block of commands
}
ok, now lets say we want to design a program for a school to calculate the grades of students
90-100: A
80-89: B
70-79: C
60-69: D
50-59: F
also students will get a message with their grades to tell them about their performance for example, excellent, very good, not bad, need more studying, etc
our code will look like this
CODE
if( grade >= 90 && grade <= 100 )
{
System.out.println( 'A' );
System.out.println( "excellent job" );
}
else
if( grade >= 80 && grade < 90 )
{
System.out.println( 'B' );
System.out.println( "very good" );
}
else
if( grade >= 70 && grade < 80 )
{
System.out.println( 'C' );
System.out.println( "not bad" );
}
else
if( grade >= 60 && grade < 70 )
{
System.out.println( 'D' );
System.out.println( "need more studying" );
}
else
if( grade < 60 && grade >= 0 )
{
System.out.println( 'F' );
System.out.println( "sorry, you have failed..please try harder next time" );
}
else
System.out.println( "the number you have entered is not valid" );
in this program we have used the logical AND operator "&&", the logical AND basically is used when you want to check for 2 conditions where both have to be fulfilled, you can read more about it on the internet or something...the first if statement says, if ( grade is greater or equal to 90 AND grade is less than or equal to 100 ) then the condition is true and the next block will be executed...note: if one of the conditions fail, then the block of that if statement does not execute and the program jumps to the next one..
so lets say the user enters 101, the program goes to the first if statements, and will check...is 101 >= 90??? answer: true, then it will check is 101 <= 100?? answer: false, and since we used the logical AND "&&" operator, both conditions should be true and so the first if statement's block will not execute and the program will keep on jumping to the next if statements till it reaches the last else statement and will execute its block...
what if the user enters 95??
then the program will check the first if statement, is 95 >= 90?? answer: true, is 95 <= 100, answer: true...since both are true, the program will execute that if statement's block
and will output:
A
excellent
this is the end of my tutorial, i hope it was helpful for you and sorry if my typing is messy, but english is not my first language lol..anyway try to understand this tutorial, if you have any problems with it you can comment on the tutorial or send me a private message, and if you like this tutorial then stay tuned for the next one which will be on nested if statements...
This post has been edited by mostyfriedman: 11 Nov, 2008 - 09:52 PM