0% found this document useful (0 votes)
13 views63 pages

C Programming Operators and Control Statements

This document serves as an introduction to C programming, focusing on operators, expressions, and control statements. It covers various types of operators, including arithmetic, relational, logical, and assignment operators, as well as decision-making structures like if statements, switch cases, and loops. Additionally, it explains the use of break and continue statements, and the goto statement for control flow in C.

Uploaded by

gauravis147
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views63 pages

C Programming Operators and Control Statements

This document serves as an introduction to C programming, focusing on operators, expressions, and control statements. It covers various types of operators, including arithmetic, relational, logical, and assignment operators, as well as decision-making structures like if statements, switch cases, and loops. Additionally, it explains the use of break and continue statements, and the goto statement for control flow in C.

Uploaded by

gauravis147
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INTRODUCTION TO C PROGRAMMING

Course Code: 1BPLC205E/105E

MODULE-2

Nithin Kumar Heraje


Asst Prof., Dept of CSE
AJIET
Operators
• Operator: An operator is a symbol (or a token) that
specifies the operation to be performed on various
types of operands.
OR
• An operator is a symbol that tells the compiler to
perform specific mathematical or logical functions.

• Operand: A variable that holds the value.


• Expression: A sequence of operands and operators
that reduces to a single value is known as expression
• Types of Operators
• Arithmetic Operators
• Relational Operators
• Logical Operators Binary Operator
• Bitwise Operators
• Assignment Operators
• Increment & Decrement Operator Unary Operator
• Conditional operator (a ? b : c) Ternary Operator
Arithmetic Operators
• An arithmetic operator performs mathematical operations
Ex:- + Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus(mod) x%y
Assignment operators in C

• The = operator is also called as the assignment operator.


• It is used to assign a constant value to the variable.
• Syntax: variable = constant ; Example: a=100;

• Or variable = another_variable ; Example: a=b;

• Or variable = expression; Example: a=1*b


Assignment operators in C

• Short hand assignment operators


• The syntax of short hand assignment statement is :
variable op= expression;
• Here the op may be operators such as +,-,/,*,%,<<,>>,
!,^ etc.
• Example: x + = 1 is same as x = x + 1
Assignment operators in C
Assignment operators in C

• Multiple Assignment statement


• A statement using which a value or a set of values are assigned to
different variables is called multiple assignment statement.
• Example : i=j=k=10;
Increment and Decrement Operators:
• Increment operator:
• The ++(double plus) operator is also called as
incrementation operator.
• This operator is used to increment the value of a
variable by 1.
Increment and Decrement Operators:
• post and pre increment operators:
• If the ++ operator is placed before the operand then
it is called as pre increment operator.
• If the ++ operator is placed after the operand then it
is called as post increment operator.
Increment and Decrement Operators:
• post and pre increment operators:
• Example 1:
• m = 5;
• y = ++m; (prefix)
• In this case the value m will be incremented to 6 first and then it will
be assigned to y.
• Therefore at the end of the execution, the value of y and m will be 6.

• Example 2:
• m = 5;
• y = m++; (postfix)
• In this case the values of m will gets assigned first to y and then it
will gets incremented.
• Therefore at the end of the execution, the value of y will be 5 and m
will be 6.
Increment and Decrement Operators:
•Decrement operator:
• The --(double minus) operator is also called as decrementation
operator.
• This operator is used to decrement the value of a variable by 1.
Increment and Decrement Operators:
•Decrement operator:
• post and pre decrement operators:

• If the -- operator is placed before the operand then it is called


as pre decrement operator.
• If the -- operator is placed after the operand then it is called as
post decrement operator.
Increment and Decrement Operators:
• Example 1:
• m = 5;
• y = --m; (prefix)
• In this case the value m will be decremented to 4 first and then it will be
assigned to y. Therefore at the end of the execution, the value of y and m
will be 4.
• Example 2:
• m = 5;
• y = m--; (postfix)
• In this case the values of m will gets assigned first to y and then it will gets
decremented. Therefore at the end of the execution, the value of y will be
5 and m will be 4.
Relational Operators
• A relational operator checks the relationship
between two operands.
Logical Operators
• An expression containing logical operator returns
either 0 or 1 depending upon whether expression
results true or false.
• Commonly used in decision making in C
programming.
Let R1 and R2 be the relations then

Consider ,
• T or True as 1 and F or False as 0
Conditional Operator (?:)
• A conditional operator is a ternary operator, that is, it
works on 3 operands.

• Syntax:
conditionalExpression ? Statement1 : statement2;

Ex:
z=(a>b)?a:b;
Precedence and Order of Evaluation

Precedence specifies the order in which the


different operators in an expression should be
evaluated.
Associativity specifies the order in which the
operators with the same precedence should be
evaluated.
Evaluation of expression
• Rules for evaluation of expression
• First, parenthesized sub expression from left to right are evaluated.
• If parentheses are nested, the evaluation begins with the innermost sub-
expression.
• The precedence rule is applied in determining the order of application of
operators in evaluating sub-expression.
• The associativity rule is applied when two or more operators of the same
precedence level appear in a sub-expression.
• Arithmetic expressions are evaluated from left to right using the rules of
precedence.
• When parentheses are used, the expressions within parentheses assume
highest priority.
Precedence and Order of Evaluation
Evaluation of expression
• a=3*(4%5)/2 // parenthesized sub expression from left to right
• a=3*4/2 // multiplication and division from left to right

• a=12/2
• a= 6
Evaluation of expression
• Z= 9 – (12 / (3 + 3) * 2) – 1;
• Z= 9 – (12/ 6 * 2) – 1;
• Z= 9 – (12 / 12) – 1;
• Z= 9 – 1– 1;
• Z= 8– 1;
• Z= 7;
X=3*4+5*6

