0% found this document useful (0 votes)
20 views48 pages

Programming Languages Unit - II

The document discusses various programming concepts in C language including conditional statements, loops, arrays and strings. It explains if, if-else, switch statements and their usage. It also describes while, do-while and for loops used for iteration. One dimensional and two dimensional arrays are defined along with array initialization. Functions for string handling like strlen(), strcat(), strcmp() are explained with examples.

Uploaded by

Vinoth Kumar M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
20 views48 pages

Programming Languages Unit - II

The document discusses various programming concepts in C language including conditional statements, loops, arrays and strings. It explains if, if-else, switch statements and their usage. It also describes while, do-while and for loops used for iteration. One dimensional and two dimensional arrays are defined along with array initialization. Functions for string handling like strlen(), strcat(), strcmp() are explained with examples.

Uploaded by

Vinoth Kumar M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 48

PROGRAMMING LANGUAGES

UNIT - II
Conditional Branching (conditional execution)
• if statement
• if-else statement
• else if construct
• switch statement
Looping (iteration)
• while statement
• do-while statement
• for statement
Control Flow (Control Structure)
• Further, for jumping out of a loop, we have the following three
statements
• break statement
• continue statement
• goto statement
1)The if Statement :
• When a statement is executed, the
computer first evaluates the value of the test
condition or expression.
• If the value is TRUE, statement block
and next statement are executed sequentially.
• If the value is FALSE, statement block is
skipped & Execution starts from the Next
statement.
Program for The if Statement :

if (test expression)
{
statement-block;
}
next statement;
The if-else Statement
• When the statement is executed, the computer
first evaluates, the value of the test condition.
• If the value is TRUE , statement block-1 is
executed & the control is transferred to the next
statement.
• If the value is FALSE, statement block – 2
executed. And the control is transferred to
NEXT statement.
The if-else Statement
if (test expression)
{
True-block statements
}
else
{
False-block statements
}
statement-x
The else if Construct (Ladder)
• Computer executes this statement from top to
bottom.
• If a TRUE test condition is found , the
statement block associated with it is executed.
• Then the control is transferred to the NEXT
statement.
• When all the Test condition are FALSE , then
the final else containing the default statement
will be executed.
if (condition 1)
{ else
statement block-1 {
} statement block-s
else if (condition 2) }
{ statement-x;
statement block-2
}
…………………….
…………………….
else if (condition—n)
{
statement block-n
}
• The Switch Statement
• When this statement is executed the computer first
evaluates the value of the expression in the keyword
switch.
• This value is successively compared with the case
label 1, label 2… label n.
• If a case label matches with the value, the statement
block associated with the case label is executed.
• Then the control is transferred to the next statement.
• If none of the case matches with the value, the
default statement block is executed.
switch (expression) default :
{ default-block
case value 1 : break;
}
statement block-1
statement-x;
break;
case value 2 :
statement block-2
break;
case value n:
………………………
statement block-n
break;
• Looping
• In C, loop structures are used to execute a group of
statements repeatedly until some condition is
satisfied.
• If section of the program to be executed repeatedly
while an expression is true.
• When the expression becomes false, the loop
terminates and the control transfers to the statement
following the loop.
• A loop contains two parts, one is known as the
control statement and the other is the body of the
loop.
• Looping
• The while statement

• When this statement is executed, the computer


first evaluates the test condition.
• If the value is FALSE ,the control is transferred
to next statement.
• If the value is TRUE , then the body of the loop
is executed repeadly until the test condition
becomes false.
• When the test condition becomes false the
control is transferred to next statement.
• The while statement

while (test expression)


{
body of the loop
}
Next statement;
• The do-while Statement
• When this statement is executed, the body of the
loop is executed first.
• Then the test condition is evaluated.
• If the value is FALSE ,the control is transferred
to next statement.
• If the value is TRUE , then the body of the loop
is executed repeadly until the test condition
becomes false.
• When the test condition becomes false the
control is transferred to next statement.
• The do-while Statement

do
{
body of the loop
}
while (test expression);
Next statement;
• The for Statement

This statement is used to execute a statement or a group of


statements repeatedly for a known number of times.

