0% found this document useful (0 votes)
54 views8 pages

Increment & Decrement Operators (++, - ) : Example (X Y && X Z) (!FLAG && T 10)

This document discusses operators, conditional statements, and control statements in C programming. It provides examples of: 1) Different types of operators in C like arithmetic, relational, logical, and bitwise operators. 2) Conditional statements like if-else and switch statements. 3) Looping statements like for, while, do-while loops. 4) Example C programs to add two numbers without addition operator, print Floyd's triangle, split a number into digits, and find the maximum of two numbers using a conditional operator.

Uploaded by

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

Increment & Decrement Operators (++, - ) : Example (X Y && X Z) (!FLAG && T 10)

This document discusses operators, conditional statements, and control statements in C programming. It provides examples of: 1) Different types of operators in C like arithmetic, relational, logical, and bitwise operators. 2) Conditional statements like if-else and switch statements. 3) Looping statements like for, while, do-while loops. 4) Example C programs to add two numbers without addition operator, print Floyd's triangle, split a number into digits, and find the maximum of two numbers using a conditional operator.

Uploaded by

sathiyavijayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Operators, Conditional Statements, Control Statements, Example C Programs

Write a C program to add two numbers without using addition operator.


Write a C program to print Floyds triangle
Write a C program to Split number into digits
Write a C program to find maximum of two numbers using Conditional Statement

Operators
There are four main classes of operators
1) Arithmetic 2) relational 3) logical 4) bitwise 5) other special
operators
1. Arithmetic operator : +,-,*,/,%
2. Assignment operators ( = )
variable = expression ;
(Multi assignment also possible such as x=y=z=20;)
3. Increment & Decrement operators ( ++, -- )
4. additional assignment operators ( +=, -=, *=, /=, %= )
Example meaning
a+=10 a=a+10
b-=c b=b-c
x*=10 x=x*10
5. unary operator - C include a class of operator that act upon a single operand
to produce a new value. Such a operators are known as unary operators
Example
-735 , -x - (b * b 4 * a * c)
priority
high ++, --, -(unary operator)
*, /, %
Low +, -

6. Relational operators: it is used to test condition variable or expression. All


relational operator will take two operands as arguments. < , <=, >,>=, ==,!=
7. Logical operators It is used to combine more than one relational
conditions statement
&& AND, || OR, ! NOT
EXAMPLE
(X > Y && X > Z) (!FLAG && T> 10)

8. The conditional operator (or) Ternary operator ( ? : ) The simple


conditional operation can be carried out with conditional operator.
General form : exp1? exp 2: exp 3;
If exp1 return true, exp2 is evaluated, otherwise exp3 evaluated.
It is similar to simple-if ,the advantage over if here is ,that you can use
ternary operator collaboration with printf
E.g:
printf(%d is greater than %d ,%d ,a>b?a:b,a,b);
big=(a>b?a:b);
9. bit wise Operators
^ (Exclusive or)
~ (Ones complement (not))
<< (left shift)
>> (right shift)
& (bitwise and )
| (bitwise or)

Bitwise operator are used to testing, setting (or) shifting actual bits in a byte (or)
word, which corresponds to char and integer type data and variants.
You cannot use bitwise operators on float, double, long double, void etc.
Bitwise AND, OR, NOT are governed by the same truth table as their logical
equivalents, except that they work bit by bit
XOR

P Q P^Q

0 0 0

1 0 1

0 1 1

1 1 1

the bit-shift operators <<, >> move all bits in a variable to the right (or) left
as specified.
Syntax : (1)shift-right
variable >> number of bit positions;
(2)shift-left
variable << number of bit positions;

Shift right effectively divides a number by 2


Shift left effectively multiply a number by 2
x=7;
x=x<<1;
binary value for 7: 0000 0111
after x<<1 the value in x is : 0000 1110 which is equivalent to 14
x=7;
x=x>>1;
after x>>1 the value of x is : 0000 0011 which is equivalent to 3
ones complement(~)
It reverses state of each bit in its operand. That is, all 1s are set to 0, all
0s are set to one
Is often used in cipher routines.
X=7; 0000 0111
X=~ x;
After ones complement 1111 1000

The & and * pointer operator


The first pointer operator is & returns the memory address of its operand,
& - is unary operator
The second pointer operator is *,which is the complement of &.
* is also unary operator that returns the value of the variables located at the
address that follows it
sizeof compile-Time operator : return the size of the variable or data-type
you have passed as argument
E.g.:
double f;
Prinf(%d,sizeof(f)); //Will print 8
Printf(%d,sizeof(int));//print 4

Control Statements
enable us to specify the flow of program control
Two types of control statements
1. branching/ selection statement.
2. Looping statements
1. BRANCHING STATEMENTS
1. If statement
Simple if
Cascaded if (or) if else ladder
Nested if
2. switch statement
int a,b,c;
char opt,action[20];
opt=getchar();
scanf(%d %d,&a,&b);
switch(opt)
{
case + : c=a+b;
strcpy(action, sum );
break;
case - : c=a-b;
strcpy(action, difference );
break;
case * : c=a-b;
strcpy(action, product );
break;
default: strcpy(action, invalid action);
}
printf( the %s of %d and %d is %d,action,a,b,c);
3. Conditional operator (? :)
LOOP STATEMENTS
for, while,do

Unconditional Branch Statements/ Unconditional Control Statement


1)return 2)goto 3)break 4)continue

Break

for(i=1;i<5;i++)
{
for(j=1;j<5;i++)
{
if(j>i)break;
printf(\t%d,j);
}
printf(\n);
}

Continue:
for(i=1;i<5;i++)
{
for(j=1;j<5;i++)
{
if(i == j)continue;
printf(\t%d,j);
}
printf(\n);
}
exit() function : immediate termination of the entire program
void exit(int return_code);
1. Write a C Program to add two numbers without using addition operator
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers: %d",sum);
return 0;
}
Twist in bitwise complement operator in C Programming

The bitwise complement of 35 (~35) is -36 instead of 220, but why?

For any integer n, bitwise complement of n will be -(n+1). To understand this, you
should have the knowledge of 2's complement

Write a c program to print Floyd Triangle(right-angled triangular array of natural


numbers)
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print :");
scanf("%d", &n);
for (i = 1; i <= n; i++){
for (c = 1; c <= i; c++){
printf("%d ",a);
a++;
}
printf("\n");
}
return 0;}
OUTPUT
Enter the number of rows of Floyd's triangle to print : 6
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21

Write a C program to find maximum of two numbers using Conditional


Operator
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,max;
clrscr();
printf(Enter two numbers:);
scanf(%d%d,&num1,&num2);
max=(num1>num2) num1:num2;
printf(Maximum between %d and %d is %d,num1,num2,max);
getch();
}

You might also like