Search This Blog

Sunday, July 19, 2020

if-then

if-then: 
  if-then statement tells your program to to execute a certain section of code if a articular test evaluates to true. This is known as Conditional  logic.


Conditional  logic :

     Conditional logic uses specific statement in java to allow us to check a condition   and execute code base on whether that condition(the expression) is true or fales.


Code block:
A code block allows more than one statement to be executed  - a block of code.

Syntax:

if(expression)
{
//one or more statement code
}

Program:



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

boolean isfine = false;

if (isfine == false)
System.out.println("I am fine - inside if bloack");
System.out.println("Second line of code :");
System.out.println(" ======================================== ");
if (isfine == true) {
System.out.println("I am fine - inside if bloack");
System.out.println("Second line of code :");
}
}
}


Program:


public class IntIfElse {

public static void main(String[] args) {

int num = 100;
if (num == 100) {
System.out.println("num == 100 ");
} else if (num < 100) {
System.out.println("num < 100");
} else if (num != 100) {
System.out.println(" num != 100");
}
}
}


Logical AND  operator 

Program:


public class LogicalAndOperator {

public static void main(String[] args) {

int myScore = 75;

int topper = 98;

if (myScore < 40) {
System.out.println("i got pass marks = " + myScore);
} else if (myScore < topper) {
System.out.println("I have less marks than Topper ");
} else if (myScore > 40 && topper > 40) {
System.out.println(" we both got pas marks");
}
}
}

No comments:

Post a Comment