Академический Документы
Профессиональный Документы
Культура Документы
Program:
#include <stdio.h>
int main()
{
int a = 300, b = 10, c = 20;
if(!(a >= 400))
b = 300;
c = 200;
printf("b = %d c = %d", b, c);
return 0;
}
Output:
b = 300 c = 200
Explanation:
if(1)
b = 300;
Program:
#include <stdio.h>
int main()
{
int a = 500, b = 100, c;
if(!a >= 400)
b = 300;
c = 200;
printf("b = %d c = %d", b, c);
return 0;
}
Output:
b = 100 c = 200
Explanation:
Program:
#include <stdio.h>
int main()
{
int x = 10, y = 100 % 90;
if(x != y);
printf("x = %d y = %d", x, y);
return 0;
}
Output:
x = 10 y = 10
Explanation:
Since the variables x and y both have the same value the condition
( x != y ) fails. Therefore the control jumps to the printf( ) and
prints out values of x and y. Note that there is a ; after the if.
This is called a null statement. Due to the ; the condition becomes:
if(x!=y)
;
printf("x = %d y = %d", x, y);
#include <stdio.h>
int main()
{
int x = 10, y = -20;
x = !x;
y = !y;
printf("x = %d y = %d", x, y);
return 0;
}
Output:
x = 0 y = 0
Explanation:
Program:
#include <stdio.h>
int main()
{
int x = 0, y = 1;
y = !x;
x = !y;
printf("x = %d y = %d", x, y);
return 0;
}
Output:
x = 0 y = 1
Explanation:
#include <stdio.h>
int main()
{
if(!3.14)
printf("REC");
else
printf("RIT");
return 0;
}
Output:
RIT
Explanation:
Program:
#include <stdio.h>
int main()
{
int x = 3, y = 4, z = 4;
printf("ans = %d", z >= y && y >= x ? 1 : 0);
return 0;
}
Output:
ans = 1
Explanation:
yields truth.
Program:
#include <stdio.h>
int main()
{
int x = 3, y = 4, z = 4;
printf("ans = %d", (z >= y >= x ? 100 : 200));
return 0;
}
Output:
ans = 200
Explanation:
#include <stdio.h>
int main()
{
float a = 12.25, b = 13.65;
if(a = b)
printf("a and b are equal");
else
printf("a and b are not equal");
return 0;
}
Output:
Explanation
To begin with a and b are not equal. The catch here is the
assignment operator used in the if statement. It simply assigns the
value of b to a, and hence the condition becomes,
if (13.65)
Program:
#include <stdio.h>
int main()
{
if('Z' < 'z')
printf("REC");
else
printf("RIT");
return 0;
}
Output:
REC
Program:
#include <stdio.h>
int main()
{
int i = 10, j = 40;
if((j - i) % 10)
printf("REC");
else
printf("RIT");
return 0;
}
Output:
RIT
Explanation:
if(0)
Program:
#include <stdio.h>
int main()
{
int i = -4, j, num = 10;
j = i % -3;
j = (j ? 0 : num * num);
printf("j = %d", j);
return 0;
}
j = 0
Explanation:
j = -4 %-3
Program:
#include <stdio.h>
int main()
{
float a = 0.7;
if(a < 0.7)
printf("REC");
else
printf("RIT");
return 0;
}
Output:
REC
Explanation:
The output is very surprising! 0.7 is never less than 0.7, so the
condition should evaluate to false. But that doesn't happen. Reason
is, when 0.7 stored in a, due to precision considerations, it is
stored as something less than 0.7. Naturally, when value stored in a
is compared with 0.7, the condition evaluates to true and 'REC' gets
printed.
long double a;
Moral of the story is: exercise utmost care while comparing floating
point values in an if statement.
Program:
#include <stdio.h>
int main()
{
int a = 10, b = 10;
printf("ans = %d", a > b ? a * a : b / b);
return 0;
}
Output:
ans = 1
Explanation
Since the condition a > b fails, the statement after the :, i.e. b /
b is evaluated and its result is printed by printf().
#include <stdio.h>
int main()
{
int x = 10, y = 20;
x = !x;
y = !x && !y;
printf("x = %d y = %d", x, y);
return 0;
}
Output:
x = 0 y = 0
Explanation:
y = !x && !y
y = !0 && !20
y = 1 && 0
Program:
#include <stdio.h>
int main()
{
int x = 10, y = 20;
if(!(!x) && x)
printf("x = %d", x);
else
printf("y = %d", y);
return 0;
}
Output:
x = 10
if (!(0) && x)
!(0) yields a 1. Note that the value of x is still 10, hence the
condition becomes,
Since both conditions yield truth and they have been combined using
&&, the whole thing is treated as true, and hence the first printf()
is executed, printing the value of x in the process.
Program:
#include <stdio.h>
int main()
{
float a = 0.5, b = 0.9;
if(a && b > 0.9)
printf("REC");
else
printf("RIT");
return 0;
}
Output:
RIT
Explanation:
Program:
#include <stdio.h>
int main()
{
int x = 100;
if(!!x)
printf("x = %d", !x);
else
printf("x = %d", x);
return 0;
}
Output:
x = 0
Explanation:
x = !x;
#include <stdio.h>
int main()
{
char s = 3;
switch(s)
{
case '1':
printf("REC\n");
case '2':
printf("RIT\n");
default:
printf("RSA\n");
}
printf("RSB");
return 0;
}
Output:
RSA
RSB
Explanation:
#include <stdio.h>
int main()
{
int k = -2, j = 4;
switch(k /= j / k)
{
default:
printf("REC\n");
case 0:
printf("RIT\n");
case 1:
printf("RSA\n");
case 2:
printf("RSB");
}
return 0;
}
Output:
RSA
RSB
Explanation:
k /= (4 / -2)
k /= -2
which evaluates to 1.
Note that in the switch the very first case is the default case.
This is perfectly acceptable.
(c) The group of statements after the if, upto and not including the
else is known as the "if block". Similarly, the statements after the
else form the "else block".
(d) If there is only one statement in the if block then the pair of
braces can be dropped.
(e) If there is only one statement in the else block then the pair
of braces can be dropped.
(h) More than one condition can be combined together using logical
operators && ( AND ), || ( OR ),! ( NOT ). For example,
Here there are four conditions being checked. They have been split
into two groups by the || operator. "Whisky is Risky" would be
printed if any one group evaluates to true. In each group there are
two conditions separated by && operator. For each group to evaluate
to true, both the conditions in the group must be satisfied.
if (!flag)
if (flag ==0)
#include <stdio.h>
int main()
{
char j = 1;
while(j <= 255)
{
printf("%d\t", j);
j = j + 1;
}
return 0;
}
Output:
Explanation:
Program:
#include <stdio.h>
int main()
{
int j = 1;
while(j <= 255);
{
printf("%c %d\n", j, j);
j++;
}
return 0;
}
Output:
Program:
#include <stdio.h>
int main()
{
int j = 1;
while(j <= 255)
printf("%d\t", ++j);
return 0;
}
Output:
Explanation:
#include <stdio.h>
int main()
{
int i;
for(i = 1; i++ <= 5; printf("%d\t", i));
return 0;
}
Output:
2 3 4 5 6
Explanation:
Let us isolate the condition in the for loop for closer examination:
i++ <= 5
Program:
#include <stdio.h>
int main()
{
int i = 1, j = 1;
for( ; j; printf("%d %d\n", i, j))
j = i++ <= 5;
return 0;
}
2 1
3 1
4 1
5 1
6 1
7 0
Explanation:
Program:
#include <stdio.h>
int main()
{
int i = 1;
for( ; i++; )
printf("%d\t", i);
return 0;
}
Output:
Since i++ is being used in the for loop, two operations are being
carried out. Firstly i is tested for truth or falsity, and then it
is incremented. To begin with, as i is equal to 1, the test results
into a truth and then i is incremented to 2. This 2 is then printed
through printf(). Next time when the control reaches since i is 2,
the test once again results into truth, i is incremented to 3 and
printed out. This goes on smoothly till i becomes 2147483647. When i
is 2147483647, it evaluates to truth and ++ tries to make it
2147483648, which being an int it cannot become. Therefore the
number from the negative side of the range ( - 2147483648 ) is
assigned to i, which is then printed out. This goes on and then
sometime later i becomes -1. With i as -1 the test results into a
truth and i++ makes the value of i as 0. This value is printed out
and once again the control reaches the test in the for loop. This
time, however, the test fails since value of i is 0. Therefore the
loop is terminated and the execution comes to an end.
Program:
#include <stdio.h>
int main()
{
int a = 5;
do
{
printf("%d\t", a);
a = -1;
} while(a > 0);
return 0;
}
Output:
Explanation:
while
do...while
for