Operators, Decision Making and Branching
Operators, Decision Making and Branching
Operators, Decision Making and Branching
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 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
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..
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
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
}
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