[go: up one dir, main page]

0% found this document useful (0 votes)
36 views3 pages

L9 - Conditional Stmts 14-12 Notes

The document discusses conditional statements in Java including if and if-else statements. It provides: 1) The if statement checks a condition and executes code if true. If false, the code is skipped. 2) The if-else statement checks a condition and executes one block of code if true, and another block if false. 3) Examples are given to check if a character is uppercase/lowercase, a vowel/consonant, and if a number is even/odd using if and if-else statements.

Uploaded by

Faarah Haani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

L9 - Conditional Stmts 14-12 Notes

The document discusses conditional statements in Java including if and if-else statements. It provides: 1) The if statement checks a condition and executes code if true. If false, the code is skipped. 2) The if-else statement checks a condition and executes one block of code if true, and another block if false. 3) Examples are given to check if a character is uppercase/lowercase, a vowel/consonant, and if a number is even/odd using if and if-else statements.

Uploaded by

Faarah Haani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9.

Conditional Constructs In Java


 Java allows us to check a condition and execute certain instructions
depending on the condition whether it is “true” or “false” such
statements are called conditional or decision or selection statements.
 In java there are 2 forms of conditional statements.
1.The if statement – to choose between two alternatives
2. The switch statement – to choose between multiples alternatives

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( ).

2. The if – else statement:


There may be a situation where you may want to execute statements
when the condition evaluates to false. For such scenarios , the if-else
control structure ( construct) is used.

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");
}
}
}

Ex: 2. W A J P to accept a character. Check and display whether the


given character is a vowel or consonant.

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");
}
}
}

Ex: 3. W A J P to accept a number. Check and display whether the given


number is Even or Odd. [ H.W]

You might also like