Page 1 of 1

Control statements in java, if-else statements Rate Topic: ***** 1 Votes

Poll: control statements in java (17 member(s) have cast votes)

was this tutorial useful?

  1. yes (15 votes [88.24%])

    Percentage of vote: 88.24%

  2. no (2 votes [11.76%])

    Percentage of vote: 11.76%

Vote Guests cannot vote

#1 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Posted 11 November 2008 - 06:53 AM

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
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:
if( condition to be checked )
	statement1
else
	 statement2



so our java code would look like this:
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
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
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 November 2008 - 10:52 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Control statements in java, if-else statements

#2 farhaan26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-June 11

Posted 11 June 2011 - 09:56 AM

PLEASE CAN YOU COMPLETE THE PROGRAM SO IT CAN BE EXECUTABLE....WHAT IS MISSING IS MAIN AND CLASS DECLARATION...IF YOU UNDERSTAND WHAT I MEAN ............I TRIED TO INVENT


class StudentGradess {
public static void main ( String args[]){

int i=100;
{

f( 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" );
}
}
}


....BUT I DONT KNOW WHAT AM DOING FOR WRONG ... THE COMPILER ANSWERS

grade cannot be resolved to a variable
grade cannot be resolved to a variable
grade cannot be resolved to a variable
grade cannot be resolved to a variable
grade cannot be resolved to a variable
grade cannot be resolved to a variable

PLEASE SHED SOME LIGHT IN THIS DEEP DARKNESS

This post has been edited by macosxnerd101: 11 June 2011 - 01:50 PM
Reason for edit:: Added code tags and removed quote

Was This Post Helpful? 0
  • +
  • -

#3 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Posted 11 June 2011 - 11:53 AM

Quote

grade cannot be resolved to a variable
simply means that you have not stored the variable grade in memory, you have to actually declare grade before using it: You could do this like so:
int grade = 95;


Was This Post Helpful? 0
  • +
  • -

#4 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,365
  • Joined: 14-January 10

Posted 12 June 2011 - 08:01 PM

View Postv0rtex, on 11 June 2011 - 01:53 PM, said:

Quote

grade cannot be resolved to a variable
simply means that you have not stored the variable grade in memory, you have to actually declare grade before using it: You could do this like so:
int grade = 95;



I usually declare mine with a value of 0. That way I don't ever have to worry about the value interfering with any of my for loops as usually they begin with 1. :D

That's just the way I do it. I guess you can call it a habit.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1