Min menu

Pages

VB

Conditions in C++


The concept of conditions inC++

Conditions in C Plus Plus  (conditions)It is used to determine the way the program works in relation to the variables that occur in the code.
As a simple example, you can build a program to watch movies, when entering it, it asks the user at the beginning to enter his age in order to display pens that fit his age.

You can put as many conditions as you want in one program, and you can put the conditions inside each other as well.

Types of conditional sentences inC++

sentence name Indications
if - else - else ifstatements We use them if we want to execute a certain code if a condition or a set of conditions we set is true.
Explanation of the conditional sentences in C Plus Plus
switch statement We use it if we want to test the value of a particular variable with a list of possibilities that we set, and if this value is equal to any possibility we set, only the orders we put in this possibility will be executed.
Explanation of the sentence switch in C Plus Plus

conditional sentences in C++

Condition sentences in general in C++

The general form of setting conditions is the following.

if ( condition )
{
    // if the condition is true, run this code
}

else if ( condition )
{
    // if the condition is true, run this code
}

else
{
    // run this code if the code is not recognized in any condition
}

note

You don't need to use the three clauses for every condition you put in the program, but you are forced to use the condition clause ifwith any condition.
Continue through the whole lesson until you know all the ways to set conditions.

clause clause if in C++

ifIn the Arabic language, it means "if", and it is used only if you want to implement a specific code according to a specific condition.


first example

