CONDITIONAL STATEMENTIN JAVA
Topics Covered
- Control Statements
- Conditional Statement
- Nested If
- Switch..case default Statement
- A limitation with switch..case
- Break statement in switch
- Lopping structure
Control statements specify the order of execution of the instructions present in a program. These make it possible for the program to make certain decisions, perform various tasks repeatedly, or even jump from any one section of the code to a different section.
2. Conditional Statement
We can use these conditions to perform different actions for different decisions.Java supports the usual logical conditions from mathematics:
Less than a < b
Less than or equal to a <= b
Greater than a > b
Greater than a >= b
Equal to a == b
Not Equal to a != b
Java has the following conditional statements
- Use
if
to specify a block of code to be executed, if a specified condition is true - Use
else
to specify a block of code to be executed, if the same condition is false - Use
else if
to specify a new condition to test, if the first condition is false - Use
switch
to specify many alternative blocks of code to be executed
The if Statement
Use of the 'if statement' specifies a block of Java code to be executed if a condition got to be
true
Syntax
if (condition) {
// block of "if the condition is true"
}
Note:-
1. if is in lowercase letters.
2. Uppercase letters (If or IF) will generate an error.
Example:-
The If..Else If Statement
Use of the 'if.elseIf statement' specifies a block of Java code to be executed if a condition got to be
true or false means the binary condition.
Syntax
if (condition)
{
// block of "if the condition is true" (if block statements)
}
else
{
// block of "if the condition is false" (else block statement)
}
Note:-
1. Never put a semi-colon after if(condition)
2. Semicolon will in each block statement ends.
Example:-
The If..Else..Else If Statement
Use of the 'if..ElseIf..Else statement' specifies a block of Java code to be executed if a condition got to be
true or false means the binary condition.
Syntax
if (condition)
{
// block of "if the condition is true" (if block statements)
}
else if(condition)
{
// block of "else if the condition is false" (else-if block statement)
}
else
{
// block of "if else the condition is also false" (else block statement)
}
Note:-
1. Never put a semi-colon after if(condition)
2. Semicolon will in each block statement ends.
Example:-
Nested If Statement
Use of the 'Nested..Else statement' is if within an another if condition
Syntax
if (condition)
{
// block of "if the condition is true" (if block statements)
if(condition)
{
// block of "Nested if the condition is true" (Nested-if block statement)
}
else
{
// block of "Nested if else's Block Nested if's if condition is also false" (Nested-if block statement)
}
}
else
{
//block of Finally else
}
Note:-
1. Never put a semi-colon after if(condition)
2. Semicolon will in each block statement ends.
Example:-
Program 1: Write a program to input three numbers (positive or negative). If they are unequal then display the greatest number otherwise display they are equal. The program also displays whether the number entered by the users are "A; positive", "All Negative" or "Mixed Numbers".
Sample Input: 56, -15, 12
Sample Output: The greatest number is 56
Entered numbers are mixed numbers.
Solution:-
class Greatest {public static void main(String[] args) {
// declaring double type variables
double n1 = -1.0, n2 = 4.5, n3 = -5.3, largest,negative;
// checks if n1 is greater than or equal to n2
if (n1 >= n2) {
// if...else statement inside the if block
// checks if n1 is greater than or equal to n3
if (n1 >= n3) {
largest = n1;
}
else {
largest = n3;
}
} else {
// if..else statement inside else block
// checks if n2 is greater than or equal to n3
if (n2 >= n3) {
largest = n2;
}
if(n1<0)
{
negative=n1
}
if(n2<0)
{
negative=n2;
}
if(n3<0)
{
negative=n3;
}
}
};
};
No comments:
Post a Comment