0% found this document useful (0 votes)
33 views13 pages

Lec5 Mcas2130

The document discusses Java control statements including if-else-if ladder, switch-case, break, continue, and return. It provides syntax and explanations for each statement across multiple pages.

Uploaded by

Tdm Don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views13 pages

Lec5 Mcas2130

The document discusses Java control statements including if-else-if ladder, switch-case, break, continue, and return. It provides syntax and explanations for each statement across multiple pages.

Uploaded by

Tdm Don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

School of Computing Science and Engineering

Course Code : MCAS2130 Course Name: Programming in Java

UNIT II
Class and Methods

Name of the Faculty: Dr. Avneesh Kumar Program Name: MCA


Control Statements: if-else-if ladder
DSDM framework

• if-else-if ladder:Here, a user can decide among multiple options.The if


statements are executed from the top down. As soon as one of the
conditions controlling the if is true, the statement associated with that if
is executed, and the rest of the ladder is bypassed. If none of the
conditions is true, then the final else statement will be executed.if
(condition) statement; else if (condition) statement; . . else statement;

Program Name: MCA 2


Control Statements: if-else-if ladder
DSDM framework

Program Name: MCA 3


Control Statements: switch-case
DSDM framework

• switch-case The switch statement is a multiway branch statement. It provides an easy


way to dispatch execution to different parts of code based on the value of the
expression.
Syntax: switch (expression)
• { case value1:
• statement1;
• break;
• case value2: statement2;
• break; . .
• case valueN: statementN;
• break; default:
• statementDefault; }

Program Name: MCA 4


Control Statements: switch-case
DSDM framework

• Expression can be of type byte, short, int char or an enumeration.


• Duplicate case values are not allowed.
• The default statement is optional.
• The break statement is used inside the switch to terminate a statement
sequence.
• The break statement is optional. If omitted, execution will continue on into the
next case.

Program Name: MCA 5


Control Statements: switch-case
DSDM framework

Program Name: MCA 6


ControlDSDM
Statements:
framework jump

• jump: Java supports three jump statement: break, continue and return. These
three statements transfer control to other part of the program.
• Break: In Java, break is majorly used for:
• Terminate a sequence in a switch statement (discussed above).
• To exit a loop.
• Used as a “civilized” form of goto.
• Using break to exit a Loop
• Using break, we can force immediate termination of a loop, bypassing the
conditional expression and any remaining code in the body of the loop.

Program Name: MCA 7


ControlDSDM
Statements:
framework jump

• Note: Break, when used inside a set of nested loops, will only break out of the
innermost loop.

Program Name: MCA 8


Control Statements:
DSDM frameworkContinue

• Continue:
• Sometimes it is useful to force an early iteration of a loop. That is, you might
want to continue running the loop but stop processing the remainder of the
code in its body for this particular iteration.
• This is, in effect, a goto just past the body of the loop, to the loop’s end. The
continue statement performs such an action.

Program Name: MCA 9


Control Statements:
DSDM frameworkContinue

Program Name: MCA 10


ControlDSDM
Statements:
framework Return

• Return: The return statement is used to explicitly return from a method. That
is, it causes a program control to transfer back to the caller of the method.

Program Name: MCA 11


References

References:

R. Naughton and H. Schildt – Java2 (The Complete Reference) –


Fifth Edition – TMH – 2004.

Program Name: MCA 12

You might also like