Introduction to Programming - (C Language) - Unit : 2 - Control Structures, Conditional statements
In C programming, control statements are used
to alter the flow of execution of a program based on certain conditions. They
allow you to make decisions, loop through code blocks, and jump to specific
parts of your program.
1.
Conditional Statements
2.
Branching/Selection Statements
3.
Looping/Iterative Statements
CONDITIONAL STATEMENTS
The if statement in C language is used to
perform operation on the basis of condition. By using if-else statement, you
can perform operation either condition is true or false.
There are many ways to
use if statement in C language:
1. If statement
2. If-else statement
3. If else-if ladder
4. Nested if
1.
If Statement
The single if statement
in C language is used to execute the code if condition is true. The syntax of
if statement is given below:
if(conditional
expression)
{
//code to be executed
}
Fl flow chart of if statement in C
Let's see a simple
example of c language if statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int n
= 0;
clrscr();
printf("enter a number:");
scanf("%d",
&nr);
if(n % 2 == 0)
{
printf("%d is even number", n);
}
getch();
}
Output
Enter a number:4
4 is even number
enter a number:5
2. If-else Statement
The if-else statement in
C language is used to execute the code if condition is true or false. The
syntax of if-else statement is given below:
if(conditional
expression)
{
//code to be executed if condition is true
}
else
{
//code to be executed if condition is false
}
Flowchart of if-else statement in C
Let's see the simple example of even and odd number using if-else statement in C language.
#include<stdio.h>
#include<conio.h>
void main()
{
int number=0;
clrscr();
printf("enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
else
{
printf("%d is odd number",number);
}
getch();
}
Output
Enter a number:4
4 is even number
enter a number:5
5 is odd number
List of programs using if else
1. Find whether the given number is positive or negative
2. Find the greatest of two numbers
3. Find the eligibility based on age (condition : age >= 18)
4. Find the result (pass or fail) based on marks (condition : marks >= 40)
5. Find the result (pass or fail) based on 4 subject marks
6. Find the Net bill to paid after deducting the discount
Total bill >= 50000 – discount is 10%
Total bill < 50000 – discount is 5%
7. Find the power bill based on consumed units
Units >= 500 – cost per unit is 5/-
Units < 500 – cost per unit is 3/-
8. Calculate the tax amount based on salary
Salary >= 40000 – tax 5%
Salary <40000 – No tax
2.
If else-if ladder Statement
The if else-if statement
is used to execute one code from multiple conditions. The syntax of if else-if
statement is given below:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
else
{
//code to be executed if all the conditions are false
}
Flowchart of
else-if ladder statement in C
Example
Program:
#include<stdio.h>
void main()
{
int number=0;
clrscr();
printf("enter a number:");
scanf("%d",&number);
if(number==10)
{
printf("number is equals to 10");
}
else if(number==50)
{
printf("number is equal to 50");
}
else if(number==100)
{
printf("number is equal to 100");
}
else
{
printf("number is not equal to 10, 50 or 100");
}
getch();
}
Output
Enter a number:4
Number is not equal to 10, 50 or 100
Enter a number:50
Number is equal to 50
List of Programs on if else-if else
ladder:
1. Print
day name using day number
2. Print
Grade using percentage
(percentage >= 90 – A+
percentage >= 75 – A
percentage >= 60 - B
percentage >= 50 – B
percentage >= 40 – C
Percentage <40 - F
3. Wishes
based on given time (Good Morning, Good afternoon, Good Evening, Good Night)
4. Calculate
Net Bill amount after deducting discount amount from Total bill
Bill >= 100000 – Discount is 15%
Bill >= 75000 – Discount is 8%
Bill >= 50000 – Discount is 5%
Bill >= 30000 – Discount is 3%
Bill >= 15000 – Discount is 2%
Bill >= 10000 – Discount is 1%
Bill < 10000 – Discount is 0%
5. Calculate Power bill based on consumed units
Units >=
2000 – cost per unit is 12/-
Units >=
1500 – cost per unit is 10/-
Units >=
1000 – cost per unit is 8/-
Units >=
700 – cost per unit is 6/-
Units >=
500 – cost per unit is 5/-
Units >=
300 – cost per unit is 4/-
Units >=
200 – cost per unit is 3/-
Units >=
100 – cost per unit is 2/-
Units < 100 – Fixed bill amount – 75/-
4.
Nested if statement
In C programming, nested if statements allow
you to place one if statement inside another if statement. This enables you to
create more complex conditions and control the flow of your program based on
multiple conditions. Here's the syntax for a nested if statement:
if (condition1) {
//
code to be executed if condition1 is true
if
(condition2) {
// code to be executed if both condition1 and condition2 are true
}
else {
// code to be executed if condition1 is true and condition2 is false
}
}
else {
//
code to be executed if condition1 is false
}
In a nested if statement, the inner if
statement is executed only if the condition in the outer if statement is true.
The inner if statement can have its own set of conditions and code blocks.
Nested if statements allow you to create more
intricate conditional logic by nesting multiple levels of conditions and code
blocks.
Example Program
#include <stdio.h>
#include <conio.h>
void
main() {
int number1, number2;
clrscr();
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2) {
if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);
}
}
else {
printf("Result: %d < %d",number1, number2);
}
getch();
}
Output:
Enter two integers: 20
10
Result: 20 > 10
List
of Programs on nested if else
1.
Biggest
of three numbers
2.
Biggest
of four numbers
3.
Create
a online payment process for phonepe.
Comments
Post a Comment