Operators, Decision Making and Branching

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

Operators, Decision

Making and Branching


Nazirul Hasan
Lecturer, CSE, KUET
Operator
• It is a symbol that tells the computer to perform certain mathematical
or logical manipulations.
• Example -
Operator

2 + 3

Operand
Operator
• C operators can be classified into a number of categories:
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Increment and decrement operators
• Conditional operators
• Bitwise operators
• Special operators
Arithmetic Operators
• Most C programs perform arithmetic calculations
Arithmetic Operators
• The arithmetic operators are all binary operators
• The expression 3+7 contains binary operator + and two operands 3
and 7
• Integer division yields an integer result. 17/4 is 4
• The modulus operator means the remainder after a division
operation. 17%4 is 1
• The % can not be used with real operand.
A simple program
int main(){
int months,days;
printf(“enter days\n”);
scanf(“%d”,&days);
months= days/30;
days=days%30;
printf(“months=%d and days=%d”,months,days);
return 0;
}
Rules of Operator Precedence

1. Expressions or portion of expressions contained in pairs of


parentheses are evaluated first. In case of nested parentheses, the
inner most parentheses will be evaluated first.
2. Multiplication, division and modulus operations are evaluated next.
If an expression contains several *, / or % operations, evaluations
proceed from left to right. These operators are on the same level of
precedence.
Decision Making: Relational Operators

• Executable C expressions either do actions (calculations or input or output)


or make a decision
• For example, in a program where the user will put his mark and want to
know if he passed or failed, you will have to do three things-
1. Take the mark from the user. Action
2. Compare the mark with a predefined pass mark range. Decision
3. Provide that decision to user. Action
Decision Making: Relational Operators
Decision Making: Relational Operators

• There is a subtle difference between == and =. In case of =, it is


associated from right to left. a=b means the value of b is assigned to a
• The relational operators have the same level of precedence and they
associate left to right
• Only equality operators (== and != )have a lower level of precedence
than others and they also associate left to right
Decision Making: Relational Operators
#include <stdio.h>
int main(){
int num1, num2;
printf(“Enter two numbers to compare: ”);
scanf(“%d %d”, &num1, &num2);
if(num1 == num2)
printf(“%d and %d are equal”, num1, num2);
if(num1 != num2)
printf(“%d and %d are not equal”, num1, num2);
if(num1 < num2)
printf(“%d is less than %d”, num1, num2);
if(num1 > num2)
printf(“%d is greater than %d”, num1, num2);
if(num1 <= num2)
printf(“%d is less than or equal to %d”, num1, num2);
if(num1 >= num2)
printf(“%d is greater than or equal to %d”, num1, num2);
return 0;
}
Logical operators
• Logical AND &&
• Logical OR ||
• Logical NOT !
• The logical operator && and || are used for testing more than one
conditions and make decisions.
• a>b && x==10
Logical operator(truth table)
Op1 Op2 Op1 && Op2 Op1||Op2

1 1 1 1

1 0 0 1

0 1 0 1

0 0 0 0
Assignment operator
Statement with simple Statement with shorthand
assignment operator operator
a=a+1 a+=1

a=a-1 a-=1

a=a*(n+1) a*=(n+1)
Increment and Decrement Operators
• C provides the unary increment operator ++ and unary decrement
operator – –
• If a variable a is incremented by 1, the increment operator ++ can be
used instead of expression a=a+1
• But the meaning differs based on the place of the operator to the
variable
• If unary increment/ decrement operators are placed in front of the
variable, they are called preincrement/ predecrement operators
otherwise they are called postincrement/ postdecrement operators.
Increment and Decrement Operators
Increment and Decrement
Operators

What is the value of c and d after executing above


statements line by line.
Conditional operator

The Conditional opeartor works as follows:


• The first expression conditionalExpression is evaluated at first. This
expression evaluates to 1 if it’s true and evaluates to 0 if it’s false.
• If conditionalExpression is true, expression1 is evaluated.
• If conditionalExpression is false, expression2 is evaluated.
Conditional operator
Bitwise operator
• It is used for manipulating data at bit Operator meaning
level & Bitwise AND
• Used for testing bits or shifting them
| Bitwise OR
right or left.
^ Bitwise
XOR
<< Shift left
>> Shift right
~ 1’s
complement
Special operator
• Comma operator
• Sizeof operator:
• It returns the no of bytes the operand occupies.
• m=sizeof(sum);
• n=sizeof(int);
Decision making and
branching
Control or decision making statement

• C language supports the following statements known as control or


decision making statements.

1. if statement
2. switch statement
3. conditional operator statement
4. goto statement
The if statement
• The if statement is used to control the flow of execution of statements and is of the form
• If(test expression)
{
statement;
statement;
................;
}

• Eg:
if(bank balance is zero)
Borrow money
Cont..

• The if statement may be implemented in different forms depending


on the complexity of conditions to be tested.

1. Simple if statement
2. if…..else statement
3. Nested if…..else statement
4. elseif ladder
General form and flow chart of
simple if statement
• The general form of a
simple if statement:
If(test expression)
{
Statement-block;
}
Statement-x;
The if statement
#include <stdio.h>
main(){
int a,b;
printf("take two inputs:");
scanf("%d %d",&a,&b);
if(a>b)
{
printf(“%d is greater“,a);
}
if(b>a)
{
printf(“%d is greater“,b);
}

printf(“end of program”);
}
THE IF…ELSE STATEMENT

• The if….else statement is an extension of simple if


statement. The general form is

If(test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x
The if else statement
#include <stdio.h>
main(){
int a,b;
printf("take two inputs:");
scanf("%d %d",&a,&b);
if(a>b)
{
printf(“%d is greater“,a);
}
else
{
printf(“%d is greater“,b);
}
printf(“end of program”);
}
NESTING OF IF…..ELSE STATEMENTS

• When a series of decisions are involved, we may have


to use more than one if….else statements, in nested
form as follows
Nesting of if else statement
void main(){
int a,b,c;
printf("take three nmbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{printf("%d is greater",a);}
else
{printf("%d is greater",c);}
}
else
{
if(b>c)
{printf("%d is greater");}
else
{printf("%d is greater",c);}

}
Dangling else problem
• When a matching else is not available for an if.
• Always match an else to the most recent unmatched if
• “else is always paired with the most recent unpaired if”
• if (condition)
if (condition)
if (condition)
else
printf("dangling else!\n"); /* dangling else, as
to which if statement, else clause associates */
The else if ladder

When a multipath decision is involved then we use else if ladder.

 A multipath decision is a chain of ifs in which the statement associated


with each else is an if.

It takes the following general form,


The else if ladder
#include<stdio.h>
int main(){
int grade;
printf("Enter your grade: ");
scanf("%d",&grade);
if (grade>90)
printf("Grade: A");
else if (grade>80)
printf("Grade: B");
else if (grade>70)
printf("Grade: C");
else if (grade>60)
printf("Grade: D");
else if (grade>50)
printf("Grade: E");
else
printf("Grade: F");
return 0;
}
References
• Web
• http://www.tutorialspoint.com/cprogramming/

You might also like