Sunday, 4 July 2021

what is if statement || Conditional statement.

• Conditional Statements

• If statement 


Conditional statement

  •  These are statements within our program which are conditionally executed .
  •  To conditionally execute statement java provides us different control structures and operators.

  if statement 
 switch statement 
 Ternary/conditional operator

If statement


  • It is used to conditionally execute a block of statements.

Syntax:- 

if(condition)            
{
 statements
 ………………. 
else 
{
 statements 
……………….. 
next statement



 -> If condition is true then statements within if block are                                                                   executed and then program control is transferred to next statements.

->If condition is false then statements within else block are executed and then program control is transferred to next statements.
else part is optional.


Boolean expressions

  • Condition can be specified by using a Boolean expression i.e. an expression whose result is true or false

  • Boolean expression can be created by using relational and logical operators
  • Relational Operators: < , > , <= , >= , == , !=
  •  Logical Operators : && , || , !

  • WAP to read a number and check if it is an even or odd.
program:- 
package com.company;
import java.util.Scanner;
public class easy
{
public static void main(String args[])
{
Scanner likein=new Scanner(System.in);
System.out.print("Enter a Number:");
int a=likein.nextInt();
if(a%2==0)
System.out.println("Number is EVEN");
else
System.out.println("Number is ODD");
}
}

output:- 
Enter a Number: 8
Number is EVEN

  • WAP to read 3 angles and check if triangle can be formed or not.
program:-
package com.company;
import java.util.Scanner;
public class easy
{
public static void main(String args[])
{
Scanner likein=new Scanner(System.in);
System.out.print("Enter 3 angles:");
int a=likein.nextInt();
int b=likein.nextInt();
int c=likein.nextInt();
if(a+b+c==180)
System.out.println("Triangle can be formed");
else
System.out.println("Triangle cannot be formed");
}
}
output:- 
Enter 3 angles:100 60 20
Triangle can be formed



THANK YOU


No comments:

Post a Comment

What is ladder if || Java Ladder if statement.

 Ladder if statement If a if statement is used within an else statement then such a control structure is called as ladder if statement. Synt...