Class 10th
COMPUTER
NOTES
BY Mustafa shahid
St. Francis Grammar School
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #4) By: Mustafa Shahid
============================SHORT QUESTIONS============================
Q01. Differentiate between if condition and if else condition.
If Selection Structure:
The if statement has the following general form / Syntax.
if (conditions)
{
Block of statements
}
If-else Selection Structure:
The if-else statement is used in situation where some code is to be executed if a condition is true and
some other code is to be executed if the condition is false.
The if-else statement has the following general form / Syntax.
if (condition)
{
Block of statements
}
else
{
Block of statements
}
When if-else statement is executed, the condition is evaluated.
Q02. Differentiate between else-if and switch selection structures.
Else-if Selection structures:
The if-else statement is used in situation where some code is to be executed if a condition is true and
some other code is to be executed if the condition is false. The if-else statement has the following
general form / Syntax.
if (condition)
{
}
else
{
Block of statement
When if-else statement is executed, the condition is evaluated.
1|Page
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #4) By: Mustafa Shahid
Switch Selection Structures:
The switch statement has the following general form
Switch (expression)
{
case const-1:
statements;
break;
case const-2:
statements;
break;
.
.
.
default:
statements;
}
The switch statement is similar to the else-if statement. It is used when multiple choices are given and
one choice is to be selected.
Q03. What is nested selection structure?
The selection structure that is within another selection structure is known as nested selection structure.
This is also supported in C language in C language; the programmer can have a selection structure (if, if-
else, else-if or switch statement) within another selection structure.
Q04. Write the following statement using if-else statement.
K = (a+b>20)? A+3*b; a-b; =
if (a+b)>20
K = a+3*b;
else
K = a-b;
Q05. Write the following statement using conditional operator.
If (x>y)
z = (x+y)/3;
else
z = x-5*y;
Ans: z = (x>y)? (x+y)/3: x-5*y;
2|Page
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #4) By: Mustafa Shahid
Q06. What will be the output of the following code?
Int n, count=15; sum=30;
If (n<25)
{ count = count+5;
printf("\nCount = %d",count);
printf("\nSum = %d", sum); }
else
{ count = count-5;
Sum = sum+n;
printf("\nCount = %d",count);
printf("\nSum = %d", sum);
Ans: Count = 10
Sum = 58
Q07. What will be the output of the following code?
charch;
ch='c';
switch(ch)
{ case 'a':
Printf("\nGood Morning! "); break;
case 'b':
printf("\nHave a Nice Day! "); break;
case 'c':
case 'd':
case 'e':
printf("\n Good Bye! "); break;
}
Ans: Good Bye!
3|Page
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #4) By: Mustafa Shahid
============================LONG QUESTIONS============================
Q01. What is control structure? Explain conditional control structure with examples?
Control Structure:
In a programming language, a control statement is an instruction which determines the sequence of
execution of other statements in a program.
Conditional control structure:
Conditional Statement:
A conditional statement is an instruction in a programming language that contains a condition. When a
conditional statement is executed, first the condition is evaluated and then based on the result (true or
false) a particular statement or a set of statements is executed.
Conditional statements of C language are if, if-else else-if and switch
statements.
Structure of IF Statement:
The if statement has the following general form.
if (conditions)
{
Block of statements
}
4|Page
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #4) By: Mustafa Shahid
Structure Of If-Else Statement:
The if-else statement is used in situation have some code is to be executed if a condition is true and
some other code is to be executed if the condition is false. The if-else statement has the following
general form.
if (condition)
{
Block of statements
}
else
{
Block of statements
}
• When if-else statement is executed. The condition is evaluated.
• If the condition is true then the block of statements following if will be executed and the block of
statements following else will be skipped.
• If the condition is false then the block of statements following if will be skipped and the block of
statements following else will be executed.
•If a single statement is to be executed after if or else then braces are not required.
Structure Of If-Else-If Statement:
The else-if is a type of conditional statement that combines more than two conditions. It allows the
programmer to make a decision based on several conditions. The else-if statement has the following
general form.
if(condition-1)
{
Block of statements
}
Else if(condition-2)
{
Block statements
}
Else if(condition-3)
{
Block of statements
.
.
}
Else
{
Block of statements to be executed
When none of the conditions is true,
}
5|Page
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #4) By: Mustafa Shahid
• When this statement is executed, contion-1 is evaluated, if it is true then the block of statements
following if is executed and if it is false, the next condition is evaluated.
• If any condition is true then the following block of statements is executed.
• If none of the conditions is true then the block of statements following else is executed
automatically.
• If a single statement is to be executed after if, else-if or else, instead of set of statement then the
braces are not required.
Switch Statement:
The switch statement has the following general form.
Switch (expression)
{
Case const-1;
Statement;
Break;
Case conts-2;
Statements;
Break;
.
.
Default:
Statements;
}
• The switch statement is similar to the else-if statement. It is used when multiple choices are given and
one choice is to be selected.
Q02. What is the purpose of switch () statement? Explain with help of example.
Switch Statement:
The switch statement has the following general form
Switch (expression)
{
Case const-1;
Statement;
Break;
Case conts-2;
Statements;
Break;
.
.
Default:
Statements;
}
6|Page
COMPUTER SCIENCE FOR 10TH CLASS
(UNIT #4) By: Mustafa Shahid
Purpose of switch statement:
• The switch statement is similar to the else-if statement. It is used when multiple choices are given
and once choice is to be selected.
• When switch statement is executed, the expression is evaluated. Based on the result of expression
one of the cases in the switch statement is executed. The result of expression is compared with the
constant values given after the key word case. If the result matches the constant value after any case,
then the statements under that case are executed.
• In switch statement, it is allowed to use a variable within the parenthesis instead of an expression
based on which statements under a case can be executed.
• The purpose of break statement is to exit the body of the switch statement after executing the
statements under a case and transfer control to the first statement following the end of the switch
statement.
• If no case is matched then the statements under the default keyword are executed. Its use is optional
if it is not used then the central exits from the body of the switch statement and goes to the first
statement following the end of the switch statement.
• The expression should be of type int, char but not float.
• When this program is executed, the switch variable must have an integer value. The value of switch
variable n is compared with the constant values statements following that particular case.
• If the switch variable does not match any of the case constants, control goes the keyword default
which is at the end of the switch statement.
• Notice the use of break statement in this program. It terminates the switch statement when the body
of the statements in a particular case has been executed.
7|Page