Conditional statement in C for SEE and NEB
Introduction:
Conditional statements are an essential feature of any programming language. They allow the program to make decisions based on certain conditions, which makes the program more flexible and intelligent. In "C," conditional statements are widely used to control the flow of execution based on the conditions specified in the code. In this article, we will discuss conditional statements in "C" for Secondary Education Examination (SEE) and National Examination Board (NEB) students.
Conditional statements in "C" are used to execute a block of code based on certain conditions. There are two main types of conditional statements in "C": if-else statements and switch statements.
In this case, a statement is executed based on whether a condition is true or false. The following statements are examples of conditional statements.a) The IF statement:
The if statement is used for decision-making. If the condition is true then, it will execute the statement otherwise it will execute an optional statement.
Syntax:
if(condition)
Statement;
Or,
if(condition)
{
Statement;
.................
}
Note: In these types of statements, if the condition is true, then the respective block of code is executed.
Example:
WAP to input age from the keyboard and display whether you can vote or not. (Note: If age is more than equal to 18 display you can vote else you can’t vote)
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int age;
printf("\n Enter your age=");
scanf("%d",&ag);
if(ag>=18)
printf("you can vote");
if(ag<18)
printf("you can't vote");
getch();
}
b) IF……ELSE statement:
It is used to take decisions on the basis of a certain condition. If the condition is true, the statement grouped under ‘if’ runs otherwise statements grouped under ‘else’ executes. Else group is optional.
Syntax:
If(condition)
Statement;
Else
Statement;
Or,
If(condition)
{
True block of statement;
}
else
{
False block of statement;
}
Note: In these types of statements, a group of statements is executed when a condition is true. If a condition is false, then else part statements are executed.
Example:
WAP to input any number display whether that number is odd or even.
Using 'C' Language
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Enter any number=");
scanf("%d",&n);
if(n%2==0)
printf("The number is even");
else
printf("The number is odd");
return 0;
}
c) Nested IF…….ELSE:
If we write an entire if-else statement within the body of the if statement or within the body of the else statement then it is called a nesteed if statement.
Syntax:
if(expression)
statement;
else if(expression)
statement;
……………………
……………………
else
statement;
Example:
WAP to display the greatest number among given three numbers.
Using 'C' Language
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,g;
printf("enter any three no");
scanf("%d,%d.%d",&a,&b,&c);
if(a>b && a>c)
g=a;
else if(b>c)
g=b;
else
g=c;
printf("\n greatest no%d",g);
return 0;
}
d) Switch statement:
This is a multi-directional conditional control statement. It tests whether an expression matches one of the numbers of signed integer constant or character constant values, with the expression given inside the braces accordingly. This can be written as
Syntax:
switch(expression)
{
case constant 1:
statement;
break;
case constant 2:
statement;
break;
……….
…………
…………
default:
statement;
}
Example:
WAP to input any two numbers and display their addition, subtraction, multiplication and division with the help of menu structure.
#include<conio.h>
int main()
{
int a,b,ch,s;
printf("\n Enter any two numbers=");
scanf("%d,%d",&a,&b);
printf("\n 1. Addition");
printf("\n 2. Subtraction");
printf("\n 3. Multiplication");
printf("\n 4. Division");
printf("\n Enter your choice (1-4)");
scanf("%d",&ch);
switch(ch)
{
case 1:
s=a+b;
printf("\n Sum of two numbers=%d",s);
break;
case 2:
s=a-b;
printf("\n The subtracton of two numbers = %d",s);
break;
case 3:
s=a*b;
printf("\n The multiplication of two numbers=%d",s);
break;
case 4:
s=a/b;
printf("\n The division of two numbers=%d",s);
break;
default:
printf("\n Invalid choice try again");
}
return 0;
}
Conclusion:
In conclusion, conditional statements are essential in programming languages like "C" to enable programmers to make decisions based on specific conditions. With if and switch statements, programmers can create more complex and efficient programs that can handle multiple scenarios with ease. Understanding these conditional statements is crucial for anyone looking to become a proficient "C" programmer. With the knowledge of these statements, programmers can create robust programs that meet the needs of their users. So, take the time to master these statements and elevate your programming skills to the next level.
No comments:
Post a Comment