Class VIII, Decision Control Structure
Class VIII, Decision Control Structure
Class VIII, Decision Control Structure
Subject - Computer
Ch- 6 , Decision Control Structure
a. In an if-else statement, if the code in the parenthesis of the if statement is true, the code inside
its brackets is executed. But if the statement inside the parenthesis is false, all the code within
the else statement’s brackets is executed instead.
b. It will execute the very first statement after IF ; If the condition is true. And it will skip the very
first statement after IF if the condition is false.
Ex. If (age>=18)
System.out.println(“You can vote”); // very first statement
System.out.println(“can vote”);
c. {
//Statement
} , We use curly braces to enclose the set of statements.
d. We use ‘if-else-if’ statement when there are multiple options given from which a decision has to
be made. The ‘if-else-if’ statements are executed from the top to down. As soon as one of the
conditions given with ‘if’ is true, the statements 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 is
executed.
e. If the value of expression matches with the constant, then the statements following this
statement execute sequentially till it executes break.
f. The break statement transfers control to the end of the switch statement. If the value of
expression does not match with any constant , the statement with default is executed.
g. Iteration statements repeat a set of statements till a test condition is satisfied.
h. In do-while loop if the condition mentioned is not true initially then the loop will execute once.
i. Jump statements transfer control to other parts of the program .Break and continue are the
jump statements in java.
j. Selection statements- the selection statement tests a condition, then executes one sequence of
statements instead of another, depending on whether the condition is true or false.
Repetition – Repetition allows the execution of a sequence of statements multiple times.
k. Default gets executed in case none of the condition matches the given input. The default
statement is optional.
Ans- Sometimes the program needs to be executed depending upon a particular condition. There we
use if statements.
Syntax of if statement:
if (condition)
{
//Statements to execute
//if condition is true
}
If- else statement – if we want to do one thing if the condition is true and something else if the
condition is false, we use the if-else statement.
Syntax of if-else statement :
if (condition)
{
//Executes this block if condition is true
}
else
{
//Executes this block if condition is false
}
If- else –if statement - We use ‘if-else-if’ statement when there are multiple options given from which a
decision has to be made. The ‘if-else-if’ statements are executed from the top to down. As soon as one
of the conditions given with ‘if’ is true, the statements 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 is executed.
if (condition)
statement;
else if (condition)
statement ;
…
….
else
statement;