Control Statements in Java
We need to execute a set of statements based on a condition, we need to use control flow statements.
Types of control statements.
1) if statement in Java
if statement is used to test the condition this statements get execute only when the given condition is true. If the condition is false then the statements inside if statement body are completely ignored.
Show in Fig.
2) If-else statement in Java
if-else statement also tests the condition. when if condition is true then print your if body statement, else part statement do not tuch.
Otherwise if part statement is false then else block is executed.
Example1: Leap Year Program
Example2: Odd and Even Program
Using Ternary Operator (? :) to perform the task like if...else statement. If the condition is true, the result of ? is returned. But, if the condition is false, the result of : is returned.
Example Ternary Operator :- Odd and Even Program
3) if-else-if Statement
Multiple statements, executes one condition
Example : Grading system for fail, D grade, C grade, B grade, A grade and A+
4) Nested if statement in Java
Nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.
Example 2 : Leap Year Program, Input by User