Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Programming Languages Unit - II-1

The document discusses various programming concepts in C including conditional statements, loops, arrays, and strings. It describes the if, if-else, else if, and switch conditional statements. It also covers different loop structures like while, do-while, for, and nested loops. The document explains how to declare, initialize, and access array elements. It provides examples of declaring character arrays to represent strings in C and how strings are null-terminated.

Uploaded by

Vinoth Kumar M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Programming Languages Unit - II-1

The document discusses various programming concepts in C including conditional statements, loops, arrays, and strings. It describes the if, if-else, else if, and switch conditional statements. It also covers different loop structures like while, do-while, for, and nested loops. The document explains how to declare, initialize, and access array elements. It provides examples of declaring character arrays to represent strings in C and how strings are null-terminated.

Uploaded by

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

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

You might also like