Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

 Session II

 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?

 Operators form expressions by joining individual


constants, variables.

 Every Operator performs different type of operations.


Types of Operator
Arithmetic + - * / %
Relational > >= < <= == !=
Logical && || !
Conditional ?:
Assignment = += -= *= /= %= &= ^= |= >>= <<= >>>=

Postfix increment / decrement ++ --


Casting (Data Type)
C Operator
Subscript []
Bitwise & | ^ ~ << >> >>>
Parentheses ()
Coma Operator: ,
Address of &
Indirection *

Structure-access Selection . ->


Operator Precedence & Associativity
Types Casting
Typecasting involves converting an expression of a given
type into another type. In C, you can perform typecasting
by placing the data type name in parentheses and placing
this in front of the value. For example:

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.

— Widening Type Conversion.

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.

— Narrowing Type Conversion.

aa == (int)
(int) c;
c;

b
b == (double)d
(double)d ++ cc;;
Control Instructions

• The ‘Control Instructions’ provide some technique by which


programmer can specify the order in which the various
instructions in a C Program to be executed by the computer.
• In other words the control instructions determine the ‘flow of
control’ in a C program.
• There are four types of control instructions in C:
— Sequence Control Instruction.

— Selection or Decision Control Instruction (if-else).

— Case Control Instruction(switch case).

— Repetition or Loop Control Instruction.


Control Instructions (Contd.)
• Sequence Control Instructions:
— The sequence control instruction ensures that the
instructions are executed in the same order in which
they appear in the program.

• Decision and Case Control Instructions:


— Decision and case control instructions allow the
computer to take a decision as to which instruction is
to be executed.

• Repetition or Loop Control Instructions:


The Repetition or loop control instruction helps
computer to execute a group of statements
repeatedly.
if-else statements

• A decision Control instruction can be implemented in C


using:
— The if statement.
— The else statement.
— The conditional operator.
if-else statements (Contd.)

• if / else if / else Selection Structures:


— Selection structures permit alternate actions based on
the evaluation of logical expressions.

— The logical expressions evaluate as either true or false,


and the action takes place if and only if the expression
is true.

— When the expression is false, the program may take


alternate action(s), or it may evaluate another
expression to determine what to do next.
if-else statements (Contd.)

• if / else if / else Selection Structures:


— A simple if structure is called a single-selection
structure because it either selects or ignores a single
action.

— The if / else structure is called a double-selection


structure because it selects between two different
actions.

— Nested if / else structures test for multiple cases by


placing if / else structures inside other if / else
structures.
if-else statements (Contd.)

• if / else Selection Structures:


— Syntax for the if selection structure is as follows:

if (this logical expression is true)


statement ;

— Syntax for the if / else selection structure is as follows:

if (this logical expression is true )


statement ;

else
statement ;
if-else statements (Contd.)

• if / else Selection Structures:


— A very simple program:

#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 if / else Selection Structures:


— Syntax for the if / else if / else selection structure is as
follows:

if (this logical expression is true)


statement ;
else if (this logical expression is true)
statement ;
else
statement ;
if-else statements (Contd.)
• if / else if / else Selection Structures for compound
Statements:
— The actual syntax for the multiple if / else if / else
selection structure for compound statements is as
follows:ifif (this
(this logical
logical expression
expression is
is true)
true)
{{
statement1
statement1 ;;
statement2;
statement2;
…………….....
…………….....
}}
else
else ifif (this
(this logical
logical expression
expression is
is true)
true)
{{
statement1
statement1 ;;
statement2;
statement2;
…………….....
…………….....
}}
else
else
{{
statement1
statement1 ;;
statement2;
statement2;
…………….....
…………….....

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

General Syntax for conditional operator

(Boolean Expression) ? expression1 : expression2


Ternary Operator(contd.)

 Simple program of ternary operator:

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

 C provides a special control statement that


allows us to handle cases effectively rather
than using a series of if statements.

 Two types of control statement


 Switch
 goto
Decision using switch
#include<stdio.h> Output:
{ I’m in case 2
Int i=2; I’m in case 3
Switch(i) I’m in default
{
Case1: printf(“I’m in case 1”);
Case2: printf(“I’m in case 2”);
Case3: printf(“I’m in case 3”);
default: printf(“I’m in default”);
}
}
Tricks on switch:
 When we run a program containing switch, the integer
expression following the keyword is evaluated.
 When a match is found, the program executes the statement
following that case and all subsequent case and default
statement.
 If no match is found, only the statement following the default are
executed.
 If we want a particular case should get executed then break
statement is used.
 There is no need of break statement after the default since
control comes out of the switch anyway.
The use of goto keyword
#include<stdio.h>
Void main()
Output:
{ Enter the number of goals scored against
Int goals; India
Printf(“Enter the number of goals scored against To err is human!
India”); Enter the number of goals scored against
Scanf(“%d”,&goals); India
If(goals<=5) About time soccer players learnt C
Goto sos; and said goodbye!adieu!to soccer
Else
{
Printf(“About time soccer players learnt C\n”);
Printf(“and said goodbye!adieu!to soccer”);
Exit();/*terminates program execution*/
}
sos:
Printf(“To err is human!”);
}
A few remarks on goto:

 The usage of the goto keyword should be


avoided as it usually violates the normal flow
of execution.
 The only programming situatuion in favor of
using goto is when we want to take the
control out of the loop that is contained in
several other loops.

You might also like