10th Computer Notes Chapter 4
Chapter 4: Conditional Control Structure

Write short answers to the following questions
I. Differentiate between if and if-else selection structures.
Answer
| IF STATEMENT | IF-ELSE STATEMENT |
| When if statement is executed, the condition is evaluated. If the condition is true then the block of statements within the braces will be executed otherwise control will be transferred to the next statement if any exists. | When if-else the statement is executed, the condition is evaluated. If the condition is true then the block of statements following if will be executed otherwise the block of statements following else will be executed. |
ii. Differentiate between else-if and switch selection structures.
Ans.
- In else-if structure, the control goes through the every else-if statement until it finds true value of the statement or it comes to the end of the else-if structure In case of switch case, as per the value of the switch, the control Jumps to the corresponding case.
- The use of break statement in switch statement is essential but there is no need of use of break in else-if structure.
- Switch statement is considered to be less flexible than the else-if structure because it allows only testing of a single expression against a list of discrete values.
- Switch case statement work on the basis of equality operator, whereas, else-if structure works on the basis of true false basis
iii. What is nested selection structure?
Ans. NESTED SELECTION STRUCTURE
The selection structure that is within another selection structure is known as Nested selection structure. Sometimes, in computer programming, it is required to use a selection structure within another 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.
iv. Write the following statement using if-else statement.
k = (a+b>20)? a+3*b: a-b;
Ans:
If((a+b)>20)
{
K=a+3*b;
}
Else
K=a-b;
}
v. 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
vi. What will be the output of the following code?
int n, count, sum;
n=28; count=15; sum=30;
if (n<25)
{ count=count+5;
printf(“\nCount=%d”, count); }
else
{ count=count-5;
sum=sum+n;
printf(“\nCount=%d”,count);
printf(“\nSum=%d”,sum); }
Answer
Count =10
Sum = 58
vii. What will be the output of the following code?
char ch;
ch=‘c’;
switch(ch)
{ case ‘a’:
printf(“\n Good Morning! “);break;
case ‘b’:
printf(“\n Have a Nice Day! “);break;
case ‘c’:
case ‘d’:
case ‘e’:
printf(“\n Good Bye!”); break;
}
Answer
Good Bye!
Extensive Questions
Q1. What is control structure? Explain conditional control structure with examples.
Answer:
Control Structure
Computer programmers have to make decisions and perform some operations depending on the user input to the program: Control structures are used in programs to implement decisions.
Everybody makes different decisions in the daily life. For example, if my friend is at home then I will visit him. If the weather is good tomorrow, I will go for picnic.
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.
Examples of Conditional Statement Conditional statements of C language are:
- if statement 2. if-else statement.
- else-if statement 4. switch statement
Q2. What is the purpose of switch( ) statement? Explain with the help of one example
Ans:
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. 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 keyword 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.
Example:
char ch;
ch=‘c’;
switch(ch)
{ case ‘a’:
printf(“\n Good Morning! “);break;
case ‘b’:
printf(“\n Have a Nice Day! “);break;
case ‘c’:
case ‘d’:
case ‘e’:
printf(“\n Good Bye!”); break;
}
Select the best answer for the following MCQs.
i. For which purpose if structure is used in programming?
A. Repetition
B. Selection
C. Sequence
D. input of data
ii. Which statement is suitable to use in a situation where there are only two choices based on a condition?
A. if statement
B. if-else-if statement
C. if-else statement
D. switch statement
iii. Which statement can be used in place of switch statement?
A. if statement
B. if-else statement
C. if-else-if statement
D. conditional operator
iv. Which statement can be used in place of conditional operator?
A. if statement
B. if-else statement
C. else-if statement
D. switch statement
v. Which statement is used to exit from the body of switch statement?
A. default
B. continue
C. exit
D. break
vi. Which of the following is a multiple selection statement?
A. if statement
B. if-else statement
C. if-else-if statement
D. none of these
vii. Which of the selection structures tests only for equality?
A. if statement
B. if-else statement
C. else-if statement
D. switch statement
viii. What will be printed when the following code is executed?
x=1;
switch(x)
{ case 1:
case 2:
case 3:
printf(“\n x is a positive number );
break;
default
printf(“\n value of x Is 1”);
}
A. Value of x is 1
B. x is a positive number
C. Nothing will be printed
D. It will give error
Lab Activities
- Write a program that reads a number and prints its square if it is greater than 10.
Answer
Program
# include<stdio.h>
# include<conio.h>
int main()
{
int n;
printf(“Input the number:”);
scanf(“%d”,&n);
if(n>10)
{
printf(“Square Value : %d”, n * n);
}
getch();
}
- Write a program that reads two numbers and prints the larger.
Answer
Program
# include<stdio.h>
# include<conio.h>
int main()
{
int x, y;
printf(“Enter the first value :”);
scanf(“%d”,&x);
printf(“\nEnter the second value :”);
scanf(“%d”,&y);
if(x>y)
printf(“\n\n%d is the largest integer value”,x);
else
printf(“\n\n%d is the largest integer value”,y);
getch();
}
- Write a program that reads two numbers and prints the larger using conditional operator.
Answer
Program
# include<stdio.h>
# include<conio.h>
int main()
{
int x,y,large;
printf(“Enter the first value :”);
scanf(“%d”,&x);
printf(“\nEnter the second value :”);
scanf(“%d”,&y);
large=(x>y)?x:y;
printf(“\n\n%d is the largest integer value”, large);
getch();
}
- Write a program that reads a number and prints a message based in its value as given below.
| Value of n | Message to print |
| n is greater than zero | It is a positive number n is less than zero |
| n is less than zero | It is a negative number |
| n is equal to zero | It is equal to zero |
Answer
Program
# include<stdio.h>
# include<conio.h>
int main()
{
int n;
printf(“Input the number :”);
scanf(“%d”,&n);
if(input ==0)
{
printf(“It is equal to zero”);
}
else if (input>0)
{
printf(“It is a positive number n is less n is less than zero”);
}
else
{
printf(“It is a negative number”);
}
getch();
}
- Write a program that reads temperature in Celsius and prints a message as given below.
| Temperature | Message to print |
| t>35 | It is hot! |
| t≥20, t≤35 | Nice day! |
| t<20 | It is cold! |
Answer
Program
# include<stdio.h>
# include<conio.h>
int main()
{
int t;
printf(“Input the temperature in Celsius :”);
scanf(“%d”,&t);
if(t>35)
{
Printf(“It is hot!”);
}
else if(t>20&&t<35)
{
Printf(“Nice day!”);
}
else if(t<20)
{
Printf(“It is cold!”);
}
getch();
}
- Write a program that reads marks of a subject and prints the letter grade as given below.
| Marks obtained | Grade |
| m≥80 | A |
| m≥60, t<80 | B |
| m≥40, t<60 | C |
| m<40 | F |
Answer
Program
# include<stdio.h>
# include<conio.h>
int main()
{
int marks;
printf(“Marks :”);
scanf(“%d”,&marks);
if(marks>=80)
{
printf(“A”);
}
else if(marks>=60 && marks<80)
{
printf(“B”);
}
else if(marks>=40 && marks<60)
{
printf(“C”);
}
else
{
printf(“F”);
}
getch();
}
- Write a program that reads basic pay of an employee and calculates and prints his net pay as given below.
Net Pay = Basic Pay + House Rent
| Basic Pay | House Rent |
| <25000 | 30% of Basic Pay |
| >25000 and <40000 | 40% of Basic Pay |
| >40000 | 50% of Basic Pay |
Answer
Program
#include<stdio.h>
# include<conio.h>
int main()
{
int basicpay;
clrscr();
printf(“Basic Pay :”);
scanf(“%d”,&basicpay);
if(basicpay<25000)
{
printf(“Net Pay: %d”,basicPay + (basicPay*30/100) );
}
else if(basicPay>25000 && basicPay<40000)
{
printf(“Net Pay: %d”,basicPay + (basicPay*40/100));
}
else if(basicPay>40000)
{
printf(“Net Pay: %d”,basicPay + (basicPay*50/100));
}
getch();
}
ythis is the best website
Thank you