Bcp Chapter 03
Bcp Chapter 03
CHAPTER – 03
DECISION STATEMENTS AND CONTROL STRUCTURE
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
For Example: The outer if statement checks if the age is greater than 18, and the inner if
statement, within the true branch of the outer if, checks if the income is greater than
30,000. Write A ‘C’ Program Based on these conditions.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int age = 25;
int income = 50000;
if (age > 18) {
if (income > 30000) {
printf("You are eligible for a loan.\n");
}
else {
printf("Your income is too low for a loan.\n");
}
}
else {
printf("You are not eligible for a loan due to age.\n");
}
getch();
return 0;
}
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
if (condition1)
statement(s)1;
else if (condition2)
statement(s)2;
else if (condition3)
statement(s)3;
.
.
.
else if(conditionN)
statement(s)N:
else
default_statement(s);
From the syntax, it is very clear that only one set of statement(s) will be executed.
depending on the condition being TRUE.
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
For Example: Write a program which asks day number and prints corresponding day name
i.e if input is 5, it will print the fifth day of week which is Thursday. Sunday being the first
day.
Program:
#include<studio.h>
#include<conio.h>
int main();
{
int day:
clrscr();
printf("Enter day number between (1-7)\n");
scanf("%d",&day);
if(day==1)
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
printf("Sunday\n");
else if(day==2)
printf("Monday\n");
else if(day==3)
printf("Tuesday\n");
else if(day==4) printf("Wednesday\n");
else if(day==5) printf("Thursday\n");
else if(day==6) printf("Friday\n");
else if(day==7) printf("Saturday\n");
else printf("Wrong input\n");
getch();
return 0;
}
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
we can do with switch statement can be done by multiple if...else statement, but reverse is
not always true.
For Example: Write a program which asks day number and prints corresponding day name.
Program :
#include<stdio.h>
#include<conio.h>
int main()
{
int day;
clrscr();
printf(“Enter the day number from 1 to 7”);
scanf(“%d”, &day);
switch(day)
{
case 1: printf(“monday”);
break;
case 2:printf(“tuesday”);
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
break;
case 3: printf(“wednesday”);
break;
case 4:printf(“thursday”);
break;
case 5: printf(“friday”);
break;
case 6:printf(“saturday”);
break;
case 7: printf(“sunday”);
break;
default:printf(“wrong input”);
}
getch();
return 0;
}
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
For Example: Write a program to print first N numbers using goto statement.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n;
int i=1;
printf(“Give Integer number: ”);
scanf(“%d”, &n);
loop :
if(i<=n)
{
printf(“number = %d\n”,i);
i++;
goto loop;
}
getch();
return 0:
}
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
while ( i <=n)
{
printf("%d\n",i);
i++;
}
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
.
.
.
In above code segment, variable i is initialized to 1. Then while statement
with condition i <= n is written in brackets. In the body part, there are two statements. The
value of i variable is printed and then incremented. The statements within the loop body
executed till the condition remains TRUE. When the condition becomes FALSE, the while
loop terminates and the control is transferred to next statement after while loop.
For Example: Write a program to print sum of first N integer numbers using while loop.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n, sum = 0, i = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n) {
sum += i;
i++;
}
printf("Sum of first %d integer numbers is: %d\n", n, sum);
getch();
return 0;
}
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
sum -= 1.0 / i;
} else {
sum += 1.0 / i;
}
}
printf("Sum of the series is: %f\n", sum);
getch();
return 0;
}
Difference between while and do-while loop.
While do-While
While loop is pre test loop. do-while is post test loop.
The statements of while loop may not be The statements of do-while loop are
executed even once. executed atleast once.
There is no semi colon(;) given after There is semi colon(;) given after
while(condition). while(condition);
The syntax of while loop is The syntax of do-while loop is as under:-
While(condition) Do
{ {
Statements Statements;
} }while(condition);
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
.
.
In above example, for(i=1;i<=3;i++) is outer for loop. We can say that
control variable for outer loop is 'i'. for(j=1;j<=3;j++) is inner for loop. Variable 'j' is control
variable for inner for loop. For each value of outer loop control variable ie 'i', all the values
of inner loop control variable are tried i.e 'j'.
Following table explains the set of values for variable 'i' and 'j' for above example.
i J
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING
From above table, it is very clear that for i=1, j goes from 1 to 3. When all values of j are
done, I gets next value i.e i=2, again j goes from 1 to 3. Similarly for i=3, j goes from 1 to 3.
For Example: Write a program to print following pattern of stars.
*
**
***
****
*****
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
for (int i = 1; i <= 4; i++){
printf("*\n");
}
getch();
return 0;
}
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING