|
Explain the following control structures with example.
(a) if statement Ans:- The if statement is of two types: 1. Simple if statement : The simple if statement in which only one statement is followed by that if statement. Sysntax:- if(apply some condition)
Example: if(a>b)
2. Compound if statement : If there are more than one statement that can be executed when if condition is true. Then it is called compound if statement. All these executable statements are placed in curly brackets. Syntax:- if(apply condiotion)
Ststement 2
(b) if else statement Ans:- The syntax for if….else statement will be:
If(condition)
Here if the condition is true then statement 1 will execute otherwise statement 2 will execute. Example:-
if(a>b)
If……else if statement
Example:
if(age==1)
(c) while statement The while loop is Java’s most fundamental looping statement. It repeats a statement or block while its controlling expression is true. Here is its general form:
while(condition) {
The condition can be any Boolean expression. The body of the loop will be executed as long as the conditional expression is true. When condition becomes false, control passes to the next line of code immediately following the loop. The curly braces are unnecessary if only a single statement is being repeated. Example: int count=1;
(d) do…while statement This is similar to while statement but the only difference between the two is that in case of do..while statement the statements inside the do…while must be executed at least once. This means that the statement inside the do….while body gets executed first and then the while condit ion is checked for next execution of the statement, whereas in the while statement first of all the condition given in the while is checked first and then the statements inside the while body get executed when the condition is true. Syntax: do
Example:
int count 1;
(e) switch case The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression.As such, it often provides a better alternative than a large series of if else-if statements.Here is the general form of a switch statement:
switch (expression) {
The expression must be of type byte, short, int, or char; each of the values specified in the case statements must be of a type compatible with the expression. Each case value must be a unique literal (that is, it must be a constant, not a variable). Duplicate case values are not allowed. The switch statement works like this: The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken. The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branches to the first line of code that follows the entire switch statement. This has the effect of “jumping out” of the switch. Here is a simple example that uses a switch statement:
class SampleSwitch
(f) for loop Here is the general form of the for statement:
for(initialization; condition; iteration) {
If only one statement is being repeated, there is no need
for the curly braces. The for loop operates as follows. When the loop first
starts, the initialization portion of the loop is executed. Generally,
this is an expression that sets the value of the loop
Example:
class ForTick
|
|
See also Do you have a Java Problem?Ask It in The Java Forum Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|