[go: up one dir, main page]

0% found this document useful (0 votes)
3 views19 pages

Bcp Chapter 03

This document covers decision statements and control structures in C programming, including nested if statements, if...else...if ladders, switch statements, and unconditional branching with goto. It also explains loops such as while, do...while, and for loops, along with their syntax and examples. Additionally, it discusses nesting of loops and provides sample programs for each concept.

Uploaded by

mrsseju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

Bcp Chapter 03

This document covers decision statements and control structures in C programming, including nested if statements, if...else...if ladders, switch statements, and unconditional branching with goto. It also explains loops such as while, do...while, and for loops, along with their syntax and examples. Additionally, it discusses nesting of loops and provides sample programs for each concept.

Uploaded by

mrsseju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

BASIC COMPUTER PROGRAMMING

CHAPTER – 03
DECISION STATEMENTS AND CONTROL STRUCTURE

1.3 Nested If Statement


For complex problems we need to check more than one condition. Inside if
statement, we can have another if statement. This type of use of if statement is called as
nested if statement. The nesting can be up to any level.
The flowchart for finding largest of three numbers as shown in figure.

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

1.4 If...else...if ladder (Multiple if...else) statement


The if...else statement provides two-way decision. The two-way decision
provided by if...else statement is not sufficient when we are having many choices, 'C'
language provides a multiple if...else statement for that purpose.

The syntax is:

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

This Flow chart explains multiple if...else statement.

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

1.5 Switch Statement.


"C" language provides a switch statement. In a complex problem, if
many alternative values are available, then writing the code using multiple if...else becomes
lengthy and also difficult to manage. At that time switch statement which is a multi-way
decision statement is handy, because management of code becomes easy.
The syntax of switch statement is:
switch (variablename or expression)
{
case value1 : statement(s)1;
break;
case value2 : statement(s)2;
break;
.
.
.
case valueN : statement(s)N;
break;
default : default_statement(s);
}

Here, statement(s)1 will be executed if the value of variablename or


expression is value1, similarly statement(s)2 will be executed if the value of variablename
or expression is value2. If the value of variablename or expression does not match any
values from valuel to valueN, then the default_statement(s) are executed.
It is to be noted that writing the default value and its corresponding
default_statement(s) is optional. The value of expression or variable written after the
switch statement must be either character or integer value.
In multiple if...else statement, we have many conditions, while in switch
statement, we have only one expression (which has only one value), depending on the
value of expression different set of statements are executed. It also means that whatever

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.

This Figure is a flow chart which explains the switch statement.

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

1.6 Unconditional Branching goto Statement.


The control structures we have studied in this chapter like if, if...else,
multiple if...else, switch statement provide branching in program which is conditional. "C"
language also provides unconditional branching mechanism called as goto statement.
Structured programming practice does not encourage the use of goto statement, because
use of goto statement in the program makes it difficult to understand and debug. 'C'
provides the control structures using which we can always write a program without using
goto statement.
The syntax is:
goto label;
where, label is a valid 'C' identifier followed by colon (:) symbol. After the colo there can be
any valid 'C' statement like.
label: statement;
The label referred in the goto statement can be declared before or after the g statement.
For example,
goto label;
.
.
.
label: statement;
is called as forward reference.
For example,
label: statement;
.
.
.
goto label;
is called as backward reference.

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

2.1 While Loop.


While loop is helpful, when we do not know in advance, how many times
the statements are to be repeated.
The syntax of while loop is:
while (condition)
{
statement(s);
}
The statements within the symbols are known as body part of the loop. The
() are not required if there is only one statement in the body part. Here, the condition is
checked first and then only the statements are executed. So, while loop is entry controlled
loop, its flowchart is same as entry control loop shown above.
The statements in the body part will be executed till the condition is TRUE.
As soon as the condition becomes false, the control will come out of the while loop. If the
condition remains TRUE always then, it will lead to infinite loop. So, writing a valid condition
is important.
Look at the following code segment which prints first n numbers:
.
.
.
i = 1;

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

2.2 Do…While Loop.


do...while is an exit control loop, where the condition is checked later.
The flowchart is same as that for exit control loop. At least once the body of the loop will
be executed. Whenever you want to make sure that loop statements must execute at least
one time, you should use do...while loop instead of while loop.
The syntax is:
do
{
statement(s);
}while (condition);
Here, the statement(s) are executed till the condition is TRUE. As soon as
the condition evaluates to FALSE, the loop terminates and the next statement after
do...while loop is executed.
We can use do...while loop wherever we have used while loop. It means
that all the program written in previous section using while loop can always be written
using do...while loop.
For Example: Write a program to compute the sum of following series 1-1/2 + 1/3-....+ 1/n.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n;
float sum = 0.0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {

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

2.3 For Loop.


We have seen in previous section that when we do not know number of
iterations required, we can use while or do...while loop. But, in certain cases, we need to
execute some steps certain number of times. In such situations where we know the number
of iterations in advance, then we can use for loop.
The syntax of for loop is :
for(initialization; condition; increment/decrement)
{
statement(s);
}
We require one variable which is used to control the loop, which is called as
control variable. The control variable is initialized in initialization expression. This statement
executes only once. The condition expression actually checks the value of control variable.
If the condition expression is TRUE, then the statements written are executed. These
statements (also called as body of for loop) if they are more than one, than put in (1.
otherwise for one statement. After the every execution of statements, the last expression
increment/decrement is executed. Then again the condition is checked and body is
executed if the condition evaluates to TRUE. Loop terminates when the condition evaluates
to FALSE.

DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING

The Above Figure Explains Flowchart Of For Loop.


For example, following for loop will print the numbers 1 to 10 each on separate line.
.
for(i=1;i<=10,i++)
printf("%d\n",i);
.
.
Here, i=1; statement is initialization which will execute only once. Then the second
expression i<=10 will be evaluated. It true, so loop body will execute i.e. printf() statement
will be executed, then i++ statement will execute, which will make i=2, then again the
condition i<=10 will be evaluated, which is true, so again printf() statement will be
executed. This sequence will go on till the condition is TRUE, when I becomes 11, at that
time condition evaluates to FALSE, so loop is over.
For Example: Write a program to print a number and its square, cube for numbers 1 to 10.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int i;
printf("Number\tSquare\tCube\n");
for (i = 1; i <= 10; i++) {
printf("Number: %d, Square: %d, Cube: %d\n", i, i * i, i * i * i);
}
getch();
return 0;
}

DIPLOMA/VDU/SEM1/BCP/SINGH RITESH
BASIC COMPUTER PROGRAMMING

2.4 Nesting Of Loops.


When a loop inside another loop is used, it is called nesting of loops. The
nesting can be for any number of levels. For certain problems, nesting of loops are needed.
When nesting is done, some care is required. The outer loop should not end between the
starting of inner loop and ending of inner loop.
Following example shows nesting up to 2 levels.

.
.
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

2.5 Break Statement.


We have used the break statement in switch statement. It causes an exit
from the switch body. If it is not written after each case statement, then control passes to
the next statement, so remaining statements of next case will also execute even if the case
value do not match and the program will not function properly.
Try this example, where the break statement is omitted. It will not work as per our
requirement.
For Example: Program demonstrating switch statement without break statement.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int option = 2;
switch (option) {
case 1:
printf("Option 1 is selected. ");
case 2:
printf("Option 2 is selected. ");
case 3:
printf("Option 3 is selected.");
default:
printf("Invalid option.");
}
getch();
return 0;
}
DIPLOMA/VDU/SEM1/BCP/SINGH RITESH

You might also like