Programming Languages Unit - II-1
Programming Languages Unit - II-1
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
do
{
body of the loop
}
while (test expression);
Next statement;
• The for Statement
}
• 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
goto label;
... .. ... ...
.. ... label:
statement;
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
T A N Z A N I A \0