Escolar Documentos
Profissional Documentos
Cultura Documentos
Type conversion in
assignments
int i;
float b;
i=3.5;
b=30;
3 is stored in i
and
30.000000 is stored in b.
Type Casting
Problem :
For Example:
void main(){
float a;
int x=6, y=4;
a=x/y;
printf(Value of a=%f, a);
}
Output..
Value of a=1.000000
The answer is coming 1.000000
instead of 1.500000
Here x and y both are integers, so
x/y yields an integer.
When it is stored in float a, it
converted to float result (1.000000)
Solution 1
One solution could be converted x
and y to float
So, it will be
float x=6, y=4;
.
Solution 2
Another solution could be type casting
for example:
void main(){
float a;
int x=6,y=4;
a=(float)x/y;
printf(Value of a=%f,a);
Output
Here is the output of the following
program
Value of a=1.500000
float a;
int x=6,y=4;
a=(float)(x/y);
printf(Value of a=%f,a);
Output
Value of a=1.000000
Here, a= (float)x/y , the expression
(float) convert the variable x from
type int to type float before being
used in division operation.
Output
Value of a on type casting=7
Value of a=7.5
Hierarchy of operations
1st priority : *, / , %
2nd priority : + , 3rd priority : =
Some notes
in case of a tie between operators of
same priority preference is given to
the operator that occurs first.
for example: z=a*b+c/d
Logical operators
&& : AND
|| : OR
! : NOT
Relational operators
< : less than
> : greater than
<= : less than or equal to
>= : greater than or equal to
== : equal to
!= : not equal to
of the
can be
The if Statement
The if statement is a one-way decision
statement. It is written as
if(condition)
statement;
If the condition is true, then the statement is
executed.
If the condition is not true then the
statement is not executed; instead the
program skips past it.
More than one statement is also possible,
then they must be enclosed within curly
braces, { }.
The if Statement
Example:
grade >= 60
false
int grade;
if ( grade >= 60 )
printf( "Passed\n");
true
print Passed
false
grade >= 60
Print Failed
int grade;
if ( grade >= 60 )
printf( "Passed\n");
else
printf( "Failed\n");
true
Print Passed
True statement1
False
condition2
True
statement2
False
condition3
False
.
.
statementN
Exit
True statement3
int grade;
if ( grade >= 90 )
printf( A );
else if ( grade >=
printf( B );
else if ( grade >=
printf( C );
else if ( grade >=
printf( D );
else
printf( F );
80 )
70 )
60 )
The if Statement
(03/06/14)
Example:
int grade;
if ( grade >= 90 )
printf( A );
if ( grade >= 80 )
printf( B );
if ( grade >= 70 )
printf( C );
if ( grade >= 60 )
printf( D );
If ( grade<60 )
printf( F );
>= 60 )
"Passed\n");
"Failed\n");
decision statement.
Unlike the multiple decision statement that can
be created using if-else, the switch statement
evaluates the conditional expression and tests it
against numerous constant values.
The branch corresponding to the value that the
expression matches is taken during execution.
The value of the expressions in a switch-case
statement must be an ordinal type i.e. integer,
char etc. Float and double are not allowed.
switch( expression )
{
case 1:
statement1;
break;
case 2:
statement2;
break;
case 3:
statement3;
break;
default :
statement4;
break;
}
switch variable
equals 1st case
constant
True
Body of
1st case
False
switch variable
equals 2nd
case
constant
False
True
Body of
2nd case
Body of
default
Exit
sequence
of
program
execution
by
unconditionally transferring control to some
other part of the program.
It is written as
goto label;
Here label is an identifier used to label the
target statement to which the control would be
transferred. The target statement will appear as:
label: statement;
Problems
Least Challenging
1.
2.
3.
4.
Problems
Challenging
1.
2.
0-200
Tk. 0.50 per unit
201-400 Tk. 100 plus Tk. 0.65 per unit excess of 200
401-600 Tk. 230 plus Tk. 0.80 per unit excess of 400
601 and above
Tk. 390 plus Tk. 1.00 per unit excess of 600
Write a program which will read the customer number and power
consumed and print the amount to be paid by the customer.
Problems
3.
4.