Credits

Saturday, February 21, 2009

Java's Conditional Statements

Just like C or C++, Java has conditional statements similar to the former's structures.

Do check these sample Java programs on IF statements.



[+/-] show/hide



//sample if program
public class SampleIF{
public static void main(String args[])
{ double grade=90.00;
if (grade<75.00)
System.out.println("Sorry, you failed!");
else
System.out.println("Sorry, you passed!");

}
}

WHAT IS THE OUTPUT OF THE CODE # 1 ABOVE?

//Guessing program version 1
class Guess{
public static void main(String args[])
throws java.io.IOException{
char ch, answer='R';
System.out.println("I am thinking of a letter between A to Z. Guess?");
ch=(char) System.in.read();
if (ch == answer) System.out.println("Your guess is right");
else System.out.println("Your guess is wrong");

}

}


//Guessing program version 2
class Guess{
public static void main(String args[])
throws java.io.IOException{
char ch, answer='R';
System.out.println("I am thinking of a letter between A to Z. Guess?");
ch=(char) System.in.read();
if (ch == answer) System.out.println("Your guess is right");
else
{ System.out.println("Your guess is wrong");
if (ch
else
System.out.println("Too high");}
}

}

HOW ABOUT THE LAST TWO PROGRAMS?

AddThis