| by Arround The Web | No comments

C# Conditional logical OR operator

The conditional logical operators are those that evaluate any statement based on one or more conditions. Conditions are used for decision-making. The logical OR operator can be utilized to decide. ‘OR’ means that even if one condition is satisfied, it will still execute the required code. This ‘OR’ operator is useful when we have different choices in the code and either one of them is true, then execute the rest of the code. Logical operators connect two or more things and conditional logical operators link them based on a condition. When condition_1 is true, condition_2 is not checked. They connect two expressions or conditions. For example, if tomorrow I have a test I will attend a school. Or if tomorrow the weather is clear, I will attend school. The first condition is evaluated first and if the first condition is true, then I will go to school. When the first condition is not fulfilled, then the second expression is evaluated. In both cases, if one condition is true, then I have to attend school. If both are false, then I will not attend. This is exactly what the ‘OR’ operator does in programming.

Syntax:

if(expression_1 > 7 || expression_2 < 4)
  {
}

The conditional logical OR operator works if any of the two conditions is satisfied; otherwise the ‘if’ statement’s body would be carried out. But if both are false the code will terminate or we can also go for an if-else statement. In the above example, if expression_1 is greater than 7 or expression_2 is less than 4, then execute the body of the ‘if’ statement. If the first defined condition is encountered, the second defined condition will not be tested.

Example 1:

A simple program to elaborate the working of conditional logical OR operator.

Let us look at the code first, define and initialize two integer type variables named ‘value_1’ and ‘value_2’. Now, use the ‘if’ statement to check the two integer variables based on conditions. The first expression is when the ‘value_1 == 78’ is satisfied, then the ‘if’ statement’s body is performed. And if this condition is false, move to the next condition given in the ‘if’ statement. The second condition is if ‘value_2 == 63’ then execute the body of ‘if’. As we can see in the code, ‘value_1’ is assigned 78 and that makes the first condition in the ‘if’ statement true. When the first condition is satisfied, the compiler executes the body without executing the second condition. But when the first expression is false, it moves to the next expression. If the next expression is true, it executes the body; if not then it will not execute the body of ‘if’. Inside the body of ‘if’, we performed addition on the two defined variables and stored their sum in another integer type variable named ‘sum’. Then, print the sum with text on the screen by the use of the Console.WriteLine() function. Here, we concatenate the message with the sum variable.

C:\Users\hira tassadaq\Desktop\article\november\OR operator\1out.png

Example 2:

In the code, we will apply the ‘OR’ operator to count how many vowels are present in a required string.

C:\Users\hira tassadaq\Desktop\article\november\OR operator\2.png

First, declare and initialize a string here it is named ‘input’. Then, declare another variable of integer type say ‘vowels’ and initialize it with 0. After that, use a ‘for’ to loop the string till the last letter of the specified string is read. Within the ‘for’ loop, declare integer type variable ‘i’ because to iterate the required string we should have known the length of the string, for how long, and until which character we want to loop. For that, we will use ‘input.Length’ to acquire the length of the required string. The variable ‘i’ will iterate and increment with i++. Within the ‘for’ loop, we have an ‘if’ statement with multiple conditions. Even if anyone of them is satisfied, the expression of ‘if’ will be implemented. In the ‘if’ statement, the first condition is input[i] == ‘a’, the input[i] will iterate from index 0 till the length of the ‘input’ string. When i=0, the first character of the required string, will be compared with the character ‘a’. If the string’s first character matches the condition then the body will execute. If not, then the next condition will be evaluated. If the second expression is not true, then the next expression would be evaluated and so on. Then, the variable ‘i’ is incremented. At index 1, all the conditions will be evaluated and the loop will continue. Whenever the condition is satisfied, the body of ‘if’ has ‘vowels’ that will increment every time. Outside the ‘for’ statement, the Console.Write() method will show the message and the number of vowels in the required string.

C:\Users\hira tassadaq\Desktop\article\november\OR operator\2out.png

There were three vowels in the string, we can manually count them to verify the result.

Example 3:

The example will explain the use of the append method with the ‘OR’ operator.

Here, we have utilized the AND operator with the OR operator, declare a variable as ‘Marks’, then apply the ‘if’ statement to evaluate the status of Marks. In the ‘if’ statement, we have two conditions. One is if the Marks are greater than 70 and marks are less than and equal to 100, execute the code below. And if Marks are greater than and equal to 50 but less than and equal to 70 execute the code below. We could do this with one condition but we have done it this way just to explain that we can use OR with other operators. (&&) is used when there is a compulsion if both have to be true. If any of the two expressions are true, the body of the ‘if’ statement will implement. If both of them are false, ‘else’ statement will be executed.

C:\Users\hira tassadaq\Desktop\article\november\OR operator\3out.png

Conclusion

In this guide, we explored the conditional logical ‘OR’ operator (||) and explained it with different example codes. We have more than two conditions in one decision statement. With the help of the ‘OR’ operator, things become easy when we have different choices. If anyone condition becomes true then execute the code. We can also use other logical operators with the ‘OR’ operator as we have done in the last example. The logical ‘OR’ operator is simple and useful in C# programming.

Share Button

Source: linuxhint.com

Leave a Reply