Você está na página 1de 37

Programming With C

Condition and Type Casting

Integer and float


conversions
Operation between int and int always
yields an int result.
Operation between real and real
always yields an real result.
Operation between int and real
always yields an real result.

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 :

Sometimes it is needed to force the compiler to


explicitly convert the value of an expression to a
particular data type.

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

What will be the output of the following


program
for example
void main()
{

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.

More about Type Casting


Another example of type casting
void main(){
float a=7.5;
printf(value of a on type casting=%d,
(int)a);
printf(value of a=%f,a);

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

If there are more than one set of


parenthesis. Must use pairs of
parenthesis.

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

Hierarchy of all operators


1st priority : ! (logical)
2nd priority : *, /, % (arithmetic)
3rd priority : +, - (arithmetic)
4th priority : <, >, <=, >= (relational)
5th priority : ==, != (relational)
6th priority : && (logical)
7th priority : || (logical)
8th priority : = (assignment)

Decision Control Structures


Decision control instructions control or
alter the flow of execution
program.
A decision control instruction
implemented in C using:
The if statement
The if-else statement
The switch-case statement
The conditional operators

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

The if-else Statement


The if-else statement is a two-way decision
statement. It is written as
if(condition)
statement1;
else
statement2;

If the condition evaluates to true, then


statement1 is executed.

If the condition evaluates to false, then


statement2 is executed.

The if-else Statement


Example:

false

grade >= 60

Print Failed

int grade;
if ( grade >= 60 )
printf( "Passed\n");
else
printf( "Failed\n");

true

Print Passed

The else-if Statement


A multiple decision construct is also possible
using else-if statement. It is written as
if( condition1 )
statement1;
else if( condition2 )
statement2;
else if( condition3 )
statement3;
.
.
else
statementN;

Once condition is met, rest of statements


skipped.

The else-if Statement

Flowchart of the else-if construct


condition1

True statement1

False
condition2

True

statement2

False
condition3
False

.
.
statementN
Exit

True statement3

The else-if Statement


Example:

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 );

Ternary conditional operator


Ternary conditional operator (?:)
Takes three arguments (condition, value if true,
value if false)
Example:
if ( grade
printf(
else
printf(

>= 60 )
"Passed\n");
"Failed\n");

This could be written


printf( "%s\n", grade >= 60 ? "Passed" : "Failed" );

Or it could have been written


grade >= 60 ? printf( Passed\n ) :
printf( Failed\n );

The switch-case statement


The switch-case statement is a multi-way

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.

The switch-case statement


Syntax:

switch( expression )
{
case 1:
statement1;
break;
case 2:
statement2;
break;
case 3:
statement3;
break;
default :
statement4;
break;
}

The switch-case statement


Even if there are multiple statements

to be executed in each case there is


no need to enclose them within a
pair of braces (unlike if, and else)
The default clause is an optional
clause that is matched if none of the
constants in the case statements can
be matched.

The switch-case statement


Flowchart of the switch-case construct

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

The switch-case statement


Example:
char grade;
scanf(%c,&grade);
switch( grade )
{
case 'A' :
printf( "Excellent" );
break;
case 'B' :
printf( "Good" );
break;
case 'C' :
printf( "OK" );
break;
case 'D' :
printf( "Mmmmm...." );
break;
case 'F' :
printf( "You must do better than this" );
break;
default :
printf( "What is your grade anyway?" );
break;
}

The goto statement


The goto statement is used to alter the normal

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;

The goto statement


Example:
void main()
{
int num=1, sum = 0;
label1:
if(num <= 100 )
{
sum += num;
num++;
goto label1;
}
printf(Summation = %d\n,sum);
}

Problems
Least Challenging
1.
2.
3.
4.

An integer number is entered through the


keyboard. Write a C program to find out whether
it is an odd or even number.
An integer number is entered through the
keyboard. Write a C program that will determine
whether it is a prime number or not.
Three integer numbers are entered through the
keyboard. Write a C program to find the largest
of these three numbers.
Write a C program using conditional operators to
determine whether a year entered through the
keyboard is a leap year or not.

Problems
Challenging
1.

A five-digit number is entered through the keyboard. Write a


program to obtain the reversed number and to determine
whether the original and reversed numbers are equal or not.

2.

The PDB charges its domestic customers for consumption of


electricity as follows:
Consumption

Unit Rate of Charge

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.

A company insures its drivers in the following cases:


- If the driver is married.
- If the driver is unmarried, male and above 30 years of age
- If the driver is unmarried, female and above 25 years of age
In all other cases the driver is not insured. If the marital status,
sex and age of the driver are entered through the keyboard, write
a program to determine whether the driver is to be insured or not.
According to the Gregorian calendar, it was Monday on the date
01/01/1900. If any year is input through the keyboard, write a
program to find out what is the day on 1st January of this year.

Você também pode gostar