Class VIII, Decision Control Structure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Class – VIII

Subject - Computer
Ch- 6 , Decision Control Structure

Q4- Distinguish between the following pairs:

a. ‘While ‘ loop and ‘do…..while’ loop


While Loop Do … Whilel loop
1.While loop is entry control loop. 1. Do… While loop is exit control loop.
2.In while loop the loop condition is tested 2.In DO… While loop the condition is tested at
before executing the loop body. the end of the loop. Do…While loop is
executed at least one time.
3. Syntax of While loop- 3. Syntax of DO… While loop
While (Test condition) do
{ {
Loop body Loop body
} }
While(test condition);

b. Break and continue statement:


Break Statement Continue statement
1. Break statement terminates the loop 1. Sometimes we want to force an early
or switch statement and transfers iteration of a loop. In such a case we
execution to the statement use the continue statement.
immediately following the loop or
switch.
2. Using break we can force immediate 2.It causes the loop to skip the remainder
termination of a loop, bypassing any of its body and immediately retest the
remaining code in the body of the condition prior to reiterating.
loop.

Q5- Very short answer type questions:-

a. The three basic control structures are –


1. Sequential control structure
2. Selection control structure
3. Iteration(looping) control structure
b. Java provides the following for implementing the selection control structure.
1. If statement
2. If-else statement
3. If-else-if (nested if) statement
4. Switch statement
c. For loop and While loop are two entry control loop.
d. It will not execute the loop.

Q6- Short Answer type questions:-

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.

Q7 & Q8 are in objectives pdf

Q9- Long answer type questions:

Ans- Sometimes the program needs to be executed depending upon a particular condition. There we
use if statements.

If statement- It is a decision statement. It is used to decide whether a certain statement or block of


statements will be executed or not. If a certain condition is true, then a statement or block of
statements is executed otherwise not.

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.

Syntax of if-else-if statement:

if (condition)
statement;
else if (condition)
statement ;

….
else
statement;

You might also like