Unit-Ii Fop
Unit-Ii Fop
R ollege/Hosur/2024
UNIT-II
Decision Making , Looping and Arrays-Decision Making and Branching:
Introduction – if, if….else, nesting of if …else statementselse if ladder –
The switch statement, The ?: Operator – The go to Statement. Decision
Making and Looping: Introduction- The while statement- the do
statement – the for statement-jumps in loops. Arrays – Character Arrays
and Strings
Introduction
Control structures are used to transfer control form one statement in
a program to any other statement
The control statements are classified as follows
Page 1
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Simple if Statement
C uses the keyword if to execute a set of statements when logical
condition is true
Page 2
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Syntax:
if(condition)
{
//Statement to be executed
//if condition is true
Statement 1;
Statement 2;
. .
. .
Statement n;
}
Flow diagram
Page 3
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
int main()
{
int x = 15;
if (x > 20)
{
printf("15 is greater than 20");
}
printf("The statement executed is outside the if block");
}
Output
The statement executed is outside the if block
The if...else Statement
Syntax
if(condition)
{
//Execute this block
//if condition is true
}
else
Page 4
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
{
//Execute this block
//if condition is false
}
Flow diagram
Example Program
#include <stdio.h>
int main(void)
{
int n;
printf("\n Enter the number : ");
scanf("%d", &n);
if(n%2 == 0)
printf("\n %d is even", n);
else
printf("\n %d is odd", n);
return 0;
}
Page 5
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Output:
Enter the number : 4
4 is even
Nested if...else Statement
Syntax
if(Test Expression 1)
{
if(Test Expression 2)
{
statement block 1;
}
else
{
statement block 2;
}
}
else
{
if(Test Expression 3)
{
statement block 3;
}
else
{
statement block 4;
}
}
Page 6
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Page 7
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Example Program
#include <stdio.h>
int main(void)
{
int a, b, c;
printf("Enter three numbers : ");
scanf("%d%d%d \n",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
Page 8
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
}
return 0;
}
Output
Enter three numbers : 12 9 11
a is the greatest
The if-else-if statement
If else ladder is used, If we want to check for multiple conditions one
after another
Syntax
if(Test Expression 1)
{
statement block 1;
}
else if(Test Expression 2)
{
statement block 2;
}
...........................
else
{
statement block x;
}
statement y;
Page 9
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Flow Diagram
Example Program
#include <stdio.h>
int main(void)
{
int num;
printf("\n Enter any number : ");
scanf("%d", &num);
if(num==0)
printf("\n The value is equal to zero");
else if(num > 0)
printf("\n The number is positive");
else
printf("\n The number is negative");
Fundamentals of Computer Programming/2023 Regulations
Page 10
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
return 0;
}
Output
Enter any number : 12
The number is positive
Switch Case
The switch case statement is used when we have multiple options and
we need to perform a different task for each option
A switch statement is an alternative to if-else-if statement
The switch statement has four important elements.
The test expression
The case statements
The break statements
The default statement
Syntax
switch (expression)
{
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
.
Page 11
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
Flow Diagram
Page 12
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Example Program
#include <stdio.h>
int main()
{
int i=2;
switch (i)
{
case 1:
printf("Case1 ");
break;
case 2:
printf("Case2 ");
break;
case 3:
printf("Case3 ");
break;
case 4:
printf("Case4 ");
break;
default:
printf("Default ");
}
return 0;
}
Output
Case 2
Page 13
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Looping Statements
while Loop
The while loop in C is used to evaluate a test condition and iterate over
the loop body until the condition returns True
Syntax
while (condition)
{
// Code to execute as long as the condition is true
}
Here,
while: This is the keyword that begins the while loop
condition: This is a Boolean expression
The code inside the curly braces {} is the body of the loop
Flow diagram
Page 14
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Example
#include<stdio.h>
int main()
{
int number=10;
Page 15
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
} while (condition);
Flow diagram
Example
#include <stdio.h>
int main()
{
// loop variable declaration and initialization
int i = 0;
// do while loop
do {
printf("Skill Vertex\n");
i++;
} while (i < 3);
return 0;
}
Output
Skill Vertex
Skill Vertex
Skill Vertex
Page 16
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
For loop
This is one of the most frequently used loop in C programming
For loop is a loop in which all three statements initialization,
condition and increment/decrement is given in a single step (before
the code)
Code may be executed for 0 or more times
A loop is used for executing a block of statements repeatedly until a
given condition returns false
It is good if number of iteration(repetition) is known by the user
Syntax
for ( starting value; condition; incrementation or
decrementation )
{
statements;
}
Page 17
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Flow diagram
Example
#includ<stdio.h>
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
return 0;
}
Output
1
2
3
Jump Statements
Page 18
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Flow diagram
Page 19
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Example
#include <stdio.h>
int main()
{
int num =0;
while(num<=100)
{
printf("value of variable num is: %d\n", num);
if (num==2)
{
break;
}
num++;
}
printf("Out of while-loop");
return 0;
}
Output
value of variable num is: 0
value of variable num is: 1
value of variable num is: 2
Out of while-loop
Continue
The “continue” keyword is used to skip an iteration of the loop if a
certain condition is met
It is used inside loop along with the decision-making statements
Page 20
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Flow Diagram
Page 21
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Example
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++)
{
if(i == 25 || i == 50 || i == 75)
continue;
printf("%d\n",i);
}
return 0;
}
Output
It will print numbers from 1 to 100 except the numbers 25,50 and 75
goto
C programming introduces a goto statement by which you can jump
directly to a line of code within the same file
The programmer needs to specify a label or identifier with the goto
statement
Page 22
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Syntax
Flow Diagram
Example
#include<stdio.h>
int main ()
Page 23
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Page 24
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Example
Page 25
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Arrays
C Programming Arrays is collection of the Elements of the same data
type
All Elements are stored in the Contiguous memory
All elements in the array are accessed using the subscript variable
(index)
C programming structure to store fixed-size, multiple values
Page 26
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Array Terminologies
Size:
Number of elements or capacity to store elements in an array
It is always mentioned in square brackets [ ]
Type:
Refers to data type
It decides which type of element is stored in the array
Base:
The address of the first element is a base address
The array name itself stores address of the first element
Index:
The array name is used to refer to the array element. For example
num[x], num is array and x is index
The value of x begins from 0
The index value is always an integer value
Page 27
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Range:
Value of index of an array varies from lower bound to upper bound
For example in num[100] the range of index is 0 to 99
Word:
It indicates the space required for an element
In each memory location, computer can store a data piece
The space occupation varies from machine to machine
If the size of element is more than word (one byte) then it occupies
two successive memory locations
The variables of data type int, float, long need more than one byte in
memory
One-dimensional Arrays
The one-dimensional arrays in C language have just one dimension
They are represented by a single row or column
Syntax
data_type array_name [size];
Array declaration
Page 28
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Example
#include<stdio.h>
int main() {
// Declare an array of integers
int numbers[5];
// Initialize the array elements
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Access and print array elements
printf("Array elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Output
Array elements: 10 20 30 40 50
Initialize an Array
Initializing an array means assigning an initial value to the variable
After array declaration
Syntax
data_type array_name [size] = {value1, value2, ... valueN};
Page 29
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Page 30
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Example
Initializing and Accessing Array Elements
#include<stdio.h>
int main() {
// Declare and initialize an array
int numbers[] = {10, 20, 30, 40, 50};
// Access and print array elements
printf("Array elements: ");
printf("%d ", numbers[0]); // Accessing the first element
printf("%d ", numbers[1]); // Accessing the second element
printf("%d ", numbers[2]); // Accessing the third element
printf("%d ", numbers[3]); // Accessing the fourth element
printf("%d ", numbers[4]); // Accessing the fifth element
printf("\n");
return 0;
}
Output
Array elements: 10 20 30 40 50
Example Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t,n;
clrscr();
scanf("%d",&n);
Page 31
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
Input
8
5
3
6
1
Output
1
Page 32
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
3
5
6
8
Multi-dimensional Arrays in C
They have more than one dimension, such as 2-D and 3-D arrays
Two dimensional arrays will be in the form of rows and columns
format
Syntax of 2-D Array
datatype array_name[size1] [size2];
Example
#include<stdio.h>
int main()
{
int i, j;
int a[3][2] = { { 1, 4 },{ 5, 2 },{ 6, 5 }};
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
Page 33
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Output
14
52
65
Example
#include<stdio.h>
int main()
{
int i, j;
int a[3][2] = { { 1 },{ 5, 2 },{ 6 }};
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
Output
10
52
60
Example
#include<stdio.h>
Page 34
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
int main()
{
int rowCount, columnCount, i, j;
int firstMatrix[10][10], secondMatrix[10][10],
resultMatrix[10][10];
Page 35
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Page 36
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
6 6
Character Arrays
Character arrays to store strings and then we can apply multiple
operations on strings like modifying a string, copying a string,
concatenation of a string to another, finding properties of strings like
length of the string etc.
The character array should be large enough to accommodate the
string. i.e. If we have a string “Rahul” then to store the string we need
a character array of size 6
Syntax
storageclass chardatatype arrayname[exp];
Example
char name[50];
Initializing the Character Array
Each element of the array is placed in a definite memory space and
each element can be accessed separately
The array element should end with the null character as a reference
for the termination of the character array
The computer will treat them as a single character
char name[5]="Rahul";
The elements would be assigned to each of the character array
position in the following way
name[0]='R'; name[1]='a'; name[2]='h'; name[3]='u'; name[4]='l';
Character Array in the Memory
Page 37
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Example
#include <stdio.h>
int main() {
// Character array example
char charArray[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Character Array: %s\n", charArray);
Output
Character Array: Hello
Example
#include <stdio.h>
#include <conio.h>
#define NUM_ITEMS 5 //optional
main()
{
charstr[12];
str[0]='H'; str[1]='e'; str[2]='l'; str[3]='l'; str[4]='o';
str[5]=' '; str[6]='W'; str[7]='o'; str[8]='r'; str[9]='l';
str[10]='d'; str[11]='\0';
Page 38
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
str[number] 0 1 2 3 4 5 6 7 8 9 10 11
Contents H E l l o W o r l d \0
ASCII code 72 101 108 108 111 32 87 111 114 108 100 0
Strings
Sequence of characters enclosed in double quotes is called a string
A character array is also called as a string
Every string ends with a null character '\0'
Example
“string_constant”
Declaring String
A string variable should be declared as a character type array and is
used to store string
Syntax
datatype variablename [size];
Example
char city[10];
Page 39
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
Syntax
strlen(str);
Example
strlen("computer");
Example
#include.stdio.h>
main()
{
char s[100];
static int len;
clrscr();
printf("\nEnter the string:");
gets(s);
len=strlen(s);
printf("\nlength=%d",len);
getch();
}
Output
Page 40
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
length=8
strcat()
Example
#include.stdio.h>
main()
{
char s[100],c[100];
clrscr();
printf("\nEnter first string:");
gets(s);
printf("\nEnter second string:");
Page 41
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
gets(c);
printf("\nAfter concatenation: %s",strcat(s,c));
getch();
}
Input
Enter first string:computer
Output
After concatenation: computerscience
strcmp()
Example
#include.stdio.h>
main()
{
char s[100],c[100];
Page 42
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
clrscr();
printf("\nEnter first string:");
gets(s);
printf("\nEnter second string:");
gets(c);
if(strcmp(s,c)==0)
printf("strings are equal");
else if(strcmp(s,c)>0)
printf("\n first string is biggerthan second");
else
printf("\nsecond string is biggerthan first");
getch();
}
Input
Enter first string:HOSUR
Enter second string:HOSUR
output
strings are equal
strrev()
strrev() function is used to reverse the given string
It takes the string as the parameter
Syntax
strrev(str);
Example
strrev("aeri");
Example
#include<stdio.h>
main()
{
Page 43
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
char s[100];
clrscr();
printf("\nEnter the string:");
gets(s);
printf("\nreverse string is=%s",strrev(s));
getch();
}
Input
Enter the string:aeri
Output
reverse string is=irea
strupr()
strupr() function is used to convert the given string into uppercase
It takes the string as parameter
Syntax
strupr(str);
Example
strupr("aeri");
Example
#include<stdio.h>
main()
{
char s[100];
clrscr();
printf("\nEnter string:");
gets(s);
printf("\nstring in uppercase:%s",strupr(s));
getch();
Page 44
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024
}
Input
Enter string:aeri
Output
string in uppercase:AERI
strlwr()
strlwr() function is used to convert the given string into lowrcase
It takes the string as parameter
Syntax
strlwr(str);
Example
strlwr("aeri");
Example
#include<stdio.h>
main()
{
char s[100];
clrscr();
printf("/nEnter string:");
gets(s);
printf("/nstring in lowercase:%s",strlwr(s));
getch();
}
Input
Enter string:AERI
Output
string in lowercase:aeri
Page 45