hello programmers... this program will show to you how to use if statement. It is for computing two input numbers by the user. this is for asking the user to choose the operator they want to execute. 1- addition, 2- subtraction, 3- Multiplication, 4- Quotient. this is a very simple program using nested if statement.
here is a sample program:
import java.io.*;
import java.util.Scanner;
public class chooseoperators
{
public static void main(String args[])
{
Scanner input==new Scanner(System.in);
int add=1;
int sub=2;
int mult=3;
int div=4;
int num1;
int num2;
int oper;
int sum=0,diff=0,prod=0,quo=0;
System.out.println("1- Addition");
System.out.println("2- Subtraction");
System.out.println("3- Multiplication");
System.println("4- Division");
System.out.print("Enter First Number:");
num1=input.nextInt();
System.out.print("Enter Second Number:");
num2=input.nextInt();
System.out.print("Chooose Operators:");
oper=input.nextInt();
if(oper==add)
{
sum=num1+num2;
System.out.println("The sum is:" + sum);
}
if(oper==sub)
{
diff=num1-num2;
System.out.println("The difference is:" +diff);
}
if(oper==mult)
{
prod=num1*num2;
System.out.println("The Product is:" + prod);
}
if(oper==div)
{
quo=num1/num2;
System.out.println("The quotient is:" + quo);
}
}
}
Showing posts with label if statement. Show all posts
Showing posts with label if statement. Show all posts
Tuesday, August 31, 2010
Wednesday, July 7, 2010
If Statement
IF statement - is a conditional statement.
syntax:
if(conndition)
{
statement 1;
}
IF ELSE - also conditional statement that the program will choose the true statement or false statement depending on thier condition.
syntax:
if(condition)
{
statement 1;
stmt 2;
}
else
{
statement 1;
stmt 2;
}
syntax:
if(conndition)
{
statement 1;
}
IF ELSE - also conditional statement that the program will choose the true statement or false statement depending on thier condition.
syntax:
if(condition)
{
statement 1;
stmt 2;
}
else
{
statement 1;
stmt 2;
}
Labels:
if,
if else,
if else in java,
if statement,
java if
Sunday, April 4, 2010
If Statement
if statement is a conditional statement in programming. In this statement we are using the conditonal operators. this is true or false only. if the conditional statement is true the program execute the true statements, if the conditional statement is false the program execute the false statements.
sample program:
this program will show to you if the grade of students is greater than 75 the program will show message "Your passed" else "Sorry your Failed".
import java.io.*;
public class sample
{
public static void main(String args[])
{
int average=75;
if(average>=75)
{
System.out.print("Your Passed");
}
else
{
System.out.print("Your Failed");
}
}
}
sample program:
this program will show to you if the grade of students is greater than 75 the program will show message "Your passed" else "Sorry your Failed".
import java.io.*;
public class sample
{
public static void main(String args[])
{
int average=75;
if(average>=75)
{
System.out.print("Your Passed");
}
else
{
System.out.print("Your Failed");
}
}
}
Subscribe to:
Posts (Atom)