Plus One Computer Science Notes Chapter 7 Control Statements

Kerala Plus One Computer Science Notes Chapter 7 Control Statements

Summary
These are classified into two decision making and iteration statements

Decision making statements:
if statement:
Syntax: if (condition)
{
Statement block;
}
First the condition is evaluated if it is true the statement block will be executed otherwise nothing will be happened.

if…else statement:
Syntax: if (condition)
{
Statement block1;
}
else
{
Statement block2;
}

Nested if:
An if statement contains another if statement completely then it is called nested if.
if (condition 1)
{
if (condition 2)
{
Statement block;
}
}
The statement block will be executed only if both the conditions evaluated are true.

The else if ladder:
The syntax will be given below
if (expression1)
{
statement block1;
}
else if (expression 2)
{
statement block 2;
}
else if (expression 3)
{
statement block 3;
}
……..
else
{
statement block n;
}
Here firstly, expression 1 will be evaluated if it is true only the statement blockl will be executed otherwise expression 2 will be evaluated if it is true only the statement block2 will be executed and so on. If all the expression evaluated is false then only statement block n will be executed

switch statement:
It is a multiple branch statement. Its syntax is given below.
switch(expression)
{
case value: statements;break;
case value: statements;break;
case value: statements;break;
case value: statements;break;
case value: statements;break;
………
default: statements;
}
First expression evaluated and selects the statements with matched case value. If all values are not matched the default statement will be executed.

Conditional operator:
It is a ternary operator hence it needs three operands. The operator is “?:”.
Syntax:
expression ? value if true : value if false. First evaluates the expression if it is true the second part will be executed otherwise the third part will be executed.

Iteration statements:
If we have to execute a block of statements more than once then iteration statements are used.

while statement:
It is an entry controlled loop. An entry controlled loop first checks the condition and execute(or enters in to) the body of loop only if it is true. The syntax is given below
Loop variable initialised
while(expression)
{
Body of the loop;
Update loop variable;
}
Here the loop variable must be initialised before the while loop. Then the expression is evaluated if it is true then only the body of the loop will be executed and the loop variable must be updated inside the body. The body of the loop will be executed until the expression becomes false.

for statement:
The syntax of for loop is
for(initialization; checking ; update loop variable)
{
Body of loop;
}
First part, initialization is executed once, then checking is carried out if it is true the body of the for loop is executed. Then loop variable is updated and again checking is carried out this process continues until the checking becomes false. It is an entry controlled loop.

do-while statement:
It is an exit controlled loop. Exit control loop first execute the body of the loop once even if the condition is false then check the condition.
do
{
Statements
} while(expression);
Here the body executes at least once even if the condition is false. After executing the body it checks the expression if it false it quits the body otherwise the process will continue.

Plus One Computer Science Notes