Bca C102 SM04
Bca C102 SM04
Unit – 𝚰𝚰
C – conditional statements allow you to
make a decision based upon the result of a
condition. These statements are called
Decision Making Statements or Conditional
Statements.
The Flow Chart of decision making
technique is –
Start
Expression
true false
Statements
Stop
1|Page
Branching Statements –
Branching Statements in C helps to control
the flow of execution of the program
according to our requirements. These
statements plays a vital role in C language
to jump from one part of program to
another on the basis of some conditions.
Types of Branching Statements :
Conditional Branching Statements :
Conditional Branching Statements in C are
used to execute the specific blocks of code on
the basis of some condition (as per the
requirements).
Syntax –
if(condition)
{
True statement-block ;
}
statement-x;
Example :
# include<stdio.h >
void main( )
{
int number;
printf (“Enter any number: ”);
scanf (“%d”, & number);
if (number > 0)
{
printf(“%d is a Positive Number\n ”,number);
}
3|Page
return 0;
}
Output :
Enter any number : 2
2 is a Positive Number
Syntax –
if (condition)
{
// statements
}
else
{
// code executed if condition
4|Page
is false
}
Example :
# include<stdio.h >
void main( )
{
int number;
printf (“Enter any number: ”);
scanf (“%d”, & number);
if (number > 0)
{
printf(“%d is a Positive Number\n ”,number);
}
else
{
printf (“% d is a Negative Number ”, number);
}
return 0;
}
Output :
Enter any number : -5
-5 is a Negative Number
5|Page
else-if Statement – It is used to check
multiple conditions. It follows an "if"
statement and is executed if the previous
"if" statement’s condition is false.
Syntax –
if (condition 1)
{
// code to be executed if
condition1 is true
}
else if (condition 2)
{
// code to be executed if
condition2 is true and
condition1 is false
}
Example :
# include<stdio.h >
void main( )
{
int number;
printf (“Enter any number: ”);
scanf (“%d”, & number);
6|Page
if (number > 0)
{
printf(“%d is a Positive Number\n ”,number);
}
else if (number%2==0)
{
printf(“%d is an Even Number\n”,number);
}
else
{
printf (“% d is a Negative Number ”, number);
}
return 0;
}
Output :
Enter any number : 4
4 is an Even Number
Syntax –
7|Page
switch (expression)
{
case value1:
// code to be executed if expression
equals value1
break;
case value2:
// code to be executed if expression
equals value2
break;
...
default:
// code to be executed if none of the
cases match
break;
}
Example :
#include <stdio.h>
int main() {
int x = 2;
switch (x) {
case 1:
printf("x is 1");
break;
case 2:
8|Page
printf("x is 2");
break;
case 3:
printf("x is 3");
break;
default:
printf("x is not 1, 2, or 3");
break;
}
return 0;
}
Output :
x is 2
9|Page
goto Statement – The "goto" statement
is an unconditional branching statement
that allows programmers to jump to a
specific labeled statement within their
code.
Syntax –
goto label;
...
label:
// code to be executed
Example :
#include <stdio.h>
int main()
{
int x = 0;
start:
x++;
10 | P a g e
if (x < 5)
{
goto start ; //Jumps to start
}
printf("x is %d", x);
return 0;
}
Output :
x is 5
Syntax –
break;
Example :
#include <stdio.h>
int main()
{
11 | P a g e
int x = 0;
while (x < 5)
{
x++;
if (x == 2)
{
break;
}
}
printf ("x is %d", x);
return 0;
}
Output :
x is 2
Syntax –
12 | P a g e
continue;
Example :
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // skip even numbers
}
printf("%d ", i);
}
return 0;
}
Output :
1 3 5 7 9
13 | P a g e
A loop statement allows us to execute a
statement or group of statements multiple
times
Types of Loops :
for loop : A for loop begins with the for
keyword and a statement declaring the three
parameters governing the iteration, all
separated by semicolons (;) .
Syntax –
for (initialization; condition; increment)
{
// Code to be executed repeatedly
}
14 | P a g e
Example :
#include<stdio.h>
int main()
{
int i ;
for ( i = 0 ; i < 5 ; i + + )
{
printf (“ %d\t ”,i );
}
return 0;
}
Output :
1 2 3 4
Output :
1 2 3 4
16 | P a g e
loop ends when the condition evaluates to
false.
Syntax –
do
{
// Code to be executed at least once.
} while ( condition ) ;
Example :
#include<stdio.h>
int main()
{
int i = 0 ;
do
{
printf (“ %d\t ”,i );
i++;
} while ( i < 5 ) ;
return 0;
}
Output :
0 1 2 3 4
17 | P a g e
Conditional Operator –
The Conditional Operator consists of a
condition followed by two statements or
expressions. If the condition is true then the
first expression is executed and if it is false then
second expression is executed.
Syntax :
testCondition ? expression1 : expression 2;
Example :
#include <stdio.h>
int main()
{
int age;
// take input from users
18 | P a g e
(age >= 18) ? printf("You can vote") : printf("You cannot
vote");
return 0;
}
Output :
Enter your age: 12
You cannot vote
Array in C –
An array is defined as the collection of
similar type of data items stored at
contiguous memory locations. Arrays are
the derived data type in C programming
language which can store the primitive
type of data such as int, char, double, float,
etc.
19 | P a g e
Properties of Array –
Declaration of Array –
data_type array_name[array_size];
Example :
#include<stdio.h>
int main()
20 | P a g e
{
int i=0;
int marks[5]; //declaration of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \t",marks[i]);
} //end of for loop
return 0;
}
Output :
80 60 70 85 75
Types of Array in C –
21 | P a g e
Arrays are classified into two types based
on their dimensions :
Syntax :
data_type array_name[array_size];
Example :
#include <stdio.h>
int main()
{
int arr[5] = {7, 8}; //single dimensional array
22 | P a g e
Output :
78000
Syntax :
Datatype_array_name[sizeof_1st_dimension]
[sizeof_2nd_dimension].....[sizeof_nth_dimen
sion];
Syntax :
Datatype_array_name[sizeof_1st_dimension]
[sizeof_2nd_dimension];
23 | P a g e
Example :
#include <stdio.h>
int main()
{
int arr[2][2]= { {1, 2}, {3,4} }; //two dimensional array
Output :
1 2
3 4
Syntax :
24 | P a g e
Datatype_array_name[sizeof_1st_dimension]
[sizeof_2nd_dimension][sizeof_3rd_dimension];
Example :
#include <stdio.h>
int main()
{
int arr[2][2][2]; //three dimensional array
printf(“Enter the elements : ”);
for (int i = 0; i < 2; i++)
{
for(int j=0; j<2; j++)
{
for(int k=0; k<2; k++)
{
scanf(“%d”,&arr[i][j][k]); // taking input
}
}
}
printf(“Elements are : \n”);
for (int i = 0; i < 2; i++)
{
for(int j=0; j<2; j++)
{
for(int k=0; k<2; k++)
{
25 | P a g e
printf(“%d\t”,arr[i][j][k]); // displays output
printf(“\n”);
}
}
}
return 0;
}
Output :
Enter the elements : 1
2
3
4
5
6
7
8
Elements are :
1 2
2 4
5 6
7 8
26 | P a g e
Strings in C –
A character array terminated by a ‘\0’ ( null
character ). Null character in a string
denotes string termination which means
there is no element lying after the null
character.
When the compiler encounters a sequence
of characters enclosed in the double
quotation marks, it appends a null
character \0 at the end by default.
Declaration of String –
Initialization of Strings –
27 | P a g e
char c[5] = {'a', 'b', 'c', 'd', '\0'};
Example :
#include <stdio.h>
int main()
{
char name[20];
28 | P a g e
printf("Enter name: ");
scanf("%s", name); //reads string
printf("Your name is : %s", name);
return 0;
}
Output :
Enter name: Dennis Ritchie
Your name is Dennis.
Example :
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
29 | P a g e
fgets(name, sizeof(name), stdin); // read string
printf("Name: ");
puts(name); // display string
return 0;
}
Output :
Enter name: Uttaranchal Institute Of Technology
Name: Uttaranchal Institute Of Technology
30 | P a g e
Example :
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = "U", str2[] = "IT";
int main()
{
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
return 0;
}
Output :
strcmp(str1, str2) = 1
strcmp(str1, str2) = 0
32 | P a g e
is 0.
Example :
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "UIT";
char str2[20];
return 0;
}
Output :
UIT
33 | P a g e
When you use strcpy(), the size of the
destination string should be large enough to store
the copied string.
Example :
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="UIT";
char b[20]={'U','I','T','\0'};
printf("Length of string a = %d \n",strlen(a));
printf("Length of string b = %d \n",strlen(b));
return 0;
}
Output :
Length of string a = 3
Length of string b = 3
34 | P a g e