If Else
If Else
The ability to control the flow of your program, letting it make decisions on what
code to execute, is valuable to the programmer. The if statement allows you to control if
a program enters a section of code or not based on whether a given condition is true or
false. One of the important functions of the if statement is that it allows the program to
select an action based upon the user's input. For example, by using an if statement to
check a user-entered password, your program can decide whether a user is allowed
access to the program.
statement statement_1
statement_n
The next program prints "Less than 10" if the input value of x is less than 10. If
the value of x is greater or equal to 10, nothing will be printed.
#include <stdio.h>
int x;
int main(void)
{
scanf("%d", &x);
if (x < 10) printf("Less than 10\n");
return 0;
}
Real world expression C notation
if x 4 , then . . . if (x > 4) . . .
if x 4 , then . . . if (x >= 4) . . .
if x 6 , then . . . if (x < 6) . . .
if x 6 , then . . . if (x <= 6) . . .
if x 7 , then . . . if (x == 7) . . .
if x 9 , then . . . if (x != 9) . . .
if (condition)
statement_1;
else
statement_2;
If the condition evaluates to true, then the if block will be executed, otherwise,
the else block will be executed.
true false
condition
statement_1 statement_2
#include <stdio.h>
int main(void)
{
int a = 10, b = 4;
printf("a = %d, b = %d\n",a,b);
// not equal to
if (a != b)
printf("a is not equal to b\n");
else
printf("a is equal b\n");
return 0;
}
int x, y;
int main(void)
{
scanf("%d", &x);
if (x < 0) y = x + 4; else y = x * x;
printf("%d\n",y);
return 0;
}
E-OLYMP 8611. Water and Ice The temperature of the air is t degrees. Print
“Water” if t is positive and “Ice” otherwise.
► If t > 0 print “Water”, otherwise print “Ice”.
A conditional branch is a statement that causes the program to change the path of
execution based on the value of an expression. Consider the following program:
int main(void)
{ A
// do A
if (condition) true false
// do B condition
else
// do C C
B
// do D
return 0;
} D
This program has two possible paths. If condition evaluates to true, the
program will execute A, B, and D. If condition evaluates to false, the program will
execute A, C, and D. As you can see, this program is no longer a straight-line program –
its path of execution depends on the value of expression.
Logical Operators are used to combine two or more conditions/constraints or to
complement the evaluation of the original condition in consideration.
operator C notation
x and y x && y
x or y x || y
not x !x
x xor y x^y
Logical AND: The ‘&&’ operator returns true when both the conditions in
consideration are satisfied. Otherwise it returns false. For example, a && b returns
true when both a and b are true (i.e. non-zero).
Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in
consideration is satisfied. Otherwise it returns false. For example, a || b returns true
if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b
are true.
Logical NOT: The ‘!’ operator returns true the condition in consideration is not
satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e.
when a = 0.
a && b a || b a
a b b
#include <stdio.h>
int main(void)
{
if (56) printf("true\n"); // 56 is true
if (6 - 6) printf("true\n"); // 0 is false
else printf("false\n");
return 0;
}
#include <stdio.h>
int main(void)
{
int x = (3 < 6) + (3 == 6) + (3 > 6);
// x = 1 + 0 + 0 = 1
int y = (5 != 6) + (5 <= 6) + true + false + (false < true);
// y = 1 + 1 + 1 + 0 + 1 = 4
printf("%d %d\n", x, y); // 1 4
return 0;
}
int main(void)
{
int x = 5, y;
y = (x + 4) * (x < 0) + x * x * (x >= 0);
printf("%d\n", y);
return 0;
}
E-OLYMP 8873. One-digit number Integer n is given. Print Ok, if n is one-digit
number and No otherwise.
► n is one-digit number if -9 ≤ n ≤ 9. Implement this condition.
Check if there exists a non degenerate triangle with sides a, b, c (the sum of any
two sides must be more than the third side):
if (a < b + c && b < a + c && c < a + b)
E-OLYMP 6278. City numbers Determine if the houses with numbers n and m
are located on one side of the street.
► The answer is affirmative if n and m have the same parity: either both even or
both odd. The conditional statement looks like:
if ((n is even and m is even) || (n is odd and m is odd))
Second solution is based on the fact that two numbers have the same parity if their
sum is even.
if ((n + m) % 2 == 0)
E-OLYMP 8864. Numbers of the same sign Determine if numbers n and m have
the same sign.
► Numbers n and m have the same sign if either they both positive or both
negative. This condition can be simplified: the answer is affirmative if the product of n
and m is positive.
Let we have if statement with multiple conditions united with and (&&) operation:
if (cond1 && cond2 && cond3 && . . . && condn)
Overall condition is true, if all condi are true. Conditions are evaluated from left to
right. If some condition condi is false, the rest conditions condi+1, …, condn are not
evaluated.
#include <stdio.h>
int a = 5;
int main(void)
{
if (a++ > 0 && a++ > 10 && a++ > 0)
printf("YES\n");
else
printf("NO\n");
printf("%d\n", a); // a = 7
return 0;
}
The same rule holds if multiple condition are united with or ( || ) operation
if (cond1 || cond2 || cond3 || . . . || condn)
Overall condition is true, if at least one condi is true. Conditions are evaluated
from left to right. If some condition condi is true, the rest conditions condi+1, …, condn
are not evaluated.
#include <stdio.h>
int a = 5;
int main(void)
{
if (a++ > 10 || a++ > 10 || a++ > 0 || a++ > 0)
printf("YES\n");
else
printf("NO\n");
printf("%d\n", a); // a = 8
return 0;
}
int main(void)
{
int x;
scanf("%d",&x);
if (x < 10)
{
printf("You entered %d\n",x);
printf("%d is less than 10\n",x);
}
else
{
printf("You entered %d\n",x);
printf("%d is not less than 10\n",x);
}
return 0;
}
Chaining if statements
It is possible to chain if-else statements together:
x 1, x 0
y ( x) x 2 ,0 x 10
x 4, x 10
#include <stdio.h>
double x, y;
int main(void)
{
scanf("%lf", &x);
if (x < 0) y = x + 1; else
if (x < 10) y = x * x; else y = x - 4;
printf("%lf\n", y);
return 0;
}
E-OLYMP 8526. Conditional statement - 3 Find the value of y according to
condition:
x 5, x 4
y x 2 3 x, 4 x 7
3
x 2 x, x 7
► Use chained if-else statements.
Nesting if statements
It is also possible to nest if statements within other if statements.
Three numbers are given. Find and print the maximum among them.
#include <stdio.h>
int a, b, c, max;
int main(void)
{
scanf("%d %d %d",&a,&b,&c);
if (a > b)
if (c > a) max = c; else max = a;
else
if (c > b) max = c; else max = b;
printf("%d\n",max);
return 0;
}
Divisibility
Number x is divisible by 2 if the remainder after dividing x by 2 is 0:
#include <stdio.h>
int x;
int main(void)
{
scanf("%d", &x);
if (x % 2 == 0)
printf("%d is even\n",x);
else
printf("%d is odd\n",x);
return 0;
}
int a, b, c, d, res;
int main(void)
{
scanf("%d %d %d %d",&a,&b,&c,&d);
res = a;
if (b > res) res = b;
if (c > res) res = c;
if (d > res) res = d;
printf("%d\n",res);
return 0;
}
int x, y, res;
int main(void)
{
x = 16; y = 3;
res = x / y;
if (x % y > 0) res++;
printf("%d\n",res); // ceil(x/y)
return 0;
}
Conditional ?: operator
The ternary operator (?:) is a very useful conditional expression used in C. It's
effects are similar to the if statement but with some major advantages.
int x, y;
int main(void)
{
scanf("%d", &x);
y = (x < 0) ? x + 4 : x * x;
printf("%d\n",y);
return 0;
}
int main(void)
{
scanf("%d %d",&a,&b);
min = (a < b) ? a : b;
max = (a > b) ? a : b;
printf("%d %d\n",min,max);
return 0;
}
QUIZ
int main(void)
{
int a = 5;
if (a > 10) printf("YES ");
else printf("NO ");
printf("%d\n", a);
return 0;
}
int main(void)
{
int a = 5, b = 10;
if (a++ + ++b >= 16) printf("%d\n", a + b);
else printf("%d\n", a * b);
return 0;
}