Control(Selection) statements in c and c++
In simple examples in the C and C++ programming language, commands were executed in one flow, ie. one after the other, until the very end of the program. Sometimes in the program some commands are executed under a certain condition.
For example. consider the following problem: For the entered numbers a and b, calculate the value of the fraction a / b.
After the declaration of variables and entries a and b by the user, we come to the part where it is necessary to calculate the fraction. Is it possible to simply introduce the variable c and calculate c = a / b?
It is known that division with zero is not allowed. This means that before writing the formula written above, it must first check whether the condition that b is different from zero is met. If so, then you can write a formula and print the result. Otherwise, something alternative can be done, e.g. to write a message that division with zero is not allowed, or to do nothing.
We conclude that there are two variants of program execution, or only one, which depends on the condition of whether b is different from zero or not. This means that branching occurs in the program and that branching is done on the basis of logical data or expressions.
For example. consider the following problem: For the entered numbers a and b, calculate the value of the fraction a / b.
After the declaration of variables and entries a and b by the user, we come to the part where it is necessary to calculate the fraction. Is it possible to simply introduce the variable c and calculate c = a / b?
It is known that division with zero is not allowed. This means that before writing the formula written above, it must first check whether the condition that b is different from zero is met. If so, then you can write a formula and print the result. Otherwise, something alternative can be done, e.g. to write a message that division with zero is not allowed, or to do nothing.
We conclude that there are two variants of program execution, or only one, which depends on the condition of whether b is different from zero or not. This means that branching occurs in the program and that branching is done on the basis of logical data or expressions.
Logical data
These are data whose value is the result of a logical expression and can have two states:
true
false
For the type of this data, the official word of the C ++ bool language is used
true
false
For the type of this data, the official word of the C ++ bool language is used
Logical data and expressions
Logical relations ie. expressions are expressions that are actually the results of some comparisons, and use the following operators:
<, >, <=, >=, !, !=, ==
"Less than", "greater than", "less than or equal", "greater or equal", "negation", "not equal", "equally"
<, >, <=, >=, !, !=, ==
"Less than", "greater than", "less than or equal", "greater or equal", "negation", "not equal", "equally"
Example of logical expressions in the programming language C.
For a logical type in C, the int data type is used, as if it were an integer with two things being used for the values:
- positive numbers which presented logical truth or true (Value 1 is usually used)
- negative numbers and zero for logical untruth(false)
int a=10, b=8,c;
int b; //logical data
c=a+b; //integer expression
c=a>b; //c=1
b=a>b; //true and value b=1
b=!(a>b); //b=!(1), b=0
int b; //logical data
c=a+b; //integer expression
c=a>b; //c=1
b=a>b; //true and value b=1
b=!(a>b); //b=!(1), b=0
In C ++, you have a special type for logical data (bool). It is a type that has two values: TRUE and FALSE.
Example of logical expressions in C ++
int a=10, b=8,c;
bool b;
c=a+b; //integer expression
c=a>b; //false
b=a>b; //b=true
b=!(a>b); //b=false
bool b;
c=a+b; //integer expression
c=a>b; //false
b=a>b; //b=true
b=!(a>b); //b=false
Logical data and more complex expressions
To connect several smaller logical expressions we use the operators:
"&&" - Logical "and"
"||" - Logical "or"
Suppose we have the following code:
int a,b,c;
a=10,b=4;c=2;
bool d,e;
Then it is:
d = (a==0) && (b>c) /* d=false && true;
d=false; */
e=(a==0) ||(b>c) /* e=false || true;
e=true; */
A more complex logical expression obtained by connecting two simple logical expressions with "&&" will be accurate only if both free expressions are correct:
true && true = true
true && false = false
false && true = false
false && false = false
A more complex logical expression obtained by connecting two simple logical expressions with "||" will be correct if at least one of the simple expressions is exact ie:
true || true = true
true || false = true
false || true = true
false || false = false
"&&" - Logical "and"
"||" - Logical "or"
Suppose we have the following code:
int a,b,c;
a=10,b=4;c=2;
bool d,e;
Then it is:
d = (a==0) && (b>c) /* d=false && true;
d=false; */
e=(a==0) ||(b>c) /* e=false || true;
e=true; */
A more complex logical expression obtained by connecting two simple logical expressions with "&&" will be accurate only if both free expressions are correct:
true && true = true
true && false = false
false && true = false
false && false = false
A more complex logical expression obtained by connecting two simple logical expressions with "||" will be correct if at least one of the simple expressions is exact ie:
true || true = true
true || false = true
false || true = true
false || false = false
Programming and Logical Data.
Logical data is used as a crossover in branching in a program. If we have a logical variable b:
This branching is done using a command if-else
if( b )
{
else
{
{
command1;
}else
{
command2;
}If there is no command in the other branch then this branch is used to skip the command1
This skip is done using the if command
if(b)
{
naredba1; // This is conditional execution of command 1
}
.
.
.
Regardless of whether the command1 will be executed or skipped, the program continues to execute after the closed bracket
if(b)
{
naredba1; // This is conditional execution of command 1
}
.
.
.
Regardless of whether the command1 will be executed or skipped, the program continues to execute after the closed bracket
Control statements in a program based on a logical expression in c and c++
a>x is a logical expression and in this case it is false, so command 1 is not executed
A logical expression can be written directly in a small bracket without introducing a bool variable.
Suppose we have:
int a=3,x=5;
if(a>x)
{
command1;
}
.
.
.
a>x is a logical expression and in this case it is false, so command 1 is not executed
A logical expression can be written directly in a small bracket without introducing a bool variable.
Suppose we have:
int a=3,x=5;
if(a>x)
{
command1;
}
.
.
.
a>x is a logical expression and in this case it is false, so command 1 is not executed
Example 1: Enter two real numbers x and y, and calculate the value of the fraction
The first thing that might have happened to us is to solve the problem in the following way:
double a,b, fraction;
cout << "Enter two real numbers a and b!" << endl;
cin >> a >> b;
fraction=a/b;
cout << fraction;
cout << "Enter two real numbers a and b!" << endl;
cin >> a >> b;
fraction=a/b;
cout << fraction;
However, the problem is that the formula for calculating the fraction must be valid for every real number. In our case, the problem is if the number 0 is found in the fractional name.
To ensure that there is no division if b is equal to zero, we will use the if command
To ensure that there is no division if b is equal to zero, we will use the if command
double a,b, fraction;
cout << "Enter two real numbers a and b!" << endl; cin >> a >> b;
if(b != 0)
{
fraction=a/b;
}
cout << fraction;
cout << "Enter two real numbers a and b!" << endl; cin >> a >> b;
if(b != 0)
{
fraction=a/b;
}
cout << fraction;
Here is an if command without clause else. If we would like to inform the user of the error in case of entering the value of zero for the reason rationale:
double a,b, fraction;
cout << "Enter two real numbers a and b!" << endl;
cin >> a >> b;
if(b != 0)
{
fraction=a/b;
}
else
{
cout << "Error: You can't devide some number with zero"
}
cout << fraction;
cout << "Enter two real numbers a and b!" << endl;
cin >> a >> b;
if(b != 0)
{
fraction=a/b;
}
else
{
cout << "Error: You can't devide some number with zero"
}
cout << fraction;
Example: Enter an integer and determine if it is divisible by 3
Solution:
A number is divisible by 3 if the remainder of the division of the number entered and the number 3 is equal to zero. We will use the if-else command to create a branch in the program, so that in one branch the message (printf) "Number is divisible by 3" is printed, while in the other branch another message will be printed: "Number is not divisible by 3 ". In order to write the branching condition, it is necessary to determine the remainder of the division. The "%" operator is used for this. So, the remainder of dividing the number a and the number 3 is: a% 3. It is then checked to see if that remainder is zero. So, a% 3 == 0 |
Selection statement example:number divisibility testing(with an English title) |
#include < stdio.h >
/*Enter an integer and determine if it is divisible by 3*/
int main()
{
/*Enter an integer and determine if it is divisible by 3*/
int main()
{
int a;
printf("Enter an integer\n");
scanf("%d",&a);
if((a % 3) == 0)
}printf("Enter an integer\n");
scanf("%d",&a);
if((a % 3) == 0)
printf("Number %d is divisible by 3\n",a);
else
printf("Number %d is not devisible by zero\n",a);
return 0;Branching in a 3-branch program
This branching is done using the if-else if- else command and two conditions (logical variables)
if(b1)
{
command1;
}
else if(b2)
{
command2
}
else{
command3;
}
{
command1;
}
else if(b2)
{
command2
}
else{
command3;
}
where
b1 and b2 are logical variable
Branching in the program can be performed based on logical expressions written durectically within parentheses, without introducing bool variables
Suppose the user has entered the whole number "a".
b1 and b2 are logical variable
Branching in the program can be performed based on logical expressions written durectically within parentheses, without introducing bool variables
Suppose the user has entered the whole number "a".
if(a>0) { command1; } else if(a==0) { command2 } else{ command3; } |
Branching in a program with more than 2 branches-video(with an English title) |
Example 2: Enter the whole number and determine its sign, ie whether the number is positive, zero or negative
We reserve the first memory for an integer a, and then enter it.
int a;
cin>>a;
cin>>a;
Further, in order to determine whether the number is positive zero or negative, we use the selection statements, i.e. if - else if-else command:
if(a > 0){
cout << "The number is positive";
} else if(a == 0){
cout << "The number is zero";
}
else{
cout << "The number is negative";
}
cout << "The number is positive";
} else if(a == 0){
cout << "The number is zero";
}
else{
cout << "The number is negative";
}
First, the first condition a> 0 is examined. If it is a point, the command will be executed within the braid brackets below the if, and then the remaining commands will be skipped.
If the first condition is incorrect, the program goes to another condition a == 0 next to else if.
If he is typing, the message "Number is equal to zero" and the rest of the expanded if the command is skipped.
If this condition is not true, then the command under another clause is executed. Note that besides else there are no conditions. It is not needed, because the command below is executed if all the previous conditions are incorrect. In this example, if the number was not positive and was not zero, it is assumed to be negative.
The clause else, if there is, is always the last. On the last, third place, instead of else, the command could end with else if. The difference over the other is that, besides else if it is necessary to write a certain requirement. In this example, the condition would be "a <0".
The next code would perform the set task in exactly the same way as it was performed using another clause in the last place:
If the first condition is incorrect, the program goes to another condition a == 0 next to else if.
If he is typing, the message "Number is equal to zero" and the rest of the expanded if the command is skipped.
If this condition is not true, then the command under another clause is executed. Note that besides else there are no conditions. It is not needed, because the command below is executed if all the previous conditions are incorrect. In this example, if the number was not positive and was not zero, it is assumed to be negative.
The clause else, if there is, is always the last. On the last, third place, instead of else, the command could end with else if. The difference over the other is that, besides else if it is necessary to write a certain requirement. In this example, the condition would be "a <0".
The next code would perform the set task in exactly the same way as it was performed using another clause in the last place:
if(a > 0){
cout << "The number is positive";
} else if(a == 0){
cout << "The number is zero";
}
else if(a <0 ){
cout << "The number is negative";
}
cout << "The number is positive";
} else if(a == 0){
cout << "The number is zero";
}
else if(a <0 ){
cout << "The number is negative";
}
You can find examples of branching in the program on the webpage Selection statements - examples
Previous
|< Operators in C/C++ languages |
Next
Loops in C/C++ >| |