L9 - Conditional Stmts 14-12 Notes
L9 - Conditional Stmts 14-12 Notes
Conditional
statements
if switch
if - else
if-elseif else
nested if
1. The if statement :
An if statement tests a particular condition, if the condition evaluates to
“true” , the statement is executed , otherwise the statement is skipped.
Syntax:
if (condition) if(cond) true
{
-- // statements stmts
--
}
Ex : W A J P to illustrate if statement.
class ifexample
{
public static void main(String hj[])
{
int age=10;
if(age>=18)
{
System.out.println(“Eligible to Vote”);
}
System.out.println(“ can nit vote”);
}}
Note: condition must be enclosed in paranthesis( ) or brackets( ).
Syntax:
if ( condition)
{
-- false if(cond) true
--
} statements statements
else
{
--
--
}
Ex: 1. W A J P to accept a character. Check and display whether the
given character is an Upper case or Lowercase letter.
import java.util.*;
public class upperlower
{
public static void main(String hj[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a character :");
char ch = sc.next().charAt(0);
if(ch>='A' && ch<='Z')
{
System.out.println(ch+" it is an UpperCase letter");
}
else
{
System.out.println(ch+" it is an LowerCase letter");
}
}
}
import java.util.*;
public class vowcon
{
public static void main(String hj[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a character :");
char ch = sc.next().charAt(0);
if(ch=='A'|| ch=='a'||ch=='E'|| ch=='e'||ch=='I'|| ch=='i'||ch=='O'||
ch=='o'||ch=='U'|| ch=='u')
{
System.out.println(ch+" It is a Vowel");
}
else
{
System.out.println(ch+" It is a consonant");
}
}
}