If the value of the variable Sis greater than 5 , the sentence will be printed:   S is bigger than 5.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int S = 0;
	  
		  if( S > 5 )
		  {
		  cout << "S is bigger than 5";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                  

Here he asked himself the following: Is the value of the variable Sgreater than 5 ?
The answer to the condition was yes( false), so the print command in the condition clause was not executed.


second example

If the value of the variable Sis greater than 5 , the sentence will be printed:   S is bigger than 5.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int S = 30;
	  
		  if( S > 5 )
		  {
		  cout << "S is bigger than 5";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    S is bigger than 5
		

Here he asked himself the following: Is the value of the variable Sgreater than 5 ?
The answer to the condition was yes( true),So execute the print command in the condition clause.

clause clause else in C++

elseIn the Arabic language, it means "anything else", and it is used only if we want to implement a specific code if the result of all the conditions before it is equalfalse.

It should always be placed at the end, because it is used if no conditional statement is executed before it.

So, if the program executes the statement if, else ifit will ignore the statement else.
And if it does not execute any of the sentences if, else ifit will execute the sentence else.


first example

If the value of the variable Sis 5 , the sentence will be printed:   S is equal 5.

If the value of the variable Sis not 5 , the sentence:   S is not equal 5.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int S = 5;
	  
		  if( S == 5 )
		  {
		  cout << "S is equal 5";
		  }
	  
		  else
		  {
		  cout << "S is not equal 5";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    S is equal 5
		

Here he asked himself the following: Is the value of the variable Sequal to 5 ?
The answer to the condition was yes( true) , so execute the print command in the sentence if.


second example

If the value of the variable Sis 5 , the sentence will be printed:   S is equal 5.

If the value of the variable Sis not 5 , the sentence:   S is not equal 5.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int S = 20;
	  
		  if( S == 5 )
		  {
		  cout << "S is equal 5";
		  }
	  
		  else
		  {
		  cout << "S is not equal 5";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    S is not equal 5
		

Here he asked himself the following: Is the value of the variable Sequal to 5 ?
The answer to the condition was yes( false) , so execute the print command in the sentence else.

clause clause else if in C++

A sentence else ifis used if you want to put more than one possibility (that is, more than one condition).

Sentence or sentences The else ifare placed in the middle, that is, between the two sentences ifand else.


Example

If the value of the variable numberis 1 the word:   one.
If the value of the variable numberis 2 , the word:   two.
If the value of the variable numberis 3 , the word:   three.
If the value of the variable numberis greater than or equal to 4 , the sentence will be printed:   four or greater.
If the value of the variable numberis less than 0 , the sentence will be printed:   negative number.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int number = 3;
	  
		  if( number == 1 )
		  {
		  cout << "one";
		  }
	  
		  else if( number == 2 )
		  {
		  cout << "two";
		  }
	  
		  else if( number == 3 )
		  {
		  cout << "three";
		  }
	  
		  else if( number >= 4 )
		  {
		  cout << "four or greater";
		  }
	  
		  else
		  {
		  cout << "negative number";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    three
		

Here he asked himself the following: Is the value of the variable numberequal to 1 ?
The answer to the condition was yes( false) , then move on to the next condition.

Then he asked himself the following: Is the value of the variable numberequal to 2 ?
The answer to the condition was yes( false) , then move on to the next condition.

Then he asked himself the following: Is the value of the variable numberequal to 3 ?
The answer to the condition this time was yes( true), so he executed the printing command found in the third condition sentence, and then bypassed all the condition sentences that came after it.

wholesale switch in C++

Definition of the sentence switch in C++

switchWe use it if we want to test the value of a particular variable with a list of possibilities that we set, and if this value is equal to any possibility we set, only the orders we put in this possibility will be executed.

Every possibility we give is calledcase.

The types of variables whose value can be tested using this sentence are:
int- byte- short- char-enum.


How to define the sentence  switch in C++

We can define it in several forms, the basic form is the following:

switch ( expression ) {

    case value :
        // Statements
        break ;

    case value :
        // Statements
        break ;

    default :
        // Statements
        break ;

}

  • switch means check the value of the variable in parentheses.


  • expression here means the variable whose value we want to test.
    The type of variable that allows us to test: int- byte- short- char- String-enum.


  • case means state, value means value, and Statements means commands.
    This means that if the expression value is equal to this value, it will execute the commands placed after the colon :.
    Now after executing all the commands placed after the colon, it must be placed in breakorder to exit the sentence switchdirectly instead of moving to the casenext in the sentenceswitch.
    We can put the number we want in the casesentenceswitch.
    Note: the expression and the value must be of the same type.


  • defaultIt means by default and it is the same as the idea of ​​the sentence else, and we can not put it either.
    This sentence is executed only if it does not implement anything casein the sentence switch, so we put it in the other.



note

No need to put breakthe last case because the program will exit the sentence switchanyway.


You will understand the idea of ​​the sentence switchthrough examples and we will show you how you can implement the same commands in more than one situation, through examples.


first example

We will test the value of the variable xand its type int.

We will put several cases, and each case prints a specific thing.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int x = 2;
	  
		  switch( x ) // x test the value of the variable
		  {
		  case 1: // if it is 1 the print command placed in it will be executed
		  cout << "x contain 1";
		  break;
	  
		  case 2: // if it is 2 the print command placed in it will be executed
		  cout << "x contain 2";
		  break;
	  
		  case 3: // if it is 3 the print command placed in it will be executed
		  cout << "x contain 3";
		  break;
	  
		  default: // if it is not equal to any of the values ​​set, the print command placed in it will be executed
		  cout << "x contain a different value";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    x contain 2
		

x We note that the print command in the second case has been executed because the value of the variable is2 .

Here he asked himself the following: Is the value of the variable xequal to 1 ?
The answer to the condition was yes( false), go to the next case.

Then he asked himself the following: Is the value of the variable xequal to 2 ?
The answer to the condition was yes( true)So, he executed the print command in this case, and then he got out of the switchentire sentence because of the sentencebreak.



second example

We will test the value of the variable xand its type int.

We will put several cases, and each case prints a specific thing.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int x = 5;
	  
		  switch( x ) // x test the value of the variable
		  {
		  case 1: // if it is 1 the print command placed in it will be executed
		  cout << "x contain 1";
		  break;
	  
		  case 2: // if it is 2 the print command placed in it will be executed
		  cout << "x contain 2";
		  break;
	  
		  case 3: // if it is 3 the print command placed in it will be executed
		  cout << "x contain 3";
		  break;
	  
		  default: // if it is not equal to any of the values ​​set, the print command placed in it will be executed
		  cout << "x contain a different value";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    x contain a different value
		

We note that the print command in the default case has been executed because the value of the variable xis not equal to any of the values ​​set in the cases.

Here he asked himself the following: Is the value of the variable xequal to 1 ?
The answer to the condition was yes( false), go to the next case.

Then he asked himself the following: Is the value of the variable xequal to 2 ?
The answer to the condition was yes( false), go to the next case.

Then he asked himself the following: Is the value of the variable xequal to 3 ?
The answer to the condition was yes( false), go to the next case.

Since he did not find any case in which the value was equal to the value of the variable being tested, he executed the commands in the default case default, and when he finished executing the commands, he left the switchentire sentence.

Putting the same commands to more than one case

If you want to put the same commands for more than one case, you have to put the cases under each other, then write the commands, then putbreak.


Example

We will test the value of the variable xand its type int.

We will put three cases carrying out the same orders.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int x = 3;
	  
		  switch( x ) // x test the value of the variable
		  {
		  case 1: // if it is 1, 2 or 3 the print command will be executed
		  case 2:
		  case 3:
		  cout << "x contain 1 or 2 or 3";
		  break;
	  
		  default: // if it is not equal to any of the values ​​set, the print command placed in it will be executed
		  cout << "x contain a different value";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    x contain 1 or 2 or 3
		

We note that the print command placed for the first three cases has been executed because a value xof3 .

Here he asked himself the following: Is the value of the variable xequal to 1 , 2 or 3 ?
The answer to the condition was yes( true)So, he executed the printing command set for these three cases, and then he left the switchentire sentence because of the sentencebreak.

Other ideas and methods for setting conditions inC++

The ways of setting conditions are many and varied, and we can put conditions inside some of them and it is called thatNested Conditional.
We can also put more than one conditional inside conditional sentences ifor else ifby using logical operators.


Here we have put examples that teach you how to put more than one condition in a sentence ifor else ifusingtheRelational Operators.

How to put more than one conditional in the if clause or else if


Here we have put an example that teaches you how to put the conditions inside each other.

How to put conditions inside each other

Put more than one condition in a sentence if in C++

You can put more than one conditional inside the conditional statement using the &&or operator ||.
The operator &&is used if you want to implement a specific code if the answer to all the conditions set is equal totrue.
The operator ||is used if you want to execute a specific code if the answer to at least one condition istrue.


first example

If the value of the variable is abetween 0 and 20 , print the sentence:   acceptable number.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 14;
	  
		  if( a >= 0 && a <=20 )
		  {
		  cout << "acceptable number";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    acceptable number
		

We note that the print command has been executed because the value of the variable is abetween 0 and 20

Here he asked himself two questions.

• The first question: Is the value of the variable agreater or equal to 0 ?
The answer to the first condition wastrue.

• The second question: Is the value of the variable aless than or equal to 20 ?
The answer to the second condition was alsotrue.

Since both answers were trueexecuting the print command.



second example

If the value of the variable is abetween 0 and 20 , print the sentence:   acceptable number.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 26;
	  
		  if( a >= 0 && a <=20 )
		  {
		  cout << "acceptable number";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                  

We notice that the print command is not executed because the value of the variable ais not between 0 and 20

Here he asked himself two questions.

• The first question: Is the value of the variable agreater or equal to 0 ?
The answer to the first condition wastrue.

• The second question: Is the value of the variable aless than or equal to 20 ?
The answer to the second condition wasfalse.

Since one of the two answers did not trueimplement the print function.



third example

If the value of the variable ais 1 , 2 , or 3 , print the sentence:   you choose a valid number.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 2;
	  
		  if( a == 1 || a == 2 || a == 3 )
		  {
		  cout << "'a' is a valid number";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    'a' is a valid number
		

We note that he executed the print command because the value of the variable ais 2

Here he would ask himself three questions because there are three conditions, but he asked himself only two questions.

• The first question: Is the value of the variable aequal to 1 ?
The answer to the first condition was, so he falsemoved to the next condition.

• The second question: Is the value of the variable aequal to 2 ?
The answer to the second condition wastrue.

Since one of the answers was truedirectly executing the command to print and did not even consider the last condition.

How to put a condition inside another condition in C++

Putting a condition within a condition in C++

We can put the conditions inside each other, and we can put as many conditions as we want.
In actual programs, the programmer puts many conditions inside each other, according to the idea that he wants to implement in his program.

As a simple example, let's say we want to convert this image into a program.

Example

Here we have prepared a variable representing the gender of its name isMale, and we said if its value trueis, it means that the person is male.
And if it is equal, falseit means that the person is a female.

In addition to a variable representing age, his name isage.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  bool isMale = false;
		  int age = 14;
	  
		  if( isMale == true )
		  {
		  cout << "Gender: male";
	  
		  if( age <= 21 )
		  {
		  cout << "\nHe is a young boy";
		  }
		  }
	  
		  else
		  {
		  cout << "Gender: female";
	  
		  if( age <= 21 )
		  {
		  cout << "\nShe is a young girl";
		  }
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    Gender: female
		  She is a young girl
	

Here he asked himself the following: Is the value of the variable isMaleequal true?
The answer to the condition was false, so he moved to the hypothetical condition sentenceelse implement what is inside.

First, he executed the print function in it, so he printed the sentence   Gender: female .

Then he found another condition inside the default condition, and asked himself the following: Is the value of the variable ageless than or equal to 21 ?
The answer to the condition was true, so he implemented the print function in it, so he printed the sentence   She is a young girl.

_________ _____