Professional Documents
Culture Documents
Operator & Selection
Operator & Selection
Operator.
Type Conversion.
Control Instruction.
Objectives
What is Operator?
Types of Operator.
Precedence of Operator.
Associativity of Operator.
Type Casting.
Sequence Control Instruction.
Selection or Decision Control Instruction (if-else).
Case Control Instruction(switch case).
Repetition or Loop Control Instruction.
What is Operator?
void
void main()
main()
{{
int
int x;
x;
xx == (int)8
(int)8 // 6;
6;
printf(“x=%d”,x);
printf(“x=%d”,x);
}}
In the above example, the usual result of 8/6 should be 1.3333 but
since the integer constant ‘8’ typecasted it, the x value would be
"1". The later part is simply truncated. The output of this program
is “x=1”.
Types Casting(Contd.)
There are two ways of casting data types in C:
automatic (implicit)
given (explicit)
Implicit Casting (automatic transformation) works in a way that a variable
(operand) of data type that is smaller in length than data type of second
variable (operand), transforming internally to variable of data type with
longer number length. It may sound mixed up, but here is an example:
short int -> int -> unsigned int ->long int -> unsigned long int -> float ->
double -> long double.
Explicit Casting (given transformation) of data types has higher priority then
automatic transformation. General declaration of explicit (given) cast (cast
operator):
(data_type) operand.
Operand can be variable or phrase.
Types Casting(Contd.)
Implicit Type Conversion
— Converted by compiler automatically.
int
int a;
a;
unsigned
unsigned long long b; b;
float
float f,f, g;
g;
double
double d; d;
gg == aa ++ f;f; //// aa transforms
transforms to to float
float
dd == aa ++ b;
b; //// aa and and bb transform
transform toto unsigned
unsigned long,
long, adding
adding is
is produced
produced in
in
unsigned
unsigned long long //// domain
domain andand then
then the
the result
result type
type unsigned
unsigned long
long is
is
transformed
transformed to to double
double
Types Casting(Contd.)
Explicit type casting
— Converted by programmer using cast operator.
aa == (int)
(int) c;
c;
b
b == (double)d
(double)d ++ cc;;
Control Instructions
else
statement ;
if-else statements (Contd.)
#include
#include <stdio.h>
<stdio.h>
void
void main
main (( ))
{{
int
int aa == 1,
1, b
b == 2,
2, cc ;;
if
if (a
(a >> b)
b)
cc == a;
a;
else
else
cc == b;
b;
}}
if-else statements (Contd.)
• Flow chart of the if/else selection structure:
Start
Start
Taking
Taking inputs
inputs as
as
integer
integer variables
variables
a,b,c
a,b,c
true false
a>b
c=a
c=a c=b
c=b
End
End
if-else statements (Contd.)
}}
if-else statements (Contd.)
• Simple Program Using if / else if / else :
#include <stdio.h>
void main ( )
{
int a , b ;
printf ("Enter values for a and b > ") ;
scanf ("%d%d", &a, &b ) ;
if ( a < b )
printf ("a is less than\n") ;
else if ( a == b )
printf (" a is equal to b\n") ;
else
printf ("a is larger than b\n") ;
}
if-else statements (Contd.)
• Simple Program Using if / else if / else :
#include <stdio.h>
void main ( )
{
int a , b ;
printf ("Enter values for a and b > ") ;
scanf ("%d%d", &a, &b ) ;
if ( a < b )
printf ("a is less than\n") ;
else if ( a == b )
printf (" a is equal to b\n") ;
else
printf ("a is larger than b\n") ;
}
if-else statements (Contd.)
• Nested if/else structures :
— Pseudocode for a nested if/else structure:
IfIf (student’s
(student’s belongs
belongs to
to MCA
MCA department)
department)
{{
Print
Print “MCA”;
“MCA”;
IfIf (student’s
(student’s grade
grade is
is greater
greater than
than or
or equal
equal
to
to 60)
60)
{{
Print
Print “Pass.”;
“Pass.”;
}}
else
else
{{
Print
Print “Fail.”;
“Fail.”;
}}
}}
else
else
{{
Print
Print “Not
“Not belongs
belongs to
to MCA”;
MCA”;
}}
if-else statements (Contd.)
• Simple program of Nested if/else structures :
#include
#include <stdio.h>
<stdio.h>
void
void main
main (( ))
{{
Int
Int
IfIf (student’s
(student’s belongs
belongs to
to MCA
MCA department)
department)
{{
Print
Print “MCA”;
“MCA”;
IfIf (student’s
(student’s grade
grade isis greater
greater than
than or
or equal
equal to
to 60)
60)
{{
Print
Print “Pass.”;
“Pass.”;
}}
else
else
{{
Print
Print “Fail.”;
“Fail.”;
}}
}}
else
else
{{
Print
Print “Not
“Not belongs
belongs to
to MCA”;
MCA”;
}}
}}
Ternary Operator
if (x > 0)
y=1
else
y = -1;
is equivalent to
y = (x > 0) ? 1 : -1;
Int
Int num=10;
num=10;
ifif (num
(num %% 22 ==
== 0)
0)
printf(“%d is even”,num);
printf(“%d is even”,num);
else
else
printf(“%d
printf(“%d is
is odd”,num);
odd”,num);
is
is equivalent
equivalent to:
to:
printf(“(num
printf(“(num %% 22 ==
== 0)?
0)? num
num ++ “is
“is even”
even” ::
num
num ++ “is
“is odd”);
odd”);
The Case Control Structure