An if statement lets you conditionally process a statement.for i.e if a condition is true(non-zero).then it will executed and if a condition is false(zero) then it will not executed.for example
Let say
int i=16
if(i==16)
{
printf(“hie”);
}
here the condition is true because i is equals to 16.then the body following the if statement will be executed.
NOTE:- if can be work without of relational operators. we can put also put an variable to fulfil if condition.
Let say
int i=16
if(i)
{
printf(“hie”);
}
This code also works, Now What are you thinking,? yes man...here if condition is works because the variable i have the value that is 16 and 16 is treated as a non-zero value. and every non-zero value treated as true.so this code will work.
IF has three basic forms:
/*simple if*/
if(expression)
{
Statement
}
/*if with else*/
if (expression)
{
statement1
}
else
{
statement2
.}
/*if-else ladder*/
if(expression)
{
statement1
}
else if(expression)
{
statement2
}
else
{
statement3
}
the if-then-else.is a generalize form of if.for i.e in certain cases we have two plans let say if bike is not ready then I will prefer to go with in a bus. in this example one thing is cleared that what about if a..if statement condition fails..don’t get worried there is a else condition.if a if condition fails then the program. continues with else condition.
The braces are not required if the body contains
only a single statement. However, they are a
good idea and are required by the 104 C Coding
Standards.
Example of if-else
if (number >= 0)
printf("Number is positive\n");
else
printf("Number is negative\n");
The following example displays Number is positive if the value of number is greater than or equal to 0. If the value of number is less than 0, it displays Number is negative.
The Conditional Operator
The conditional operator is short-hand form of if-else
The conditional operator have the following form:
expr1? expr2: expr3
If expr1 is true then expr2 is executed, else expr3 is evaluated,
i.e.:
x = ((y < z)? y=5: z=10);
in the above example if y is less then z. then an assignment of 5 is take place in y.
otherwise an assignment of 10 is take place in z.
we can also write this as in an improved form.
(y < z) ? printf (“%d is smaller”,y): printf(“%d is smaller”,y);
= (assignment operator) versus == (equality test operator)
int a = 2 ;
if ( a = 1 ) /* semantic (logic) error! */
{
printf (“a is one\n”) ;
}
else if ( a = = 2 )
{
printf (“a istwo\n”) ;
}
else
{
printf (“a is %d\n”, a) ;
}
The statement if (a = 1) is syntactically correct,
so no error message will be produced. (Some
Compilers will produce a warning.) However, a
Semantic (logic) error will occur.
If else with logical operators
we have already learned about how to use relational operators and what are their working in the chapter second of this tutorial. Now here we can see how we can implement the logical operators with if-else statement. consider the following example for && logical operator.
if ( age < 1 && gender == ‘m’)
{
printf (“Infant boy\n”) ;
}
consider the following example for && logical operator.
if (grade == ‘D’ || grade == ‘F’)
{
printf (“See with your Juniors !\n”) ;
}
consider the following example for && logical operator.
printf("Enter the value of a and b:");
scanf ("%d %d", &a, &b);
result=power(a,b);
printf("%d raised to %d is %d", a, b, result);
}
power (int a, int b)
{
int calculation=1, calc;
for (calc=1; calc <=b; calc++) { calculation=calculation*a; continue; } return(calculation); }
(c) Write a general-purpose function to convert any given year into its roman equivalent.
The following table shows the roman equivalents of decimal numbers:
{
printf("\nThe entered year is a leap year.");
}
else
{
printf("\nThe entered year is not a leap year.");
}
}
leap_year(int y)
{
int lp;
if (y%4==0)
{
lp=1;
}
else
lp=0;
return(lp);
}
(e) A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number. For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.
printf("Enter the number whose prime factors are to be calculated:");
scanf ("%d", &number);
primefactor(number);
}
//The following function detects a Prime number.
prime(int num)
{
int i, ifprime;
for (i=2; i<=num-1; i++)
{
if (num%i==0)
{
ifprime=0;
}
else
ifprime=1;
}
return
(ifprime);
}
//The following function prints the prime factors of a number.
primefactor(int num)
{
int factor,ifprime;
for (factor=2; factor<=num;
)
{
prime(factor);
//so that the factors are only prime and nothing else.
if (ifprime)
{
if(num%factor==0) //diving by all the prime numbers less than the number itself.
{
printf("%d ", factor);
num=num/factor;
continue;
}
else
{
factor++;//this cannot be made a part of the for loop
}
}
}
return 0;
}
In c programming, an operator is a character that represents an action,
as for example * is an arithmetic operator that represents multiplication. there are various types of operators in c language.they are as follows:
C Arithmetic Operators
C Relational Operators
C Logical Operators
C Bitwise Operators
C Assignment Operators
[1]C Arithmetic Operators
Arithmetic operators perform the same basic operations you would expect if you used them in mathematics (with the exception of the percentage sign). They take two operands and return the result of the mathematical calculation.
C has five arithmetic operators:
Name Operator Example
Addition + num1 + num2
Subtraction - initial - spent
Multiplication * fathoms * 6
Division / sum / count
Modulus % m % n
pre increment ++ ++a
post increment ++ a++
pre decrement -- --a
post decrement -- a--
Note:--In c / (division operator) always gives quotient and %(modulus operator) always give the remainder of equation.
Increment Operator
• If we want to add one to a variable, we can
say:
count = count + 1 ;
• Programs often contain statements that
Increment variables, so to save on typing, C
provides these shortcuts:
count++ ; OR ++count ;
Both do the same thing. They change the
value of count by adding one to it but they
do this in slightly different manner.
Preincrement Operator
• If the ++ is before the variable(i.e ++a), then the
incrementing is done first (a preincrement).
int amount, count ;
count = 3 ;
amount = 2 * ++count ;
• 1 gets added to count first, then amount gets the
value of 2 * 4, which is 8.
• So, after executing the last line, amount is 8 and
count is 4.
Postincrement Operator
• The position of the ++ determines when the value
is incremented. If the ++ is after the variable, then
the incrementing is done last (a postincrement).
int amount, count ;
count = 3 ;
amount = 2 * count++ ;
• amount gets the value of 2 * 3, which is 6, and
then 1 gets added to count.
• So, after executing the last line, amount is 6 and
count is 4.
Decrement Operator
• If we want to subtract one from a variable, we
can say:
count = count - 1 ;
• Programs often contain statements that
decrement variables, so to save on typing, C
provides these shortcuts:
count-- ; OR --count ;
Both do the same thing. They change the
value of count by subtracting one from it.
Predecrement Operator
• If the -- is before the variable, then the
decrementing is done first (a predecrement).
int amount, count ;
count = 3 ;
amount = 2 * --count ;
• 1 gets subtracted from count first, then amount
gets the value of 2 * 2, which is 4.
• So, after executing the last line, amount is 4 and
count is 2.
Postdecrement Operator
• The position of the -- determines when the value is
Decremented. If the -- is after the variable, then
the decrementing is done last (a postdecrement).
int amount, count ;
count = 3 ;
amount = 2 * count-- ;
• amount gets the value of 2 * 3, which is 6, and
then 1 gets subtracted from count.
• So, after executing the last line, amount is 6 and
count is 2.
Practice
Given
int a = 1, b = 2, c = 3,D;
What is the value of this expression?
D= ++a * b - c--
What is the value of D?
Practice With Evaluating Expressions
Given integer variables a, b, c, d, and e,
where a = 1, b = 2, c = 3, d = 4,
evaluate the following expressions:
a + b - c + d
a * b / c
1 + a * b % c
a+ d % b - c
e= b = d + c / b - a
[2]Relational Operator
A relational operator compares two operands to determine whether one is greater than, greater than or equal to, less than, less than or equal to the other.there are following conditional operators present in c:
> Greater than
>= greater than or equal to
< Less than
<= less than or equal to
When used in an expression they all return a Boolean value(true(non-zero) or false(zero)) which states the result of the comparison (i.e., 4 > 3 can be read as is 4 greater than 3? Which returns true).
Practice with Relational Expressions
Given
int a = 1, b = 2, c = 3 ;
Expression Result
a < c ??
a + b >= c ??
b <= c ??
a + b = = c ??
c <= a ??
a != b ??
a > b ??
a + b != c ??
b >= c ??
[3] Logical Operators
This is what logical operators can do for you. A logical operator combines one or two conditions into a single new condition. C provides three logical operators:
&& and.
|| or.
! not.
Let's look at an example. If you need to know if the value of the variable x is between zero and one, inclusive, you can write this with the help of logical operators for i.e:
if ((x >= 0.0) && (x <= 1.0))what about or logical operator? the or logical operator is used where we have two conditionsand we only want one to fulfil.if you need to know the value of variable is vowel or not,you can write this with the help of or logical operatorif((x=='a')||(x=='e')||(x=='i')||(x=='o')||(x=='u'))
The Operator ! is the C operator to perform the Boolean
operation NOT, it has only one operand, located at its right, and the
only thing that it does is to inverse the value of it, producing false
if its operand is true and true if its operand is false. Basically, it
returns the opposite Boolean value of evaluating its operand. For
example:!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true.
Practice With Logical Operators
Now you try. Translate the following English questions into C conditions.
1)Is the temperature temp greater than 32.0 and less than 212.0?
2)Is the height not equal to zero?
3)Is the absolute value of pos greater than 5.0?
[4] Bitwise Operators in C
Generally, as a programmer you don't need to concern yourself about operations at the bit level. You're free to think in bytes, or ints and doubles, or even higher level data types composed of a combination of these. But there are times when you'd like to be able to go to the level of an individual bit. Exclusive-or encryption is one example when you need bitwise operations.
& Bitwise And
| Bitwise OR
~ Bitwise Complement
^ Bitwise X-OR(exclusive-or)
Bitwise And
The bitwise AND operator is a single ampersand: &. A handy mnemonic is that the small version of the boolean AND, &&, works on smaller pieces (bits instead of bytes, chars, integers, etc). In essence, a binary AND simply takes the logical AND of the bits in each position of a number in binary form.
For instance, working with a byte (the char type):
01001000 &
10111000 =
--------
00001000
--------
The most significant bit of the first number is 0, so we know the most significant bit of the result must be 0; in the second most significant bit, the bit of second number is zero, so we have the same result. The only time where both bits are 1, which is the only time the result will be 1, is the fifth bit from the left. Consequently,
72 & 184 = 8
Bitwise OR
Bitwise OR works almost exactly the same way as bitwise AND. The only difference is that only one of the two bits needs to be a 1 for that position's bit in the result to be 1. (If both bits are a 1, the result will also have a 1 in that position.) The symbol is a pipe: |. Again, this is similar to boolean logical operator, which is ||.
01001000 | 10111000 = --------11111000--------
and consequently
72 | 184 = 248
Bitwise Complement
The bitwise complement operator, the tilde, ~, flips every bit. A useful way to remember this is that the tilde is sometimes called a twiddle, and the bitwise complement twiddles every bit: if you have a 1, it's a 0, and if you have a 0, it's a 1.
unsigned int max = ~0;
of course, is all 0s: 00000000 00000000. Once we twiddle 0, we get all 1s: 11111111 11111111.
Bitwise Exclusive OR(X-OR)
There is no boolean operator counterpart to bitwise exclusive-or, but there is a simple explanation. The exclusive-or operation takes two inputs and returns a 1 if either one or the other of the inputs is a 1, but not if both are. That is, if both inputs are 1 or both inputs are 0, it returns 0. Bitwise exclusive-or, with the operator of a carrot, ^, performs the exclusive-or operation on each pair of bits. Exclusive-or is commonly abbreviated XOR.
For instance, if you have two numbers represented in binary as 10101010 and 01110010 then taking the bitwise XOR results in 11011000. It's easier to see this if the bits are lined up correctly:
01110010 ^
10101010
------------
11011000
----------
You can think of XOR in the following way: you have some bit, either 1 or 0, that we'll call A. When you take A XOR 0, then you always get A back: if A is 1, you get 1, and if A is 0, you get 0. On the other hand, when you take A XOR 1, you flip A. If A is 0, you get 1; if A is 1, you get 0.
[5] Assignment Operators in C
An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value. After the assignment, an assignment expression has the value of the left operand but is not an l-value.In C any value that is having an address is called an Lvalue.there are following versions of assignment operator avaliable in c.In assignment, the type of the right-hand value is converted to the type of the left-hand value, and the value is stored in the left operand after the assignment has taken place.
=
Simple assignment
*=
Multiplication assignment
/=
Division assignment
%=
Remainder assignment
+=
Addition assignment
–=
Subtraction assignment
<<=
Left-shift assignment
>>=
Right-shift assignment
&=
Bitwise-AND assignment
^=
Bitwise-exclusive-OR assignment
|=
Bitwise-inclusive-OR assignment
Practice with Assignment Operators
Given
int i = 1, j = 2, k = 3, m = 4,Z ;
Expression Value
Z+= j + k
Z*= k = m + 5
Z-= m /= j * 2
find out the value of Z?
Operator Precedence and Associativity
Operator precedencetell us about which operation is performed first,for i.e
for i.e if we have a equation 5 + 3 * 2, in this type of equation compiler comes in a confusion that what to perform first multiplication or addition,because in both cases result is different.in this kind of equations operator precedence is used.in operator precedence the priority for each type of operator is defined.multiplication have higher priority then addition.so here first multiplication is performed then addition.
Sometimes there are cases when an operator comes more than one time in an equation.for i.e 8 - 3 - 2here both the operators have same priority then which operation is performed first 8-3 or 3-2..for this c have another feature of operator associative.operator associativity for subtraction is Left to Right so.first 8-3 will be evaluated.
1. Write a program in C to check whether given number is even or odd
number?
2. Write a program in C to check whether given number is
postive,negative or zero?
3. Write a program in C to check whether given year is leap or not?
4. Write a program in C to check whether upper, lower, number or
not?
5. Write a program in C to convert upper to lower case letters?
6. Write a program in C to convert lower to upper letters ?
7. Write a program in C to print a word form of given number between
0 to 9?
8. Write a program in C to print a word form of given number between
0 to 99?
9. Write a program in C to perform relational operations of two integer
numbers?
10. Write a program in C to perform relational operations of two float
numbers?
11. Write a program in C to print the grade of given mark ?
12. Write a program in C to find out biggest of two numbers?
/* program no : 1 */
/* purpose : check whether given number is even or odd number */
#include
int main()
{
int number;
scanf("%d", &number);
if ( number % 2 ==1)
printf("odd");
else
printf("even");
}
/* program no : 2 */
/* purpose : check whether given number is postive , negative or zero */
#include
int main()
{
int number;
scanf("%d", &number);
if ( number >0)
printf("postive");
else
{
if (number<0)
printf("negative");
else
printf("zero");
}
}
/* program no : 3 */
/* purpose : check whether given year is leap or not*/
#include
int main()
{
int year;
scanf("%d", &year);
if ( year % 4 ==0)
printf("leap year");
else
printf("not a leap");
}
/* program no : 4 */
/* purpose : check whether upper, lower, number or not */
#include
int main()
{
char any;
scanf("%c", &any);
if(any>='A' && any <='Z')
printf("upper case");
else
if(any>='a' && any <='z')
printf("lower case");
else
if(any>='0' && any <='9') printf("number");
else
printf("not ");
}
/* program no : 5 */
/* purpose : convert upper to lower case letters */
#include
int main()
{
char any;
scanf("%c", &any);
if(any>='A' && any <='Z')
printf("%c",any+32);
else
printf("%c",any);
}
/* program no : 6 */
/* purpose : convert lower to upper letters */
#include
int main()
{
char any;
scanf("%c", &any);
if(any>='a' && any <='z')
printf("%c",any-32);
else
printf("%c",any);
}
/* program no : 7 */
/* purpose : print a word form of given number between 0 to 9 */
#include
int main()
{
int number;
scanf("%d", &number);
if ( number==0)
printf("Zero");
else
if ( number==1)
printf("One");
else
if ( number==2)
printf("Two");
else
if ( number==3)
printf("Three");
else
if ( number==4)
printf("Four");
else
if ( number==5)
printf("Five");
else
if ( number==6)
printf("Six");
else
if ( number==7)
printf("Seven");
else
if ( number==8)
printf("eight");
else
if( number==9)
printf("Nine");
else
printf("others");
}
/* program no : 8 */
/* purpose : print a word form of given number between 0 to 99 */
#include
int main()
{
int number;
scanf("%d", &number);
if ( number==10)
printf("Ten");
else
if ( number==20)
printf("Twenty");
else
if ( number==30)
printf("Thirty");
else
if ( number==40)
printf("Fourty");
else
if ( number==50)
printf("Fifty");
else
if ( number==60)
printf("Sixty");
else
if ( number==70)
printf("Seventy");
else
if ( number==80)
printf("eighty");
else
if( number==90)
printf("Ninty");
else
printf("others");
}
/* program no : 9 */
/* purpose : relational operations of two integer numbers */
#include
int main()
{
int one, two;
scanf("%d%d",&one ,&two);
if ( one==two)
printf("first is equal to second");
else
{
if ( one > two)
printf("first is greater than second");
else
printf("first is less than second");
}
}
/* program no : 10*/
/* purpose : relational operations of two float numbers */
#include
int main()
{
float one, two;
scanf("%f%f",&one ,&two);
if ( one==two)
printf("first is equal to second");
else
{
if ( one > two)
printf("first is greater than second");
else
printf("first is less than second");
}
}
/* program no : 11 */
/* purpose : print the grade of given mark */
#include
int main()
{
int mark;