BCP Unit3 Wordpress PDF
BCP Unit3 Wordpress PDF
UNIT: 3
Decision Statements and Control Structure
Decision Statements
Conditional branching statements:
Simple if statement:
Syntax:
if (condition)
{
Statement (s)
}
If the given condition is true then the statements inside the body of “if” will be executed. If the condition
is false then the statements inside the body of “if” will be skipped.
#include<stdio.h>
#include<conio.h>
void main()
{
int marks=45;
if( marks < 35 )
{
printf(“Student is fail”);
}
getch();
}
If-else statement:
Syntax:
if(condition)
{
Statement 1;
}
else
{
Statement 2;
}
True False
Condition?
Statement- 1 Statement- 2
Statement - X
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int marks=45;
if( marks < 35 )
{
printf(“Student is fail”);
}
else
{
printf( “Student is pass” );
}
getch();
}
if (condition1)
{
if (condition2)
stmt1;
else
stmt2;
}
else
{
if (condition3)
stmt3;
else
stmt4;
}
Example:
#include<stdio.h>
#include<conio.h>
void main (){
int a,b,c;
printf("Enter the values of a,b,c: ");
scanf("%d,%d,%d",&a,&b,&c);
if((a>b)&&(a>c))
{
if(a>c)
{
printf("%d is the largest",a);
}
else
{
printf("%d is the largest",c);
}
}
else
{
if(b>c)
{
printf("%d is the largest",b);
}
Prepared By: Department of Computer Engineering Page 4
Subject Name: Basic Computer Programming Unit No: 03 Subject Code: 4310702
else
{
printf("%d is the largest",c);
}
}
if else if…ladder
Syntax:
if(condition-1)
{
Statement - 1;
}
else if(condition-2)
{
Statement - 2;
}
else if(condition-3)
{
Statement - 3;
}
.
.
.
else if(condition-N)
{
Statement -N;
}
else
{
Default statement;
}
3. This procedure repeated until all the condition is checked. if all the condition became false then the
default statement is executed.
Flowchart:
True
Condition 1? Statement - 1
False
True
Condition 2? Statement - 2
True
Condition 3? Statement - 3
.
.
. True
Condition N? Statement - N
Default Statement
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int no;
printf(“enter number\n”);
scanf(“%d”,&no);
if( no > 0 )
{
printf(“Number is Positive”);
}
else if (no < 0)
{
printf( “Number is Negative” );
}
else
{
printf( “Number is Zero” );
}
getch();
}
Switch statement:
It is used to select one among multiple decisions.
The switch statement is also known as multi-choice or multi decision statement.
„switch‟ successively tests a value against a list of integers or character constant.
When a match is found, the statement(s) associated with that value is executed.
Break statement send the control to the next statement after switch statement.
If the case not found then the statement associated with the default case is executed.
Syntax:
Example:
void main()
{
int a=2;
clrscr();
switch(a)
{
case 1:
printf(“One \n”);
break;
case 2:
printf(“Two”);
break;
case 3:
printf(“Three”);
break;
default:
printf(“Wrong choice”);
getch();
}
O/p: Two
Syntax:
goto label ;
…………. Statement;
…………. ………
Label: ………..
Target statement comes after the goto. Target statement comes before the goto.
Control Statements:
When we want to repeatedly run a particular statement until a particular condition is met, then in that case we
use Loop Statements.
While loop
Do while loop
For loop
While loop:
Syntax:
while (Condition)
{
Statements;
}
Example:
i =1;
Do-While loop:
Syntax:
do
{
// statements inside the loop
}
While (condition);
Example:
i =1;
do
{
Prepared By: Department of Computer Engineering Page 10
Subject Name: Basic Computer Programming Unit No: 03 Subject Code: 4310702
printf( “% d \n ” , i );
i = i+1;
} while (i < = 10);
For loop :
In For Loop, we write both the initialization and control condition of the variable together inside
the parentheses “()”.
If the condition of For Loop is true, then the statement inside it is run, or else the statement is not
run.
Syntax:
Initialization
False
Condition?
True
Increment/
Decrement Body
Syntax:
Example:
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
Break statement:
It terminates the loop or switch statement and transfers execution to the statement immediately
following the loop or switch.
Keyword used is "break".
Break from loop:
for( ; ; )
{
for( ; ; )
{
Prepared By: Department of Computer Engineering Page 12
Subject Name: Basic Computer Programming Unit No: 03 Subject Code: 4310702
if(condition)
break;
}
--------------------
}
Example;
for(i=1; i<=5 ; i++)
{
if(i==3)
break;
printf("%d\n",i);
}
Output: 1 2
Continue statements :
It causes the loop to skip the remainder of its body and starts the next iteration.
Keyword used is "continue".
for( ; ; )
{
--------------
if( condition)
continue;
------------
Example: