This document describes different conditional statements in Java including if-then, if-then-else, if-else-if ladder, and switch statements. The if-then statement executes code if a boolean expression is true, if-then-else adds an else block for when the expression is false, if-else-if ladder allows checking multiple expressions in sequence, and switch compares a variable to case values and executes the matching code block.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views7 pages
Conditional Statement
This document describes different conditional statements in Java including if-then, if-then-else, if-else-if ladder, and switch statements. The if-then statement executes code if a boolean expression is true, if-then-else adds an else block for when the expression is false, if-else-if ladder allows checking multiple expressions in sequence, and switch compares a variable to case values and executes the matching code block.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7
Conditional Statement
• Java if (if-then) Statement
The syntax of if-then statement in Java is: if (expression) { // statements }
• Here expression is a boolean expression (returns either true or false).
• If the expression is evaluated to true, statement(s) inside the body of if (statements inside parenthesis) are executed. • If the expression is evaluated to false, statement(s) inside the body of if are skipped from execution. • Java if...else (if-then-else) Statement • The if statement executes a certain section of code if the test expression is evaluated to true. The if statement may have an optional else block. Statements inside the body of else statement are executed if the test expression is evaluated to false. • The syntax of if-then-else statement is: if (expression) { // codes } else { // some other code } Java if..else..if Statement
• In Java, the if..else..if ladder executes a block of code
among many blocks. The switch statement can a substitute for long if..else..if ladders which generally makes your code more readable. • The syntax of switch statement is: switch (variable/expression) { case value1: // statements break; case value2: // statements break; .. .. ... .. .. ... default: // statements } • The switch statement evaluates it's expression (mostly variable) and compares with values(can be expression) of each case label. • The switch statement executes all statements of the matching case label. • Suppose, the variable/expression is equal to value2. In this case, all statements of that matching case is executed. • Notice, the use of break statement. This statement terminates the execution of switch statement. The break statements are important because if they are not used, all statements after the matching case label are executed in sequence until the end of switch statement.