0% found this document useful (0 votes)
100 views

All Subjects Free PDF Educational Books, Notes, Past Papers Etc. Website:, E-Mail

The document discusses different types of conditional control structures in C programming language including if, if-else, else-if, switch statements. It provides examples to explain the syntax and usage of each structure. Key differences between if/if-else and switch statements are outlined. Nested conditional structures are also described.

Uploaded by

Kss Brothers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

All Subjects Free PDF Educational Books, Notes, Past Papers Etc. Website:, E-Mail

The document discusses different types of conditional control structures in C programming language including if, if-else, else-if, switch statements. It provides examples to explain the syntax and usage of each structure. Key differences between if/if-else and switch statements are outlined. Nested conditional structures are also described.

Uploaded by

Kss Brothers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Download All Subjects Free PDF Educational Books, Notes, Past Papers etc.

Website: www.easymcqs.com , E-mail: [email protected]


TH
COMPUTER SCIENCE FOR 10 CLASS (UNIT # 4)
============================================================
SHORT QUESTIONS

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 condi ion is false.
The if-else statement has the following general form / Syn ax.
if (condition)
{
Block of statements
}
else
{
Block of statements
}

Differentiate between else-if and switch selection structures.


Else-if Selection structures:
The if-else statement is used in situ tion where some code is to be executed if a
condition is true and some other c de is to be executed if the condition is false.
The if-else stateme t has the f llowing general form / Syntax.
if (condition)
{
Block of statement
}
else
{
Block of statement
}
When if-else statement is executed, the condition is evaluated.
S itch Selection Structures:
The s itch statement has the following general form
S itch (expression)
{
case const-1:
statements;
break;
case const-2:
statements;
============================================================
Visit www.easymcqs.com for Notes, Old Papers, PDF, IT Courses & more (Page 1 of 7)
Download All Subjects Free PDF Educational Books, Notes, Past Papers etc.
Website: www.easymcqs.com , E-mail: [email protected]
TH
COMPUTER SCIENCE FOR 10 CLASS (UNIT # 4)
============================================================
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.

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.

Program:
When this program is executed, it reads an integer number, stores it in the
variable n and then the condition (n>0) is evaluated.
If it is true, it prints the message that the entered number is a positive number
and then the nested if-else statement is exe uted.
The nested if-else statement prints whether n is an even number or an odd
number.
If the condition (n>0) is false then the statements following the first if are skipped
and the last statement after the else is executed which prints the message that the
user entered a negative number r zero.

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;
============================================================
Visit www.easymcqs.com for Notes, Old Papers, PDF, IT Courses & more (Page 2 of 7)
Download All Subjects Free PDF Educational Books, Notes, Past Papers etc.
Website: www.easymcqs.com , E-mail: [email protected]
TH
COMPUTER SCIENCE FOR 10 CLASS (UNIT # 4)
============================================================
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;

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

What will be the output of the following code?


charch;
ch=‟c‟;
switch(ch)
{case „a‟:
Printf(“\nGood Mor i g! “); break;
Case „b‟:
printf(”\nHave a Nice Day! “); break;
case „c‟:
case „ ‟:
case „e‟:
printf(“\n Good Bye! “); break;
}

Ans: Good Bye!

============================================================
Visit www.easymcqs.com for Notes, Old Papers, PDF, IT Courses & more (Page 3 of 7)
Download All Subjects Free PDF Educational Books, Notes, Past Papers etc.
Website: www.easymcqs.com , E-mail: [email protected]
TH
COMPUTER SCIENCE FOR 10 CLASS (UNIT # 4)
============================================================
LONG QUESTION

What is control structure? Explain conditional control structure with


examples.
Control Structure:
In a programming language, a control statement is an instruction which deter ines
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, fir t the condition is
evaluated and then based on the result (true or false) a particular tat ment 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
}

Use of If Statement:
Program 1: The program in Fig, demonstrates the use of if statement.

============================================================
Visit www.easymcqs.com for Notes, Old Papers, PDF, IT Courses & more (Page 4 of 7)
Download All Subjects Free PDF Educational Books, Notes, Past Papers etc.
Website: www.easymcqs.com , E-mail: [email protected]
TH
COMPUTER SCIENCE FOR 10 CLASS (UNIT # 4)
============================================================
Structure Of If-Else Statement:
The if-else statement is used in situation hwre 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 evalua d.
If the condition is true then the block of statements f ll wing if will be executed
and the block of statements following else will be skipped.
If the condition is false then the block of stateme ts 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 f statements
}
Else if(con ition-3)
{
Block of statements
.
.
.
}
Else
{
Block of statements to be executed
When none of the conditions is true,
}

============================================================
Visit www.easymcqs.com for Notes, Old Papers, PDF, IT Courses & more (Page 5 of 7)
Download All Subjects Free PDF Educational Books, Notes, Past Papers etc.
Website: www.easymcqs.com , E-mail: [email protected]
TH
COMPUTER SCIENCE FOR 10 CLASS (UNIT # 4)
============================================================
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 f 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 simi ar to the else-if statement. It is used when multiple
choices are given and o e ch ice is to be selected.

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;
}
============================================================
Visit www.easymcqs.com for Notes, Old Papers, PDF, IT Courses & more (Page 6 of 7)
Download All Subjects Free PDF Educational Books, Notes, Past Papers etc.
Website: www.easymcqs.com , E-mail: [email protected]
TH
COMPUTER SCIENCE FOR 10 CLASS (UNIT # 4)
============================================================
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 con rol o he first statement
following the end of the switch statement.
If no case is matched then the statements u der the default keyword are
executed. Its use is optional if it is not used then the co trol exits from the body of
the switch statement and goes to the fir t tatement following the end of the
switch statement.
The expression should be of type int, ch r but not float.

When this pr gram 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 s itch variable does not match any of the case constants, control goes the
key ord 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.

============================================================
Visit www.easymcqs.com for Notes, Old Papers, PDF, IT Courses & more (Page 7 of 7)

You might also like