0% found this document useful (0 votes)
15 views34 pages

Bca C102 SM04

Bca first year notes

Uploaded by

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

Bca C102 SM04

Bca first year notes

Uploaded by

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

Programming in C Notes :

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

The following are different types of conditional


branching statements in C –

Simple if Statement – It first checks


whether the given expression or condition
is true or false. On the basis of this the
control is transferred to a particular
2|Page
statement and then that statement is
executed. If condition becomes true then it
executes statements written in true block,
if
condition fails then true block will be
skipped.

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

if…else statement – The if-else


statement is an extension of the simple if
statement. If the test expression or
condition of “if” statement is true then it is
executed otherwise the false block which is
“else” statement is executed. In other case,
either true-block or false block will be
executed, not both.

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

switch-case Statement – The switch


statement in C is used when we have
multiple conditions to check. It is often
used as an alternative to multiple "if" and
"else if" statements.

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

 Unconditional Branching Statements :


Unconditional branching statements are used
in C to change the normal flow of program
execution. These statements allow us to jump
to a specific point in their code regardless of
any condition.

There are the following types of unconditional


branching statements in C –

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

The "goto" statement jumps to the statement


labeled "label" and executes the code within that
block. The "label" can be any valid identifier followed
by a colon (:).

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

break Statement – The break Statement


is one of the unconditional Branching
Statements in C which is generally used in
the loops for exiting the loop before the
completion of the loop.

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

continue Statement – The continue


statement is used in C programming
language to skip the current iteration of a
loop and move to the next iteration. This
statement is typically used in loops like for
or while.

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

Loop Control Statements –


We may encounter a situation sometimes,
when a block of code needs be executed
several number of times. To counter this,
we use loop control statements.

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
}

i. The initialization defines where to begin the


loop by declaring (or referencing) the
iterator variable.
ii. The condition determines when to stop
looping (when the expression evaluates to
false).
iii. The increment statement updates the
iterator each time the loop is completed.

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

 while loop : The “while” loop creates a loop


that is executed as long as a specified condition
evaluates to true. The loop will continue to run
until the condition evaluates to false.
Syntax –
while ( condition )
{
// Code to be executed
}
15 | P a g e
Example :
#include<stdio.h>
int main()
{
int i = 0 ;
while ( i < 5 )
{
printf (“ %d\t ”,i );
i++;
}
return 0;
}

Output :
1 2 3 4

 do…while loop : A do…while statement


creates a loop that executes a block of code
once, checks if a condition is true, and then
repeats the loop as long as the condition
remains true. They are used when the loop
body needs to be executed at least once. The

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

printf("Enter your age: ");


scanf("%d", &age);
// ternary operator to find if a person can vote or not

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 –

i. Each element of an array is of same data


type and carries the same size, i.e., int = 4
bytes.
ii. Elements of the array are stored at
contiguous memory locations where the
first element is stored at the smallest
memory location.
iii. Elements of the array can be randomly
accessed since we can calculate the
address of each element of the array with
the given base address and the size of the
data element.

 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[0]=80; //initialization 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 :

i. Single Dimension Array – It is a linear


collection of similar data types, and the
allocated memory for all the blocks in the
single-dimensional array remains
consecutive.

Syntax :
data_type array_name[array_size];

Example :

#include <stdio.h>
int main()
{
int arr[5] = {7, 8}; //single dimensional array

for (int i = 0; i < 5; i++)


{
printf("%d \t", arr[i]); //prints elements of array
}
return 0;
}

22 | P a g e
Output :
78000

ii. Multi-Dimensional Array – It is an advanced


version of a single-dimensional array that can
be nested up to multilevel. It may be two
dimensional or three dimensional.

Syntax :

Datatype_array_name[sizeof_1st_dimension]
[sizeof_2nd_dimension].....[sizeof_nth_dimen
sion];

 Two Dimensional Array – A two-dimensional


array is a specialized form of a
multidimensional array which have two
dimensions. It is widely used among all types of
arrays in C programming to represent a matrix
in a data structure.

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

for (int i = 0; i < 2; i++)


{
For(int j=0; j<2; j++)
{
printf("%d \t", arr[i]); //prints elements of array
}
}
return 0;
}

Output :
1 2
3 4

 Three Dimensional Array – It is also a


specialized form of a multidimensional array,
generally, they are used to represent three-
dimensional coordinates in programming.

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 –

Char s[size]= “characters”;

 Initialization of Strings –

Strings can be initialized in following ways :


char c[] = "abcd";
char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '\0'};

27 | P a g e
char c[5] = {'a', 'b', 'c', 'd', '\0'};

 Reading and writing Strings – We can


use several predefined functions from
“string.h” header file according to our
requirement. Such as : if we want to read a
character then we can use “scanf()”
function with “%s” format specifier but if
we want to read both the characters as well
as a sentences then we can use “gets() or
fgets()” function. To display it on screen we
use “puts()” function.

 Scanf() – The scanf() function reads


the sequence of characters until it
encounters whitespace (space, newline,
tab, etc.).

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.

 Even though Dennis Ritchie was entered in the


above program but only Dennis was stored in
“name” string in the program. It is because there
was a space after Dennis.

 fgets() and puts() – We can use


“fgets() or gets()” function to read a line of
string and “puts()” function to display the
string.

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

 On the place of “fgets()” we can use “gets()” to


take input from user. However, it is removed from
the C standard.

 String Functions – String functions


perform different types of operations
which are predefined in “string.h” header
file.

 strcat() – The strcat() function


concatenates or combines the
“destination” string and the “source”
string, and the result is stored in
“destination” string.

30 | P a g e
Example :
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = "U", str2[] = "IT";

// concatenates str1 and str2


// the resultant string is stored in str1
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
}
Output :
UIT
IT

 When we use strcat(), the size of the destination


string should be large enough to store the
resultant string. If not, we will get an error.

 strcmp() – The strcmp() compares


two strings character by character. If the
strings are equal, the function returns 0
else returns 1.
31 | P a g e
Example :
#include <stdio.h>
#include <string.h>

int main()
{
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;

// comparing strings str1 and str2


result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);

// comparing strings str1 and str3


result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);

return 0;
}

Output :
strcmp(str1, str2) = 1
strcmp(str1, str2) = 0

 In above program, strings str1 and str2 are not


equal. Hence, the result is a non-zero integer.
strings str1 and str3 are equal. Hence, the result

32 | P a g e
is 0.

strcpy() – The strcpy() function copies the


string pointed by source (including the
null character) to the destination and
returns the copied string.

Example :
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "UIT";
char str2[20];

// copying str1 to str2


strcpy(str2, str1);

puts(str2); // will display UIT

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.

 strlen() – The strlen() function takes a


string as an argument and returns its
length.

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

 The strlen() function doesn't count the null


character \0 while calculating the length.

34 | P a g e

You might also like