TESTClass8compchJAVA-Decisionmakingstatement
TESTClass8compchJAVA-Decisionmakingstatement
Conditional construct or Decision Making statements are specific statements that allow us to check a condition
and execute certain parts of code depending on whether the condition is true or false.
Conditional statements in Java are : if, if-else, if–else if –else and switch case. (if and if-else is in your syllabus)
if statement
Syntax
if (expression) {
// statements
}
if(comp>=50)
System.out.println("PASS");
if(comp<50)
System.out.println("FAIL");
}
}
if else
Syntax
if (expression)
{
// statements
}
else
{
// statements
}
Pag e1|4
// Program to display the result - pass / fail(passing marks - 50 /100)
class ifEg
{
public static void main(String args[])
{
int comp=30;
if(comp>=50)
System.out.println("PASS");
else
System.out.println("FAIL");
}
}
WAP to accept a number from the user and check whether it is positive or negative.
class PosNeg
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
System.out.println(“Enter any number”);
int n = in.nextInt();
if(n >0 )
{
System.out.println(“Positive number”);
}
else
{
System.out.println(“Negative number”);
}
}
}
WAP to accept a number from the user and check whether it is odd or even.
class OddEven
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
System.out.println(“Enter any number”);
int n = in.nextInt();
if(n % 2==0 )
{
System.out.println(“Even number”);
}
else
{
System.out.println(“Odd number”);
}
}
}
Pag e2|4
WAP to accept a number from the user and check whether it is divisible by 5 or not.
class Divisible
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
System.out.println(“Enter any number”);
int n = in.nextInt();
if(n % 5==0 )
{
System.out.println(“ number is divisible by 5”);
}
else
{
System.out.println(“number is not divisible by 5”);
}
}
}
WAP to accept a number from the user and check whether it is divisible by 3 and 11 or not.
class Divisible
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
System.out.println(“Enter any number”);
int n = in.nextInt();
WAP to accept a number from the user ,take out the last digit and check whether it is divisible by 7 or not.
class Divisible
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
System.out.println(“Enter any number”);
int n = in.nextInt();
int ld=n%10;
if(ld %7==0 )
{
Pag e3|4
System.out.println(“ last digit is divisible both by 7”);
}
else
{
System.out.println(“last digit is not divisible by 7”);
}
}
}
Pag e4|4