type conversion and type casting
• Type conversion or typecasting of variables refers to
changing a variable of one data type into another.
• Type conversion is done implicitly, whereas typecasting has
to be done explicitly by the programmer.
• Type conversion is done when the expression has variables
of different data types. To evaluate the expression, the data
type is promoted from lower to higher level where the
hierarchy of data types (from higher to lower) can be given
as: double, float, long, int, short, and char.
Consider the code given below in which an
integer data type is promoted to float. This is
known as promotion (when a lower-level data
type is promoted to a higher type)
float x;
int y = 3;
x = y;
• Typecasting - It is also known as forced
conversion. Type- casting an arithmetic
expression tells the compiler to represent the
value of the expression in a certain way.
• It is done when the value of a higher data type
has to be converted into the value of a lower
data type.
• But this casting is under the programmer's
control and not under compiler's control.
• For example, if we need to explicitly typecast a
floating-point variable into an integer variable,
then the code to perform typecasting can be
given as
float salary = 10000.00;
int sal;
sal = (int) salary
Decision control statements/conditional
branching statements
• Decision control statements that can alter the flow of a
sequence of instruction.
• The conditional branching statements help to jump from one
part of the program to another depending on whether a
particular condition is satisfied or not.
if statement
if-else statement
if-else-if ladder statement
nested if statement
Switch statement
Figure -1 Decision control statements
if statement
• If statement is the simplest form of decision
control statements that is frequently used in
decision making.
• First the test expression is evaluated. If the
test expression is true, the statement of if
block (statement 1 to n) are executed
otherwise these statements will be skipped
and the execution will jump to statement x.
if statement
syntax Example program
if(text expression)
{
Statement -1;
……..
………
Statement -n;
}
Statement x;
if-else statement
• In the if-else construct, first the test expression is
evaluated. If the expression is true, statement
block 1 is executed and statement block 2 is
skipped. Otherwise, if the expression is false,
statement block 2 is executed and statement
block 1 is ignored.
• In any case after the statement block 1 or 2 gets
executed the control will pass to statement x.
Therefore, statement x is executed in every case.
if-else statement
syntax Example program
if(text expression)
{
Statement block-1;
}
else
{
Statement block-2;
}
Statement x;
if-else-if ladder statement
• C language supports if else if statements to
test additional conditions apart from the initial
test expression. The if-else-if construct works
in the same way as a normal if statement.
if-else-if statement
Syntax Example program
if(text expression)
{
Statement block-1;
}
else if(text expression)
{
Statement block-2;
}
……………………………..
……………………………..
else
{
Statement block -n;
}
Statement x;
nested if statement
A nested if in C is an if statement that
is the target of another if statement.
Nested if statements mean an if
statement inside another if statement.
Dangling else problem
• This problem is created when there is no matching else for
every if statement. In such cases, C always pairs an else
statement to the most recent unpaired if statement in the
current block.
Conditional/Ternary
operators
• Conditional operators in C refer to
the ternary operator "?:" that allows
executing code based on a condition.
• Examples include finding the
maximum of two numbers and
checking if a number is even or odd.
Conditional/Ternary
operators
if-else ternary
if(marks>50) Result=marks>50? ‘P’ : ‘F’;
Result=‘P’
else
Result=‘F’;
if( a>b) big = a>b ? a:b;
big=a;
else
big=b;
switch statement
• A switch case statement is a multi-way
decision statement.
• Switch statements are used:
– When there is only one variable to evaluate in the
expression
– When many conditions are being tested for
Switch case statement advantages include: Easy to
debug, read, understand and maintain Execute
faster than its equivalent if-else construct
switch statement
syntax Example program
switch(variable)
{
case value 1:
statement block 1;
break;
case value 2:
statement block 2;
break;
-------------------
-------------------
case value n:
statement block n;
break;
default:
statement block d;
break;
}
statement x;
Iterative or looping statements
Iterative or looping statements
• Iterative statements are used to repeat the
execution of statements, depending on the
value of an integer expression.
• Types of iterative statements
while loop
do – while loop
for loop
while loop
• The while loop is used to repeat one or more
statements while a particular condition is true. In
the while loop, the condition is tested before any
of the statements in the statement block is
executed.
• If the condition is true, only then the statements
will be executed otherwise the control will jump to
the immediate statement outside the while loop
block. We must constantly update the condition of
the while loop.
while loop
Example program
Syntax
Initialization;
while(condition)
{
Statement block;
Update expression;
}
do-while loop
• The do-while loop is similar to the while loop. The only
difference is that in a do-while loop, the test condition is
tested at the end of the loop. The body of the loop gets
executed at least one time (even if the condition is false).
• The do while loop continues to execute whilst a condition
is true. There is no choice whether to execute the loop or
not. Hence, entry in the loop is automatic there is only a
choice to continue it further or not. The major
disadvantage of using a do while loop is that it always
executes at least once, so even if the user enters some
invalid data, the loop will execute.
do-while loop
Example Program
Syntax
Initialization;
do {
Statement block;
Update expression;
} while(condition);
for loop
• For loop is used to repeat a task until a particular condition is true.
The syntax of a for loop When a for loop is used, the loop variable is
initialized only once. With every iteration of the loop, the value of
the loop variable is updated and the condition is checked.
• If the condition is true, the statement block of the loop is executed
else, the statements comprising the statement block of the for loop
are skipped and the control jumps to the immediate statement
following the for loop body.
• Updating the loop variable may include incrementing the loop
variable, decrementing the loop variable or setting it to some other
value like, i +=2, where i is the loop variable.
for loop
Syntax
for(initialization; condition; update expression)
{ Example Program
Statement block;
}
Nested loop-nested for loop
A nested loop means a loop statement inside another loop statement.
That is why nested loops are also called “loop inside loops“
Syntax
for(initialization; condition; update expression)
{
for(initialization; condition; update expression)
{
Statement block;
}
Statement block;
}
•
Ex
am
pl
e
Pr
og
ra
m
Unconditional statements/Jumping
statements
• In C, an unconditional statement is a
statement that is executed without
any condition or consideration of
whether a particular condition is true
or false.
break statement
break statement
• The break statement is used to terminate the execution
of the nearest enclosing loop in which it appears. When
compiler encounters a break statement, the control
passes to the statement that follows the loop in which
the break statement appears.
• Its syntax is quite simple, just type keyword break
followed with a semi-colon.
break;
• In switch statement if the break statement is missing
then every case from the matched case label to the end
of the switch, including the default, is executed.
break statement
Example program
Syntax
for(initialization; condition;
update expression)
{
If(condition)
break;
Statement block;
}
continue statement
continue statement
• The continue statement can only appear in the
body of a loop. When the compiler encounters a
continue statement then the rest of the statements
in the loop are skipped and the control is
unconditionally transferred to the loop-
continuation portion of the nearest enclosing loop.
• Its syntax is quite simple, just type keyword
continue followed with a semi-colon.
continue;
continue statement
Syntax Example program
for(initialization; condition;
update expression)
{
If(condition)
Continue;
Statement block;
}
goto statement
goto statement
• The goto statement is used to transfer control to a specified label. Here
label is an identifier that specifies the place where the branch is to be
made. Label can be any valid variable name that is followed by a colon
(:).
• Note that label can be placed anywhere in the program either before or
after the goto statement. Whenever the goto statement is encountered,
the control is immediately transferred to the statements following the
label.
• Goto statement breaks the normal sequential execution of the program.
If the label is placed after the goto statement then it is called a forward
jump and in case it is located before the goto statement, it is said to be a
backward jump.
goto statement
• Syntax • Example program
goto label
------------
------------
label:
statements;