1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* If Else statement Example This Java Example shows how to use if else statement in Java program. */ public class SimpleIfElseStatementExample { public static void main(String[] args) { /* * If else statement is used to execute either of two conditions based * upon certain condition. * Syntax of if else statement is, * * if(<condition>) * statement1 * else * statement2 * * where <condition> is a boolean expression. * If <condition> is true, statement1 will be executed, else statement2 will * be executed. */ int i = 0; if(i == 0) System.out.println("i is 0"); else System.out.println("i is not 0"); } } /* Output would be i is 0 */ |
how to use if else statement by making a pingpong games.
uhmm…how can you compare 4 different statements using if else…that was exam yesterday… and i wasn’t able to answer it…please help me…thanks a lot…the problem was…. making a program that would show numbers if they are odd or even and if they are negative or a positive odd/even number…the condition is…input a positive or negative number…numbers range from -100 to 0 and 0 to +100
public class oddeven
{
public static void main(String args[])
{
System.out.println(“the odd and even number in between -100 to 100:”);
int num1=100;
if(num1!=0)
{
System.out.print(“the positive even numbers are:”+” “);
for(int i=0;i<num1;i++) {=”” if(i%2=”=0)” system.out.print(i+”=”” “);=”” }=”” system.out.println();=”” system.out.print(“the=”” positive=”” odd=”” numbers=”” are:”+”=”” “);=”” for(int=”” i=”0;i<num1;i++)” {=”” if=”” (i%2=”=0)” continue;=”” else=”” system.out.print(i+”=”” “);=”” }=”” }=”” int=”” num2=”-100;” if(num2<0)=”” {=”” system.out.println();=”” system.out.print(“the=”” negative=”” even=”” numbers=”” are:”+”=”” “);=”” for(int=”” i=”0;i”>num2;i–)
{
if(i%2==0)
System.out.print(i+” “);
}
System.out.println();
System.out.print(“the negative odd numbers are:”+” “);
for(int i=0;i>num2;i–)
{
if(i%2==0)
continue;
else
System.out.print(i+” “);
}
}
}
}
how to use if-else statement in converting the distance in km to miles,yard,meter and feet?
I have one doubt. Is an else statement needed really here?? As per the explanation, if continue statement is given;it will skip the rest of the statements in the loop of the current iteration.So without an else aslo it will work correctly. Can u please clarify.
we given condition if(i==0),the ‘i’ is zero.then condition is true, i will write the ‘continue’ statement.the continue statement to continue the task but else statement, i will used for print the false condition.but i can’t write the print statement in the continue statement.
interface
example