X= 3 * 4 % ( 5 / 2 )

X= 3 * 4 % 5 / 2

X= 3 * ( ( 4 % 5 ) / 2 )
Decision Making, Branching,
Looping
C SUPPORTS MAINLY THREE TYPES OF CONTROL STATEMENTS
Decision Making and Branching
• if Statement
• switch Statement
• Conditional Statement
• goto Statement
Simple if
Flowchart
Syntax:
if(test expression)
{
Statement-Block;
}
Statement-x;
Write a c program to accept the a number and if the number is greater than 10 then
add 100 to it and show the final result.
#include<stdio.h>
void main( )
{
int num;
printf(“Enter an integer\n”);
scanf(“%d”,&num);
if(num>10)
{
num=num+100;
}
printf(“The result is:%d\n”,num);
}
if… else Statement

Syntax:
if(test expression)
{
true-block Statement(s);
}
else
{
false-block Statement(s);
}
statement-x;
Write a c program to check if the entered number is even or odd
#include<stdio.h>
void main( )
{
int num;
printf(“Enter a non zero integer\n”);
scanf(“%d”,&num);
if(num%2==0)
{
printf(“The entered number is even\n”);
}
else
{
printf(“The entered number is odd\n”);
}
}
Nesting of if…else Statements
Syntax Flowchart
output1 output2 output3
Enter your age Enter your age Enter your age
17 22 26
Not Eligible to apply for Eligible to apply for 2- Eligible to apply for all
any types of driving wheeler and LMV type of types of driving license
license driving license

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING, CANARA ENGINEERING COLLEGE - MANGALURU


The else if Ladder
Flowchart
output1
Enter student percentage
71
FCD
output2
Enter student percentage
63
FC

output4 output3
Enter student percentage Enter student percentage
30 55
Fail SC
Switch Case
Rules for Switch Statement
• Case Label must be unique
• Case Labels must ends with Colon(:)
• Case labels must have constants / constant expression
• Case label must be of Integer,Character or string
• Case label should not be ‘floating point number ‘
• Switch case should have at most one default label
• Default label is Optional
• Default can be placed anywhere in the switch
• Break Statement takes control out of the switch
• Two or more cases may share one break statement
• Nesting ( switch within switch ) is allowed.
• Relational Operators are not allowed in Switch Statement.
Simple example to understand the working of a switch case statement in C program
#include <stdio.h>
int main()
{
int num=2;
switch(num)
{
case 1:
printf("Case1: Value is: %d", num);
break;
case 2:
printf("Case2: Value is: %d", num);
break;
case 3:
printf("Case3: Value is: %d", num);
break;
default:
printf("Default: Value is: %d", num);
break;
}
return 0;

}
Looping
• C language provides a concept called loop, which helps in executing one or more
statements up to desired number of times.
• Loops are used to execute one or morestatements repeatedly.
There are 3 types of loops in C programming:
1) while loop
2) for loop
3) do while loop
FOR LOOP

•A for loop statement can be used to execute s set of statements


repeatedly as long as a given condition is true.
•The syntax is shown below:
for(expr1;expr2;expr3)
{
statement1;
}
expr1 contains initialization statement; expr2 contains
limit test; expr3 contains updating expression
•Firstly, expr1 is evaluated. It is executed only once.
• Then, expr2 is evaluated to true or false.
• If expr2 is evaluated to false, the control comes out
of the loop without executing the body of the loop.
• If expr2 is evaluated to true, the body of the loop
(i.e. statement1) is executed.
•After executing the body of the loop, expr3 is evaluated.
•Then expr2 is again evaluated to true or false. This cycle
continues until expression becomes false.
• The flow diagram of for loop:
WHILE LOOP
•A while loop statement can be used to execute a set of
statements repeatedly as long as a given condition is true.

• The syntax is shown below:

while(expression)
{
statement1;
statement2;
}
• Firstly, the expression is evaluated to true or false.

• If the expression is evaluated to false, the control comes


out of the loop without executing the body of the loop.

• If the expression is evaluated to true, the body of the loop


(i.e. statement1) is executed.

• After executing the body of the loop, control goes back to


the beginning of the while statement and expression is
again evaluated to true or false. This cycle continues until
expression becomes false.
The flow diagram is shown below:
Output:
Sum=55
DO WHILE LOOP
•When we do not know exactly how many times a set of statements have to be
repeated, do-while statement can be used.
• The syntax is shown below:
do
{
statement1;

}while(expression);

• Firstly, the body of the loop is executed.


i.e. the body of the loop is executed at least once.
• Then, the expression is evaluated to true or false.
• If the expression is evaluated to true, the body of the loop (i.e. statement1) is
executed
• After executing the body of the loop, the expression is again evaluated to true or
false.
This cycle continues until expression becomes false.
BREAK STATEMENT

•The break statement is jump statement which


can be used in switch statement and loops.
• The break statement works as shown below:
1) If break is executed in a switch block,
 the control comes out of the switch block and the statement
following the switch block will be executed.
2) If break is executed in a loop,
 the control comes out of the loop and the statement following
the loop will be executed.
CONTINUE STATEMENT

•During execution of a loop, it may be necessary to


skip a part of the loop based on some condition. In
such cases, we use continue statement.

•The continue statement is used only in the loop to


terminate the current iteration.
GOTO STATEMENT
•The goto statement is used to transfer control to a specified label.
•The 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 ( : ).
•The label is placed immediately before the statement where the
control is to be transferred.

The syntax is shown below:


goto label;
-------
-------
label :

You might also like