Content of the lesson:
In a real life we often encounter conditions – deciding on the basis on some experience; for example:
In general: When a condition is valid, then ..., otherwise ... Conditions may be of different levels of complexity and quantity – and this complicates the whole system of conditions. But then the whole system can provide solutions even for relatively complex situations.
In C (as in many other programming languages) exists the command IF-THEN (ELSE). The syntax of this command should be:
if (condition)
{
sequence-of-commands
}
If we also want to specify what should happen if the condition is not valid, the command IF-THEN includes the part ELSE which is executed if the condition is not valid.
if (condition
)
{
sequence-of-commands-1
}
else
{
sequence-of-commands-2
}
Take a look at the following simple example:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a;
scanf("%d", &a);
if (a > 5)
{
printf("a je vetsi nez 5")
}
fflush(stdin);
getchar();
return 0;
}
If we want to have more exact information we use the complete IF-ELSE form:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a;
scanf("%d", &a);
if (a > 5)
{
printf("a je vetsi nez 5")
}
else
{
printf("a je mensi nez 5");
}
fflush(stdin);
getchar();
return 0;
}
If we want to be even more precise and evaluate both inequality and equality, we have to use nested if or the construction if-else if-else:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a;
scanf("%d", &a);
if (a > 5)
{
printf("a je vetsi nez 5")
}
else
{
if (a == 5)
{
printf("a je rovno 5");
}
else
{
printf("a je mensi nez 5");
}
}
fflush(stdin);
getchar();
return 0;
}
Note: You can also see the "is equal to" operator which is represented by two characters equal to, not only one as for assigning a value.
You can also shorten this syntax using the construction if – else if – else.
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a;
scanf("%d", &a);
if (a > 5)
{
printf("a je vetsi nez 5")
}
else if (a == 5)
{
printf("a je rovno 5");
}
else
{
printf("a je mensi nez 5");
}
fflush(stdin);
getchar();
return 0;
}
If a more complex condition is needed, it can be put together from more conditions using the operators && (meaning of AND) and || (meaning of OR).
The operator && is a parallel to the conjunction "and". If we put together several conditions using AND, then the resultant condition is valid ONLY if EACH condition is valid. In everyday life we use this operator in the following consideration: "When it's raining AND I am out THEN I open my umbrella." or "When it is raining on me AND I have my umbrella THEN I open it." (if ((it is raining of me) && (I have an umbrella)) {I open it}).
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a, b;
scanf("%d", &a);
scanf("%d", &b);
if (a < 0 && b < 0)
{
printf("Cisla a a b jsou zaporna!")
}
else
{
printf("Cisla a a b nejsou obe zaporna!")
}
fflush(stdin);
getchar();
return 0;
}
The operator || is parallel to the conjunction "or". If we put together several conditions using OR, then the resultant condition is valid when AT LEAST ONE of the conditions is valid (and it is not valid only if none of the conditions is valid). In everyday life we use this operator in the following considerations, for example: "When I am hungry or I like tasty meals, I take something delicious." or "When I want to be a good student or I am bored, I learn something." (if ((I want to be a good student) || (I am bored)) {I learn something}).
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a, b;
scanf("%d", &a);
scanf("%d", &b);
if (a > 0 || b > 0)
{
printf("Alespon jedno z cisel a a b je vetsi nez 0!")
}
else
{
printf("Cisla a a b jsou obe mensi nebo rovna 0!")
}
fflush(stdin);
getchar();
return 0;
}
In previous lessons we solved the calculation of simple formulas. Some of them already looked quite complicated. Try to write a program for calculating a simple quotient of two numbers a and b.
Sample: Calculation of the quotient of two arbitrarily given numbers a and b.
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a, b;
printf("Zadejte cislo a: ");
scanf("%d", &a);
printf("Zadejte cislo b: ");
scanf("%d", &b);
if (b == 0)
{
printf("Deleni nulou je nedefinovaná operace, proto nelze podil zadaných cisel spocitat.");
}
else
{
printf("Podil zadanych cisel je %d",a/b);
}
fflush(stdin);
getchar();
return 0;
}
As we know from Math, division by zero is not an allowed operation. If we forget this fact, the program crashes when it tries to divide by zero (which is an unacceptable status). We can see that without the condition IF-THEN it is not possible to handle a simple quotient of any two numbers correctly.
We can complicate the task slightly by the requirement that the previous program should store the quotient of a / b in a variable c. The following solution is the first thing that comes to our minds:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a, b, c;
printf("Zadejte cislo a: ");
scanf("%d", &a);
printf("Zadejte cislo b: ");
scanf("%d", &b);
if (b == 0)
{
printf("Deleni nulou je nedefinovaná operace, proto nelze podil zadaných cisel spocitat.");
}
else
{
c = a / b;
printf("Podil zadanych cisel je %d",c);
}
fflush(stdin);
getchar();
return 0;
}
You can face one problem. In case you divided two numbers (for example 5 and 2) you get an integer number (in our case 2). However, you might want to get more accurate result.
The problem is in the data types. The result of division of 2 integer numbers in C is again an integer number (even if you save it into float variable). If you want to get a float number and input integer numbers (for example because of memory), you have to retype one of the values (change its data type for this calculation). This can be done by adding the required data type into brackets before the name of the variable.
The final program is illustrated in the following source code:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a, b;
float c;
printf("Zadejte cislo a: ");
scanf("%d", &a);
printf("Zadejte cislo b: ");
scanf("%d", &b);
if (b == 0)
{
printf("Deleni nulou je nedefinovaná operace, proto nelze podil zadaných cisel spocitat.");
}
else
{
c = (float)a / b;
printf("Podil zadanych cisel je %f",c);
}
fflush(stdin);
getchar();
return 0;
}
Write a program to count the formula X = (A+B) / (C/D). The user assigns the variables A, B, C and D.
Write a program to calculate square root from the given value A.