Selection

Lesson by Rogério Júnior

In this lesson we will learn how to use the if command. if is how we use the logical operators of a language. It conditions the execution of a sequence of commands to any condition, executing it only if the condition is true. Your grammar is if (condition) {do this} . That is, we call if , we open parentheses and insert a condition inside them. If the condition is true, the program will execute everything inside the keys that succeed if . Note that it is not necessary to insert "; " after the keys.

In this way, a code that prints a number saved in a variable called x only if it is greater than 10 looks like this:


if(x>10){
  cout<<x;
}

Besides that, after a if, we can also use an else. For example:


if(condition){
  //do this
}else{
  //do that
}

So, if the condition is true, do this, else (if it is false) do that. Remember that what is between the keys can be any series of commands, including others if's and else's. That said, a code that would save the absolute value of an integer x In a variable mod would look like this:


if( x>0 ){ 
  mod = x;
}else{
  mod = -x;
}

It is worth remembering that the equality operator is==, and not =. To print a number x only if it equals 5, we must write:


if(x==5){
  cout<<x;
}

Remember that = means assignment, so if(x=5) would assign the value of 5 to the integer x . Other common operators are the "Not equal to" (!=), the "less than or equal to" (<=), "greater than or equal to" (>=), "less than" (<) and "greater than" (>).

To use the full power of the if command involves using the Logical Operators and and or. The && or and, works like this: if(condition1 and condition2){ do this }, the program will execute this only if condition1 and condition2 are both true. It can be used multiple times, that is, if we want to do something only if three conditions are true at the same time, we can write if(condition1 and condition2 and condition3).

The || operator, which can also be used as the or operator, is used in the following way: if(condition1 or condition2){ do this }, the program will execute this if condition1 or condition2, or both, are true. Just like and It can also be used multiple times. Let's look at the code of a program that reads a number n on the screen and then prints a written line "is in the interval" if the number is between 5 and 10 (including 5 and 10) and printing "is not in range" otherwise.

It is important to note that in C ++ it is not possible to do something like 5<=n<=10, because the computer simply does not understand this type of construction, it is really necessary to separate in two blocks of the form 5<=n and n<=10


#include <iostream> // cin and cout

using namespace std;

int main(){

  int n; // declare the variable n
  cin>>n; // read the value and save it in n

  if(5<=n and n<=10){
    cout<<"is in the interval\n"; // if n is between 5 and 10, print that it is in the interval
  }
  else{
    cout<<"is not in the interval\n"; // if it isn't, print "is not in the interval"
  }

  return 0;
}