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.
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.
In the code, we will apply the ‘OR’ operator to count how many vowels are present in a required string.
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.
There were three vowels in the string, we can manually count them to verify the result.
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.
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.