3.Decision statement and Loop
3.Decision statement and Loop
The decision statement helps the programmer to skip the block of instructions
from the execution if the condition is not satisfied.
TYPES OF DECISION STATEMENTS:
I. if statement
II. if-else statement
III. if-else if ladder
IV. switch
}
else
{
}
WORKFLOW:
If the condition is satisfied then the instruction written inside the if block
gets executed if not satisfied else block gets executed (any one of the blocks
will be skipped based on a condition.)
Syntax to create if-else if statement :
if (condition)
{
}
else if (condition)
{
}
else if (condition)
{
}
else
{
}
WORK FLOW:
If the condition is satisfied then the instruction written inside the if block gets
executed if not satisfied, the condition is checked in the else if block from top to
bottom order and if the condition is satisfied in any of the else if block then , only that
else if block is gets executed if not satisfied else block gets executed remaining
blocks are skipped
WORKFLOW :
• The value / variable / expression passed in the switch gets compared
with the value passed in the case from top to bottom order.
• If any of a case is satisfied, the case block is executed and all the blocks
present below get executed.
• If no case is satisfied then the default block gets executed.
• For a case we can use a break statement which is optional.
NOTE:
• For a switch we can’t pass long, float, double, boolean.
• For a case we can’t pass a variable.
BREAK:
• break is a keyword, it is a control transfer statement.
• break is used inside the switch and loop block
• When the break statement is executed control is transferred outside the
block.
ACTIVITY
1. Write a java program to read a number from the user, if the number is
divisible by 5 print lucky else do nothing.
2. Write a java program to read a character from the user, check whether
the character is an Alphabet, or a number, or a special character.
3. Write a java program to read 3 integers from the user and identify the
largest number.
4. Write a java program to find whether the given year is a leap year or
not.
5. Read two integers from the user, display a menu where the user will
have the following option
1. addition
2. subtraction
3. multiplication
4. Division
Users can select any one of the following tasks that task has to be
done and the result must be displayed, if the user selects an invalid
option display please enter valid input.
LOOP STATEMENT
Loop statement helps the programmer to execute the set of instructions
repeatedly
In java we have different types of loop statements, they are:
I. while loop
II. do-while loop
III. for loop
IV. for each / advanced for / enhanced for
V. nested loop
Syntax to create while loop:
while(condition)
{
Statement to be repeated ;
}
WORKFLOW:
CASE 1: When the condition is true
• The loop continues.
• Control execute the statement which belongs to the loop.
• After execution once the loop block ends, control goes back to the
condition and the entire process will be repeated till the condition becomes
false.
CASE 2: When the condition is false
• The loop is stopped i.e. repetition is stopped.
• The loop block will not get executed.
• The control comes outside the loop to the next statement.
//statement
}
while(condition) ;
WORKFLOW :
CASE 1 : When the condition is true
● Control goes to the loop block directly , execute the instructions.
● Then control goes to the condition, if the condition is true the control
goes back to the do block.
CASE 2: When the condition is false
● Control goes to the loop block directly, execute the instructions.
● Then control goes to the condition, if the condition is false the loop is
stops and control goes to the next statement.
WHILE DO - WHILE
First the condition is checked, if the condition In do_while, first the loop block gets executed
is true then the loop block gets executed. and then the condition is checked.
ACTIVITY
1. Write a java program to read a number(m) from the user, and print the natural numbers upto m.
2. Write a java program to read numbers(m and n) from the user, and print the odd numbers.
3. Write a java program to read numbers(m and n) from the user, and count numbers present
between them (excluding m and n).
4. Write a java program to read numbers(m and n) from the user, and obtain the sum of all the
values.
6. Write a java program to print the numbers which are multiples of 3 and 5 between m and n?