LDP Unit 3
LDP Unit 3
The conditional statements (also known as decision control structures) such as if, if
else, switch, etc. are used for decision-making purposes in C programs.
1) IF Statement:
Syntax of if Statement:
if(condition)
{
// Statements to execute if
// condition is true
}
Page 1
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15)
{
printf("10 is greater than 15");
}
printf("I am Not in if");
}
OUTPUT:
I am Not in if
2) IF ELSE Statement:
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t. But what if we want to do something
else when the condition is false? Here comes the C else statement. We can use the
else statement with the if statement to execute a block of code when the condition is
false. The if-else statement consists of two blocks, one for false expression and one
for true expression.
Syntax of if Statement:
Page 2
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15)
{
printf("i is smaller than 15");
}
else
{
printf("i is greater than 15");
}
return 0;
}
Page 3
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
i is greater than 15
Syntax of if Statement:
if (condition1)
{
// Executes when condition1 is true
if (condition_2)
{
// statement 1
}
else
{
// Statement 2
}
}
else {
if (condition_3)
{
// statement 3
}
else
{
// Statement 4
}
}
Page 4
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
int main()
{
int i = 10;
if (i == 10)
{
if (i < 15)
printf("i is smaller than 15\n");
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
else
{
if (i == 20)
{
if (i < 22)
printf("i is smaller than 22 too\n");
else
printf("i is greater than 25");
} }
return 0;
}
Page 5
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
i is smaller than 15
i is smaller than 12 too
4) IF-ELSE-IF Statement:
The if else if statements are used when the user has to decide among multiple options.
The C if statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and the rest
of the C else-if ladder is bypassed. If none of the conditions is true, then the final else
statement will be executed. if-else-if ladder is similar to the switch statement.
Syntax of if Statement:
if (condition)
statement;
else if (condition)
statement;
else
statement;
#include <stdio.h>
int main()
{
Page 6
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
OUTPUT:
i is 20
5) SWITCH Statement:
The switch case statement is an alternative to the if else if ladder that can be used to
execute the conditional code based on the value of the variable specified in the switch
statement. The switch block consists of cases to be executed based on the value of the
switch variable.
Syntax of if Statement:
switch (expression)
{
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Page 7
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
int main()
{
int var = 2;
switch (var)
{
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
Page 8
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
Case 2 is executed
6) Break Statement:
This loop control statement is used to terminate the loop. As soon as the break
statement is encountered from within a loop, the loop iterations stop there, and
control returns from the loop immediately to the first statement after the loop.
Syntax of if Statement:
break;
Page 9
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
OUTPUT:
7) Continue Statement:
This loop control statement is just like the break statement. The continue statement is
opposite to that of the break statement, instead of terminating the loop, it forces to
execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or execute
the next iteration. When the continue statement is executed in the loop, the code
inside the loop following the continue statement will be skipped and the next iteration
of the loop will begin.
Syntax of if Statement:
continue;
Page
10
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
int main()
{
// loop from 1 to 10
for (int i = 1; i <= 10; i++)
{
if (i == 6)
continue;
else
printf("%d ", i);
}
return 0;
}
OUTPUT:
1 2 3 4 5 7 8 9 10
7) Go to Statement:
The goto statement in C also referred to as the unconditional jump statement can be
used to jump from one point to another within a function.
Syntax of if Statement:
Page
11
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
#include <stdio.h>
void printNumbers()
{
int n = 1;
label:
printf("%d ", n);
n++;
if (n <= 10)
goto label;
}
int main()
{
printNumbers();
return 0;
}
Page
12
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
1 2 3 4 5 7 8 9 10
Looping Statements:
1) FOR Loop:
For loop in C programming is a repetition control structure that allows programmers
to write a loop that will be executed a specific number of times. for loop enables
programmers to perform n number of steps together in a single line.
Syntax of if Statement:
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
printf( "Ankit\n");
}
return 0;
}
OUTPUT:
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Page
14
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
2) While Loop:
While loop does not depend upon the number of iterations. In for loop the number of
iterations was previously known to us but in the While loop, the execution is
terminated on the basis of the test condition. If the test condition will become false
then it will break from the while loop else body will be executed.
Syntax of if Statement:
initialization_expression;
while (test_expression)
{
// body of the while loop
update_expression;
}
#include <stdio.h>
int main()
{
int i = 2;
while(i < 10)
{
printf( "Ankit\n");
i++;
}
return 0;
}
Page
15
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
3) Do While Loop:
The do-while loop is similar to a while loop but the only difference lies in the do-
while loop test condition which is tested at the end of the body. In the do-while loop,
the loop body will execute at least once irrespective of the test condition.
Syntax of if Statement:
initialization_expression;
do
{
// body of do-while loop
update_expression
}
while (test_expression);
Page
16
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
int main()
{
int i = 2;
do
{
printf( "ANKIT\n");
i++;
} while (i < 1);
return 0;
}
OUTPUT:
ANKIT
Array:
Page
17
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
}
C Array Initialization:
Initialization in C is the process to assign some initial value to the variable. When the
array is declared or allocated memory, the elements of the array contain some garbage
value. So, we need to initialize the array to some meaningful value. There are
multiple ways in which we can initialize an array in C.
In this method, we initialize the array along with its declaration. We use an initializer
list to initialize multiple elements of the array. An initializer list is the list of values
enclosed within braces { } separated b a comma.
data_type array_name [size] = {value1, value2, ... valueN};
Page
18
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
If we initialize an array using an initializer list, we can skip declaring the size of the
array as the compiler can automatically deduce the size of the array in these cases.
The size of the array in these cases is equal to the number of elements present in the
initializer list as the compiler can automatically deduce the size of the array.
data_type array_name[] = {1,2,3,4,5};
We initialize the array after the declaration by assigning the initial value to each
element individually. We can use for loop, while loop, or do-while loop to assign the
value to each element of the array.
for (int i = 0; i < N; i++)
{
array_name[i] = valuei;
}
int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
Page
19
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
1) Code Reusability
2) Modularization
3) Efficient Compilation
Types of Array in C
There are two types of arrays based on the number of dimensions it has. They are as
follows:
One Dimensional Arrays (1D Array)
Multidimensional Arrays
The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have
only one dimension.
Page
20
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
int main()
{
// 1d array declaration
int arr[5];
// 1d array initialization using for loop
for (int i = 0; i < 5; i++) {
arr[i] = i * i - 2 * i + 1;
}
printf("Elements of Array: ");
// printing 1d array by traversing using for loop
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT:
Elements of Array: 1 0 1 4 9
#include <stdio.h>
int main()
{
// creating array of character
char arr[6] = { 'A', 'N', 'K', 'I', 'T', '\0' };
// printing string
int i = 0;
while (arr[i])
{
printf("%c", arr[i++]);
}
return 0;
}
OUTPUT:
ANKIT
Page
21
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
int main()
{
// declaring and initializing 2d array
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
printf("2D Array:\n");
// printing 2d array
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Page
22
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
2D Array:
10 20 30
40 50 60
#include <stdio.h>
int main()
{
int arr[2][2][2] = { 10, 20, 30, 40, 50, 60,70,80 };
// printing elements
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n \n");
}
return 0;
}
Page
23
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
3D Array:
10 20
30 40
50 60
70 80
Characteristics of Arrays in C
Fixed Size: The size of an array is defined at compile time and cannot be changed
during runtime.
Homogeneous Collection: Arrays can only store elements of the same data type.
Indexing: Array indexing starts from 0. The first element is at index 0, and the last
element is at index N-1 for an array of size N.
Dimensions: Arrays can be single-dimensional, multi-dimensional, or even higher-
dimensional. The dimension indicates the number of indices required to access elements.
Contiguous Storage: Elements are stored in contiguous memory locations, enabling
efficient access and management.
Random Access: Any element can be accessed in constant time,
No Index Out-of-Bounds Checking: C does not provide runtime checks for accessing
indices beyond the array's declared range, which can lead to undefined behavior.
Advantages of Array in C:
Disadvantages of Array in C:
Page
24
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Insertion and deletion of elements can be costly since the elements are needed to be
rearranged after insertion and deletion.
#include <stdio.h>
float getAverage(float* arr, int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += arr[i];
}
return sum / size;
}
int main()
{
float arr[5] = { 10, 20, 30, 40, 50 };
int n = sizeof(arr) / sizeof(float);
printf("Array Elements: ");
for (int i = 0; i < n; i++)
{
printf("%.0f ", arr[i]);
}
printf("\nAverage: %.2f", getAverage(arr, n));
return 0;
}
OUTPUT:
Array Elements: 10 20 30 40 50
Average: 30.00
#include <stdio.h>
int getMax(int* arr, int size)
{
int max = arr[0];
Page
25
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++)
{
sum += arr[i];
}
printf("The sum of the array elements is: %d\n", sum);
return 0;
}
Page
26
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
String:
Page
27
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
#define MAX 50
int main()
{
char str[MAX];
fgets(str, MAX, stdin);
printf("String is: \n");
puts(str);
return 0;
}
OUTPUT:
String is:
Ankit
#include <stdio.h>
int main()
{
char str[20];
scanf("%[^\n]s", str);
printf("%s", str);
return 0;
}
OUTPUT:
ANKIT
Page
28
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
#include <stdio.h>
int main()
{
char dest[50] = "Hello";
char src[50] = " I am Ankit";
printf("String Before: %s\n", dest);
strcat(dest, src);
printf("String After: %s", dest);
return 0;
}
OUTPUT:
2) strlen() Function:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hi I am Ankit";
size_t length = strlen(str);
printf("String: %s\n", str);
printf("Length: %zu\n", length);
return 0;
}
Page
29
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
String: Hi I am Ankit
Length: 13
3) strcmp() Function:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
int result = strcmp(str1, str2);
if (result == 0)
{
printf("The strings are equal.\n");
} else if (result < 0)
{
printf("The first string is less than the second string.\n");
} else
{
printf("The first string is greater than the second string.\n");
}
return 0;
}
OUTPUT:
4) strcpy() Function:
int main()
{
char source[] = "ANKIT";
char dest[20];
Page
30
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
strcpy(dest, source);
printf("Source: %s\n", source);
printf("Destination: %s\n", dest);
return 0;
}
OUTPUT:
Source: ANKIT
Destination: ANKIT
5) strchr() Function:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Ankit";
char ch = 'e';
char* result = strchr(str, ch);
printf("The character '%c' is found at index %ld\n",ch, result - str);
}
else
{
printf("The character '%c' is not found in the " "string\n", ch);
}
return 0;
}
OUTPUT:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Ankit";
char str2[] = "PATEL";
Page
31
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
strupr(str1);
strlwr(str2);
printf("Upper String=%s",str1);
printf("Upper String=%s",str2);
return 0;
}
OUTPUT:
6) strtok() Function:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Ankit,Patel.Gandhinagar";
// Delimiters: space, comma, dot,
// exclamation mark
const char delimiters[] = ",.";
char* token = strtok(str, delimiters);
while (token != NULL)
{
printf("Token: %s\n", token);
token = strtok(NULL, delimiters);
}
return 0;
}
OUTPUT:
Token: Ankit
Token: Patel
Token: Gandhinagar
7) strstr() Function:
#include <stdio.h>
#include <string.h>
Page
32
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
int main()
{
char s1[] = "ankitpatelgandhinagar";
char s2[] = "patel";
char* result;
result = strstr(s1, s2);
if (result != NULL)
{
printf("Substring found: %s\n", result);
}
else
{
printf("Substring not found.\n");
}
return 0;
}
OUTPUT:
Substring found:patelgandhinagar
String Storage in C:
1) Character Array:
Example:
2) String Literal:
Page
33
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Here, the string "Hello" is stored in a read-only section, and str points to its starting
memory address.
Example:
Strings can also be allocated dynamically using functions like malloc or calloc.
Example:
strcpy(str, "Hello");
String Initialization in C:
Using an Array:
Using a Pointer:
Dynamic Allocation:
strcpy(str3, "Hello");
Page
34