Lecture 5 Conditional Statements in JAVA
Lecture 5 Conditional Statements in JAVA
if (boolean-condition)
statement;
if(a > b)
System.out.print("A is greater");
else;
System.out.print("B is greater");
}
}
Solution to Previous Question
public class Vote
{
public static void main(String[] args)
{
int a = 10, b = 5;
if(a > b)
System.out.println("A is greater");
else;
System.out.print("B is greater");
}
}
Output will be:
A is greater
B is greater
because the semicolon after the else statement creates an empty block,
so the next line ("System.out.print("B is greater")") is executed unconditionally, regardless of the condition in
the if statement.
Output?
public class Vote
{
public static void main(String[] args)
{
int a = 10, b = 5, c = 2;
if(a > b || ++c == 3)
System.out.print("A is greater");
else
System.out.print("B is greater");
System.out.print(c);
}
}
Solution to Previous Question
public class Vote
{
public static void main(String[] args)
{
int a = 10, b = 5, c = 2;
if(a > b || ++c == 3)
System.out.print("A is greater");
else
System.out.print("B is greater");
System.out.print(c);
}
}
Output:
A is greater
2
This is because the || operator (logical OR) is used here, which evaluates the left-hand side first.
Since a > b is true, the right-hand side (++c == 3) is not evaluated, and the if block is executed. The
value of c remains 2 because ++c == 3 is not executed.
Output?
public class Vote
{
public static void main(String[] args)
{
int a = 10, b = 5, c = 2;
if (a > b && ++c == 3)
System.out.print("a is greater");
else
System.out.print("b is greater");
System.out.print(c);
}
}
Solution to Previous Question
public class Vote
{
public static void main(String[] args)
{
int a = 10, b = 5, c = 2;
if (a > b && ++c == 3)
System.out.print("a is greater");
else
System.out.print("b is greater");
System.out.print(c);
}
}
Output: a is greater 3
because the && operator (logical AND) is used here, and both conditions must be true for the if
block to be executed. Since a > b is true, the right-hand side (++c == 3) is also evaluated, and the
value of c is incremented to 3.
Program:
Checking whether a number is Even or not
Switch Statements in Java
• Use the switch statement to select one of many code blocks to be executed.
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Switch Statements in Java
This is how it works:
• The switch expression is evaluated once.
• The value of the expression is compared with the values of each
case.
• If there is a match, the associated block of code is executed.
• The break and default keywords are optional,
The break Keyword
• When Java reaches a break keyword, it breaks out of the switch block.
• This will stop the execution of more code and case testing inside the
block.
• When a match is found, and the job is done, it's time for a break.
There is no need for more testing.
The default Keyword
• The default keyword specifies some code to run if there is no case
match
Output?
public class Vote
{
public static void main(String[] args)
{
char ch = 'A';
switch (ch) {
case 'a':
System.out.print("a");
case 'A':
System.out.print("A");
case 'b':
System.out.print("b");
default:
System.out.print("Default");
}
}
}
Output?
public class Vote
{
public static void main(String[] args)
{
int a = 10, b = 5;
switch (a - b) {
case 2:
System.out.print("Two");
break;
case 5:
System.out.print("Five");
break;
case 10:
System.out.print("Ten");
break;
default:
System.out.print("Default");
}
}}