Contact Us || Privacy Policy || About Us |
|
Control Statements in JavaWe 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 Javaif 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. Syntax: ![]() Example: Show in Fig. ![]() 2) If-else statement in Javaif-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. Syntax: Example1: Leap Year Program ![]() Example2: Odd and Even Program ![]() Ternary OperatorUsing 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 StatementMultiple statements, executes one condition Syntax Example : Grading system for fail, D grade, C grade, B grade, A grade and A+ ![]() 4) Nested if statement in JavaNested 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. Syntax : Example 1 Output ![]() Example 2 : Leap Year Program, Input by User Output ![]()
Next TopicSwitch Statement in Java
|