for (initialization ; test-condition; increment or


decrement)
{
body of the loop

}
• Nested Loops
• If one loop is enclosed within another for loop
then , such loops are known as nested for
loops.
• There is no limit on the number of for loops
that can be nested.
• The break statement:
It is used to exit from a loop while the test condition is true.
This statement can be used within a for, while, do-while or switch
statement.
• The break statement:
int main (void)
{
int t;
for(t=0; t < 100; t++)
{
printf(''%d ", t);
if(t == 10) break;
• The continue Statement

The continue statement is


used to skip the remaining
loop statements and the
control is transferred to the
beginning of the loop.
In otherwords , the statements
after the keyword continue
will be skipped and the
control is transferred to the
beginning of the loop.
goto statement
• The goto statement can be used to jump from
anywhere to anywhere within a function.

goto label;
... .. ... ...
.. ... label:
statement;
ARRAYS

• An array is a variable that can store multiple


values. For example, if you want to store 100
integers, you can create an array for it.
• int data[100];
Arrays

Example:
i) int mark[100];
• This declares an integer type array named as
mark having 100 memory locations to store
100 integer data.
float salary[100];
• This declares a floating point type array
named as salary having 100 memory
locations to store 100 floating point data.
• How to declare an array?
• dataType arrayName[arraySize];
• For example,
• float mark[5];
• Here, we declared an array, mark, of floating-
point type. And its size is 5. Meaning, it can
hold 5 floating-point values.
• It's important to note that the size and type of
an array cannot be changed once it is
declared.
• How to initialize an array?
• It is possible to initialize an array during
declaration. For example,
• int mark[5] = {19, 10, 8, 17, 9};
Array Initialization

• Example :
• Int age[3] = {10,20,30};
• This declares age as an integer array having
three locations and assigns initial values.
• An array is a group of related items that store
with a common name.
Syntax
• The syntax is as follows for declaring an array
• datatype array_name [size];
Types of arrays
• One – dimensional arrays
• Two – dimensional arrays
• One – dimensional array
• The Syntax is as follows
• datatype array name [size];
• For example, int a[5]
Array example program
• Two multidimensional arrays
• These are used in situations where a table of
values have to be stored (or) in matrices
applications.
• The syntax is given below
datatype array_ name [rowsize] [column size];
int a[3][3]
strings

• A sequence of characters enclosed within


double quotes is called string.
• When a string is stored in the computer
memory, computer automatically places a
null character ‘\0’ as the last character.
• Declaring string variable
• A string variable should be declared as a
character type array and is used to store
string. The general form is
• char variablename[size]= string;
• Char country[8] = “TANZANIA”;
strings

• char variablename[size]= string;


• Char country[9] = “TANZANIA”;

T A N Z A N I A \0
strings

• Reading strings
• strings can be read into memory by means
of any one function namely
• i)getchar function
– This function is used to read a single character.
strlen()

• The strlen() function calculates the length of a


given string.
• The strlen() function takes a string as an
argument and returns its length.
• The strlen() function is defined
in string.h header file.
• Syntax:
int strlen(const char *str);
• Parameter:
• str: It represents the string variable whose
length we have to find.
strcat() function

• In C programming, the strcat() function


contcatenates (joins) two strings.
• The strcat() function is used for string
concatenation.
• It concatenates the specified string at the end
of the another specified string
• C strcat() Declaration
char *strcat(char *str1, const char *str2)
• This function takes two pointer as arguments
and returns the pointer to the destination
string after concatenation.
str1 – pointer to the destination string.
str2 – pointer to the source string which is
appended to the destination string.
#include<stdio.h>
#include<string .h>
Int main()
{
Char strng1[5] = “Hello” ;
Char strng1[5] = “Hi” ;
printf ( “\n Source string = %s”, strng1 ) ;
printf ( “\n Target string = %s”, strng2 ) ;
strcat(strng1, strng2);
printf(“\n Target string after strcat() = %s \n”,
strng1);
return 0;
• Following is the declaration for strcmp()
function.
• int strcmp(const char *str1, const char
*str2)Parameters
• str1 − This is the first string to be compared.
• str2 − This is the second string to be
compared.

You might also like