Conditions in C++ - ProgrammerTech
Language:

Search

Conditions in C++

  • Share this:
Conditions in C++

Conditions in C++

Conditions cpp are used to define the way the program works according to the variables that occur in the code. You can put as many conditions as you want in the program, and you can also put the conditions inside each other.

Types of conditional clauses in C++

  1. if.
  2. else.
  3. else if.
  4. switch.

 

The if conditional statement in C++

The if statement specifies the path of the program under certain conditions, and the most important example of this is when you enter your password to lock your mobile device. The device takes your password that you entered and compares it with the password you previously specified in the system. If the number is correct, your device will be unlocked. Otherwise, the number is not correct and the system will reject you and give you another try.

The general form of writing an if statement in C++

if (condition)
{
The code that will be run if the condition is met
}

Example 1

if (password == 12345)
{
	cout << "The password is correct";
}

We set the condition that the phone password is 12345, and when the user types the password correctly, it will print to him that the password is correct and unlock the phone.

Example 2

#include <iostream>
using namespace std;
int main()
{
    const int password = 12345;
    int input;
    cout << "Enter the password";
    cin >> input;
    if(input == password)
    {
        cout << "The password is correct" << endl;
    }
    return 0;
}

We created a numerical variable named const, which has the secret code and the value 12345, then we created an input variable to prompt the user and print it to enter the secret number. And through the cin function, we asked the user to enter a number from him, and we set a condition that in case of matching, the user would be printed the correct password.

 

The else conditional clause in C++

The else clause is used if the condition of the first if clause is not met, and the else clause cannot exist without the preceding if clause. Any if statement, if its condition is met, the commands inside the if statement are run, and the commands inside else are not met, and vice versa. Whereas, if the condition in if is not satisfied, the commands in else are not satisfied, and the commands in the if statement are not satisfied.

The general form of writing an else statement in C++

if (expression)
	{
		exprission1  /If the condition is met
	}
else
	{
	    expression2  / If the condition is not met
	}

Example 3

#include <iostream>
using namespace std;
int main()
{
    const int password = 12345;
    int input;
    cout << "Enter the password";
    cin >> input;
    if(input == password)
    {
        cout << "The password is correct" << endl;
    }
    else
    {
    	cout << "You entered a wrong number" << endl;
    }
    return 0;
}

We created a numerical variable named const, which has the secret code and the value 12345, then we created an input variable to prompt the user and print it to enter the secret number. And through the cin function, we asked the user to enter a number from him, and we set a condition that in case of matching, the user would be printed the correct password. And we put an else clause in case the user entered an incorrect number, and the sentence will be printed for him. I entered the wrong number.

Example 4

#include <iostream>
using namespace std;
int main()
{
if (7 > 0)
    {
        cout << "good" << endl;
    }
    else
    {
        cout << "bad" << endl;
    }
}

We gave the program if the number 7 is greater than zero 0, type the word good, that is, when the condition is met. And the second part of the condition, if this condition is not met, print for us no good, and in fact the condition is fulfilled, that the 7 is greater than zero. The word good is printed for us in the output.

The program can only come with an if statement without else, but else cannot come without an if statement before it because it is an essential part of the condition.

 

The else if conditional statement in C++

The else if statement is a way to make a set of multiple choices that start with an if statement and follow an else if statement and only one of these choices is satisfied. The program first checks the state of the first if statement, and if it is true, it will not check any of the other else if statements that follow it until the last else statement. So if you want to make a block of options that only one of them is checked, you start with an if statement. The ending will be in an else clause, or even an else if if it is not followed by another else clause. The structure of the if clause ends, as the if clauses and the else if clauses that follow them are all related as if they were a single block of commands, only one of which is checked and the others are not. If the first sentence is not checked, it goes directly to the second else if sentence that follows it, and if it is true, it will not check any other else if clauses. And so on for all else if clauses within the set, even if none of these options is checked and you find else at the end and that the else command will definitely be checked, i.e. not any of the above.

The general form of writing an else if statement in C++

if (Condition 1) {
  /If the condition is true, this will be implemented
} else if (Condition 2) {
  /If the first condition is false and the second is true,
} else {
  /This is done if the sentences above are wrong
}

Example 5

#include <iostream>
using namespace std;
int main()
{
int time = 23;
if (time < 12) {
  cout << "good morning.";
} else if (time < 19) {
  cout << "Happy day.";
} else {
  cout << "Good evening.";
}
}

We created a scalar variable, time, and its value is 23, that is, it is 11 at night, and we put the condition if if the variable is less than 12, then print Good morning, that is, the time is less than 12 noon. And if it is less than 19, print a happy day, i.e. less than 7 pm. Otherwise, print Good evening, because it will surely be night.

 

switch clause in C++

The switch clause is similar in its action to the conditional if clause, and it is preferable to use the switch clause in comparison operations and investigation in the case of multi-comparison in terms of equality.

The general form of the switch statement in C++

switch(Expression)
{
case 1 :
break;
default:
break; 
}

For example, if between the brackets () the number 1 will be implemented, and if the number 2 will be implemented by the second, and so on... If what is between the brackets is empty, the default will be implemented, and it does not accept only integer numbers.

Example 6

#include <iostream>
using namespace std;
int main()
{
    switch (1)
    {
    case 1:cout << "case #1" << endl;
        break;
    case 2:cout << "case #2" << endl;
        break;
    case 3:cout << "case #3" << endl;
        break;
    default:
        cout << "not rang" << endl;
        break;
    }
}

We wrote a switch statement and put the condition in parentheses as 1 and we started implementing several possibilities on this condition.

  1. If the first possibility is 1 it prints case #1 and stops.
  2. If the second possibility is 2, it prints case #2 and stops.
  3. If the third possibility is 3, it prints case #3 and stops.
  4. Otherwise, it prints not rang because it is a default value.

When you run the code, the result will be:

case #1

We also noticed that what is between the parentheses is the number 1 case1 is printed for us and if we put it 2 it will print 2 and so on .... But if its value is greater than the case values ​​it will print default.

Example 7

#include <iostream>
using namespace std;
int main()
{
    switch (30)
    {
    case 1:cout << "case #1" << endl;
        break;
    case 2:cout << "case #2" << endl;
        break;
    case 3:cout << "case #3" << endl;
        break;
    default:
        cout << "not rang" << endl;
        break;
    }
}

We wrote a switch statement and put the condition in parentheses as 30 and we started implementing several possibilities on this condition. Which:

  1. If the first possibility is 1 it prints case #1 and stops.
  2. If the second possibility is 2, it prints case #2 and stops.
  3. If the third possibility is 3, it prints case #3 and stops.
  4. Otherwise, it prints not rang because it is a default value.

When you run the code, the result will be

not rang

If we do not write break, it will print the complete sentences for us because the task of break is to stop the action when the condition is executed.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com