Tag Archivio per: Operator


Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Regular data structures. With LINQ, we can also perform database query operations. LINQ supports many methods and functions that are used for Data manipulation like Updating, deletion, Insertion, and etc.

LINQ ElementAtOrDefault

LINQ ElementAtOrDefault operator is used to search the element present in a data source. It searches the element based on the index value provided. If the element is found at a specified index, the element is returned. Otherwise, it returns empty by default.

Syntax:

input_source.ElementAtOrDefault(index_number);

Where input_source is the data source that can be an array or List or any collection.

We will understand this by seeing the following examples.

As we know, Indexing starts with 0.

Example 1:

Here, we will create a list that has 10 integers and get the elements based on index numbers provided inside ElementAtOrDefault().

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class – Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create List named input_numbers
    var input_numbers = new  List() {100,200,300,456,12,34,56,78,54,44};
   
    //return 6th value
    Console.WriteLine(«Element present at 6th position: «+input_numbers.ElementAtOrDefault (5));
   
    //return 9th value
    Console.WriteLine(«Element present at 9th position: «+input_numbers.ElementAtOrDefault (8));
   
    //return 1st value
    Console.WriteLine(«Element present at 1st position: «+input_numbers.ElementAtOrDefault (0));
   
    //return 4th value
    Console.WriteLine(«Element present at 4th position: «+input_numbers.ElementAtOrDefault (3));
   
    //return 10th value
    Console.WriteLine(«Element present at 10th position: «+input_numbers.ElementAtOrDefault (9));
   
    }
   
}

Output:

Explanation:

1. So first, we created a list named input_numbers that holds 10 integer elements.

2. After that, we searched and displayed the below values using their index positions.

Example 2:

Here, we will create a list that has 3 strings and get the elements based on index numbers provided inside ElementAtOrDefault().

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class – Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create List named input_strings
    var input_strings = new  List() {«Linuxhint»,«c#»,«vignan»};
   
    //return 1st value
    Console.WriteLine(«Element present at 1st  position: «+input_strings.ElementAtOrDefault(0));
   
        //return 3rd value
    Console.WriteLine(«Element present at 3rd position: «+input_strings.ElementAtOrDefault(2));
   
    //return 6th value
    Console.WriteLine(«Element present at 6th position: «+input_strings.ElementAtOrDefault(5));
   
    //return 9th value
    Console.WriteLine(«Element present at 9th position: «+input_strings.ElementAtOrDefault(8));
   
   
    }
   
}

Output:

Explanation:

1. So first, we created a list named input_strings that holds 3 string elements.

2. After that, we searched and displayed the below strings using their index positions.

The index positions 6 and 9 are not present. Hence, empty is returned.

This is how to return the element based on the index number using ElementAtOrDefault operator available in C# – LINQ. If the element is found at index, the element is returned. If it is not found, it returns empty by default. We demonstrated two different examples to understand the concept better and make sure to use the modules using System, using System.Linq, using System.Collections.Generic in your code.



Source link


The LINQ Skip operator is used to return the remaining values by skipping the values from the given data structure. It takes an integer value as a parameter that represents the total number of elements to be skipped from the given data structure.

Syntax:

Or

from element in input_source select element.Skip(n)

Where input_source is the data source.

Parameters:

It takes an integer value(n) that skips that number of elements from the given data structure from first and returns the remaining elements.

Now, consider a scenario where the value of n(integer) is greater than the total number of elements in the data structure, all the elements in the data structure are skipped without any error.

Example 1:

Here, we will create a list that has 10 integers and skip 5 elements using the Skip operator using both the methods(Method and Query).

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class – Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create List named input
    var input = new  List() {34,56,78,12,34,53,56,78,90,100};
   
    //return remaining integers by skipping 5 values
    var result=input.Skip(5);
    Console.WriteLine(«—Using Method—«);
    //display the result
  foreach (int i in result)  
            {  
Console.WriteLine(i);  
            }  
   
    Console.WriteLine(«—Using Query—«);
   
    //return remaining integers by skipping 5 values
    foreach (int j in (from  element in input select element).Skip(5))  
            {  
Console.WriteLine(j);  
            }  
   
    }
   
}

Output:

Explanation:

1. First, we created a list named input_numbers that hold 10 integer elements.

2. After that, we are skipping 5 values using Skip() with Method syntax.

3. Finally, we are displaying the remaining values using a foreach loop.

4. Similarly, we are displaying remaining values by skipping 5 values using Query Syntax.

Example 2:

Here, we will create a string array that has 4 strings and skip 6 elements using the Skip operator using both the methods(Method and Query).

using System;
using System.Linq;

 //create a class – Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create string array named input
    string[] input =  {«Linuxhint»,«java»,«html»,«sravan»};
   
    //skip all strings
    var result=input.Skip(6);
    Console.WriteLine(«—Using Method—«);
    //display the result
  foreach (string i in result)  
            {  
Console.WriteLine(i);  
            }  
   
    Console.WriteLine(«—Using Query—«);
   
    //display the result by skipping all strings.
    foreach (string j in (from  element in input select element).Skip(6))  
            {  
Console.WriteLine(j);  
            }  
   
    }
   
}

Output:

So, you can see that all the elements are skipped.

Explanation:

Create a string array named input that holds 4 strings.

Use Method syntax to skip all values using the Skip() operator.

Use Query syntax to skip all values using the Skip() operator.

Conclusion

We have seen how to skip the elements using Skip() operator in C# – LINQ. It takes an integer value(n) that skips that number of elements from the given data structure from first and returns the remaining elements.

In each example we implemented Skip() with Method as well as Query syntax. Finally, we came to notice that if the value of n(integer) is greater than the total number of elements in the data structure, then all the elements in the data structure are skipped without any error.



Source link


Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Común data structures. With LINQ, we can also perform database query operations. LINQ supports many methods and functions that are used for data manipulation, like updating, deletion, insertion, etc.

LINQ Take()

The LINQ Take operator is used to return the values from the given data structure. It takes an integer value as a parameter that represents the total number of elements to be retrieved from the given data structure.

Syntax:

Or

from element in input_source select element.Take(n)

Where input_source is the data source.

Parameters:

It takes an integer value(n) which is used to return that number of elements from the given data structure.

Now consider a scenario where the value of n(integer) is greater than the total number of elements in the data structure. All the elements in the data structure are returned without any error.

Example 1:

Here, we will create a list that has 10 integers and get 5 elements using the Take operator using both the methods(Method and Query).

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class – Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create List named input
    var input = new  List() {34,56,78,12,34,53,56,78,90,100};
   
    //return 5 integers
    var result=input.Take(5);
     Console.WriteLine(«—Using Method—«);
    //display the result
  foreach (int i in result)  
            {  
                Console.WriteLine(i);  
            }  
   
     Console.WriteLine(«—Using Query—«);
   
    //display the result by returning 5 integers.
    foreach (int j in (from  element in input select element).Take(5))  
            {  
                Console.WriteLine(j);  
            }  
   
    }
   
}

Output:

Explanation:

1. So firstly, we created a list named input_numbers that holds 10 integer elements.

2. After that we are taking only 5 values using Take() with Method syntax.

3. Finally, we are displaying the result using a foreach loop.

Example 2:

Here, we will create a string array that has 4 strings and get 6 elements using the Take operator using both the methods(Method and Query).

using System;
using System.Linq;

 //create a class – Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create string array named input
     string[] input =  {«Linuxhint»,«java»,«html»,«sravan»};
   
    //return all strings
    var result=input.Take(6);
     Console.WriteLine(«—Using Method—«);
    //display the result
  foreach (string i in result)  
            {  
                Console.WriteLine(i);  
            }  
   
     Console.WriteLine(«—Using Query—«);
   
    //display the result by returning all strings.
    foreach (string j in (from  element in input select element).Take(6))  
            {  
                Console.WriteLine(j);  
            }  
   
    }
   
}

Output:

You can see that all the elements from the string array were returned without any error.

Explanation:

Create a string array naemd input that holds 4 strings.

Use Method syntax to get all values using the Take() operator.

Use Query syntax to get all values using the Take() operator.

Conclusion

So we have seen how to return the elements using Take() operator in C# – LINQ. It takes an integer value as a parameter that represents the total number of elements to be retired from the given data structure. In each example, we implemented Take() with Method as well as Query syntax. Finally, we came to notice that if the value of an integer is greater than the total number of elements in the data structure, then all the elements in the data structure are returned without any error.



Source link


When returning from a SELECT query in SQLite, the Except clause is essential for excluding some rows. If we use the Except clause with consecutive SQLite Select statements, it returns all data with the first select expression except those from the second. With examples in this SQLite article, we will learn about the SQLite Except operator. Then, we will learn how to use it to retrieve all the records from this first select statement beside the data produced by a second select statement.

In most cases, we utilize the SQLite Except clause with Select statements. All of the Select statements, then, that include the Except clause should have the same number of records. The Except clause in SQLite is summarized in the figure below:

If you look at the above SQLite Except clause pictorial representation, it will return the records that are in the color shaded area. This indicates that the Except clause will provide all of the records from the first Select statement except those that are in the second Select statement.

Syntax of the Except clause in SQLite:

The SQLite Except clause is used with a select statement in the following pattern:

SELECT column_Name1, column_Name2,…, columnn_Name_n
FROM TABLE_NAME
[WHERE condition]
EXCEPT

SELECT column_Name1, column_Name2,…, columnn_Name_n
FROM TABLE_NAME
[WHERE condition];

We recognized two Select queries with the Except operator in SQLite, as you can see above. In this case, the SQLite Except operator produces entries of the first SELECT query that do not occur in the second Select query. We specified some of the attributes that are used in the Except operator syntax above:

Column: You want to compare the columns or operations between both the SELECT statements. For each of the Queries, the fields do not have to be the same, but the related columns must be of similar data types.

Table_Name: The Table_Name out of which you would like to retrieve records. The FROM clause must mention at least one table.

WHERE condition: The Where condition is Optional here. We used the Where condition for the criteria that should be required for records to be chosen.

Create Tables for the Except clause:

We should first construct tables in the database before we can use the Except clause. We have created a table “Teacher” and set its fields as mentioned in the figure.

The insert query is used in the following figure to insert the record in the table “Teacher”.

From the SELECT query, we can fetch the table record as follows:

As we have created the table “Teacher”. Now, we are required to make another table for the except clause. For this, we have created the second table in the existing database as “Student”. The fields are set for the table “Student” as mentioned below.

Then, we have inserted the entries for the table “Student” by utilizing the insert query as follows:

You can see the record in the ‘’Student” table in the following prompt shell.

Example 1: Program of using SQLite Except clause with the single expression:

For that basic SQLite query, we will examine how to use the SQLite Except clause in Select statements with a single column. Within our SQLite shell, we provided our first SELECT expression that included the field ID from the table “Student” and then we used the Except clause. After that, the second SELECT expression is utilized to select the same field ID but from the table “Teacher”. This example of the EXCEPT clause retrieves all ID entries from the table Student but not from the table Teacher. This means that if an ID value was present in both the Student and Teacher tables, the ID value would not appear in the EXCEPT query data.

As the ID “6” is a unique ID found in the table Student so it is displayed in the output here:

SELECT ID
FROM Student
EXCEPT
SELECT ID
FROM Teacher;

Example 2: Program of using SQLite Except clause with the multiple expression:

Next, let’s examine a case of an SQLite EXCEPT query that returns multiple columns. Here, we have two SELECT expressions with the WHERE condition. We have taken the fields ID, St_NAME, SUBJECT, and CGPA from the table student. Then, we applied a WHERE condition that only displays the record whose CGPA is greater than and equal to “3.0”. We have a second SELECT expression which selects the column ID, T_NAME, T_AGE, and SUBJECT from the table Teacher. The second SELECT expression uses the WHERE condition that fetched the record from the table Teacher where the SUBJECT is equal to Computer. In between these two SELECT expressions, we have an EXCEPT clause that returns only the record of the table Student with the specified field values but not the field values matched with the Teacher table.

SELECT ID, St_NAME, SUBJECT , CGPA
FROM Student
WHERE CGPA >= 3.0
EXCEPT
SELECT ID, T_NAME, T_AGE ,SUBJECT
FROM Teacher
WHERE SUBJECT = ‘Computer’;

The output from the Except query is as follows:

Example 3: Program of using SQLite Except for clause with ORDER BY clause:

Here, we have used the Except clause along with the order by descending order. First, we have defined two SELECT expressions with the different fields from the table Student and Teacher. These SELECT expressions are called the except clause in between them. As the field names in the two SELECT expressions differ, it’s preferable to refer to the fields in the ORDER BY clause by their result set position.

SELECT ID, St_NAME, SUBJECT
FROM Student
WHERE SUBJECT = ‘Science’
EXCEPT
SELECT ID, T_NAME, SUBJECT
FROM Teacher
WHERE ID < 4
ORDER BY 1 DESC;

We have successfully retrieved the record from Except query in descending order as follows:

Conclusion:

This is how we may utilize the SQLite Except clause to retrieve data from Select statements that match our conditions. We have provided a quick introduction with a pictorial representation in this post. Also, described the syntax of except clause and used this syntax in the example to show the results. We can use the except clause for the single column and multiple columns. The order by clause is also used with the except clause for the sorted results.



Source link


“In this article, we will be discussing the concept of operator overloading in the C# programming language. The method for making a común operator do other operations other than its traditional operation is the basic meaning of operator overloading. C# allows users to predefine some operators that are overloadable as there are two types of operators when it comes to overloading that is Overloadable Operators and Non-overloadable Operators. The unary operators and the binary operators are the overloadable operators in the C# programming language. While the rest of the operators are non-overloadable operators, some of them are logical operators, compound operators, and cast operators.”

Operator Overloading

To understand the concept of overloading an operator, we are going to implement the most basic and commonly overloaded operator that we use, the “+” operator. Normally the “+” operator is used to add two numbers. But if we write it between a string and a number, it will concatenate both of them. This is a fine example of overloading a unary operator.

Text Description automatically generated

In the above C# program, we demonstrated a simple function in which we use the “+” operator to add 2 numbers in the first line, and then in the second line, we use the same operator to add a string and a number together this word result in the concatenation of the string and a number as we are overloading the operator to do so.

A screenshot of a computer Description automatically generated with medium confidence

The output of the above program showcases the dual nature of the “+” operator; in the first operation, it is simply adding two numbers for the result as 4, but in the other operation, the “+” operator is acting as an agent of concatenating a string to a number as it is pre-defined in the C# programming language.

Now we will look at some examples regarding the overloading of an operator in which we will use the “operator” keyword to state it as the overloading of a certain operator.

Example 01: Overloading the “-” Operator in Ubuntu 20.04

In this example, we will be focusing on the extensive overloading of an operator through the inheritance of the overloading methods. In this case, the “-” operator will be overloaded. The value of an integer variable will be allocated to the “-” operator; however, the typical operation of the “-” will be altered by overloading. We will use algebra to translate the “-” operator into a sign that can be given to a number.

Text Description automatically generated

In the above example, we have two integer variables and substituted their values other two integers. Then we created the overloading function with the help of the “operator” keyword in which the conventional operation of the “-” operator was redefined. Then we called the overloaded operator in the main program to see the difference between the común and overloaded operator. After compiling and executing the above program, we will get the following result:

Text Description automatically generated

As we can see that the operator is now overloaded, and the predefined function from the overloaded method is carried out on the last call.

Example 02: Overloading the “+” Operator in Ubuntu 20.04

As we discussed earlier, the “+” operator is overloaded and can perform multiple operations, but it can still be overloaded through methods and inheritance of class objects. In this example, we will be overloading the “+” operator in which we will be making the “+” operator into adding two objects of a class rather than numbers which is the predefined function of the “+” operator. The “+” operator will be overloaded by using the “operator” keyword method.

In this C# program, we will be creating a method for overloading the “+” operator. First, we will create a class and call its object in the overloading method. These objects will be used in defining the functionality of the “+” operator, which is adding two objects to a class with the help of the “+” operator. Then the Display method will be created, which will display the “num” object. In the main program, we will create new objects and assign values to the classes and then use the overloaded “+” operator to add two objects to each other. After compiling the above program, we will get the output as shown below:

Text Description automatically generated

The object values of the class are shown in the output, which are “5” and “10,” and the sum of these objects is “15,” indicating that we have now overloaded the “+” operator to add two objects of the class and sum them for a result, much like adding two integers ordinarily.

Example 03: Overloading the “*” Operator in Ubuntu 20.04

The method for overloading the “*” operator will be discussed in this example. The operator keyword will be used to provide an overloading method for the “*” operator. The “*” operator’s fundamental job is to multiply two integers, but in this example, we’ll overload it by defining it to multiply two objects of a class and treat them as two numbers.

Graphical user interface, text, application Description automatically generated

We have created a public method in which we have assigned an integer to an object. Then we created a public method for overloading the “*” operator by using the operator keyword before the symbol and the parameters. The parameters of this function are the objects of the previous class; hence it is inheriting the previous class entity, which is then being multiplied by each other in a new object, and this object will be returned from this method. In the main function, we will create three new objects and assign two of them some values in the parameter. Then these objects will be multiplied using the overloaded operator, and the result will be displayed on the how output screen. After successfully compiling and executing the above program, you will get the following result, as shown in the snippet:

Text Description automatically generated

As we can see that the first two numbers are displayed using the display function, which returns the objects of the Exam class. Then we have the resultant value of these two objects’ multiplication which is “50,” although the “*” operator only takes the product of two numbers due to our overloading, the operator successfully multiplied two objects as well.

Example 04: Overloading the Equality Operator in Ubuntu 20.04

Now we will be overloading the equality operator in the C# programming language. In this example, the equality operator will be used to compare objects of a class rather than its común function for assigning values to variables. The object’s values will be the same in this scenario, but to differentiate, we will use a different reference and see the difference in the result of this operation.

Text Description automatically generated

We will build a class “Exam” in the aforementioned C# program and initialize certain variables in it, which we will use in the intrinsic function “Display(),”. Following that, we will give equivalent numbers to two objects of the “Exam” class and compare them to each other since the third item was referred from the second, resulting in an equal result. On the Ubuntu 20.04 command line terminal, we will test this feature by running the aforementioned application.

Text Description automatically generated

As we can see that the first comparison was not a success, but the second one was because they had the same reference.

Conclusion

In this article, we have discussed the basic concept of Operator Overloading. The C# programming language allows the operator loading concept and even has already adapted this in regard to the “+” operator, which adds numbers and concatenates strings as well. The overloadable operators were discussed and implemented in the Ubuntu 20.04 environment to have a better understanding of how overloading methods are created.



Source link


PowerShell offers a variety of comparison operators that are used to achieve a couple of functionalities such as matching some particular patterns and comparing different values. There are different categories of the comparison operators such as equality, replacement, matching, etc. If we talk about the like operator it falls in the category of match operator.

In this post, we will explore the different aspects of the “like” operator in PowerShell “Where-object” as listed below:

  • What is a “like” Operator in PowerShell?
  • Basic Syntax
  • How to use a “like” Operator in PowerShell Where-Object?

So let’s begin!

What is a “like” Operator in PowerShell?

-like, -match, -notlike, -notmatch, all these operators belong to the same category i.e. “Matching”. These operators are used to find out whether a specific string matches the wildcard/regex pattern or not. If we talk about the “like” Operator, it is specifically used to match a wildcard pattern within a string.

Basic Syntax

The below snippet shows how to use the like operator in PowerShell:

The like operator will return true if a match is found in the string, and false if the matching value is not found in the targeted string.

How to use a “like” Operator in PowerShell Where-Object?

This section will explore how to use the -like operator in PowerShell, to do that, it will consider a couple of examples.

‘Welcome to linuxhint.com’ -like ‘*welcome*’

In the above-given piece of code, we were looking for the word “welcome” in the string, and to do that we utilized the -like operator along with the wildcard “*”. The output for the above cmdlet will go like this:

The -like operator returned true, it verified that the match “welcome” found in the specified string.

Let’s modify the cmdlet a little bit to check what will happen if didn’t utilize the wildcard operator:

‘Welcome to linuxhint.com’ -like ‘welcome’

When we run the above cmdlet, we get the following result:

What went wrong this time? Why do we get a false value?

In the first example, we utilized the wildcards which say that there can be some text on either side of the targeted word i.e. “welcome”. While the string in which we didn’t utilize the wildcards will look for the exact match (nothing more, nothing less). This means it will return true only if it finds the exact word i.e. there is not a single character on either side of the targeted word as shown in the below snippet.

Let’s run the below cmdlet on Windows PowerShell:

‘welcome’ -like ‘welcome’

The output shows that this time the like operator returns true.

Conclusion

In PowerShell, the “like” Operator belongs to the category of matching operators and is specifically used to match a wildcard pattern within a string. The like operator will return true if a match is found in the string, and false if the matching value is not found in the specified string. This post explained how to use the like operator in PowerShell Where-Object with the help of some suitable examples.



Source link


There are several types of operators are present in C languages. With these operators, we can manipulate different types of operands or data with different procedure. Arithmetic operator is one of the operators by which we can operate different types of mathematical values. First, we have to see that what the position of arithmetic operator in operator groups is.

Operator Groups:

  • Unary operators
  • Arithmetic operators
  • Bitwise operators
  • Relational operators
  • Logical operators
  • Conditional operators
  • Assignment operators

One kind of proceeding rules, if in a problem there are multiple operator present, then this type of problem is solved according to this order of operator groups. This means:

Unary -> Arithmetic -> Bitwise -> So on.

Arithmetic Instruction:

Arithmetic Instruction is an Instruction which is used to manipulate data using operators.

Example:

1

3    +     4     *    5

One result may be 23 another may be 35.

A result is Right, another is wrong. We use operator proceeding BODMAS. But in C language, there is no rule of BODMAS.

Arithmetic operators:

There are different types of arithmetic operators are present in C language. The main operators are division (/), multiplication (*), addition (+) and subtraction (-). Their priorities are as follows:

*, /, % (same priority) 1st priority

+, – (same priority) 2nd priority

Associtivity rule is from Left to Right.

For example:    3 + 4 – 7 = 7 – 7 = 0.

Here two operators are used. One is addition operator and another is subtraction operator. As both operators belong to the same priority level, so preceding rules are followed from left to right. For this addition operator executes first then subtraction operator executes next.

Programming Example 1:

Here we see an example of arithmetic expression. How divide operator is executed in the expression?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <stdio.h>

int main ()

{

    int  x ;            // declaring a variable.
    x= 3 / 4 ;      // arithmetic expression.
    printf ( «%d», x ) ;

return 0 ;

}

Output:

Explanation:

In this programming example the given arithmetic expression is x= 3 / 4;

In C language, if we perform any operation between two integers, result comes as an integer.  Here 3 and 4 both are integers, so the result has not come up with any actual number. So, it cannot come and float number.

So, 11/5= 2, then result will come 2. If x =3.0/4, the result will come 0.75. It means, if a data type will be actual, then result will come in float.

So,

1
2
3
4
5
6
7

3/4 = 0;

3.0/4 = 0.75;

3/4.0 = 0.75;

3.0/4.0 = 0.75;

It means integer will come if and only if both are integer. Otherwise, it will come any actual number.

% operator gives the result of Remainder.

1
2
3
4
5

x = 23%4 = 3

x= 25%5 = 0

x= 3%4 = 3

If we want to divide any number to another number, means it is divisible or not, then use only modulo (%) operator.

Programming Example 2:

Here we see an example of arithmetic expression. How addition operator is executed in the expression?

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <stdio.h>

int main ()

{

    int  x  ;
    x = 3 + 4 ;
    printf ( «%d n « , x ) ;
    return 0 ;

}

Output:

Explanation:

In this programming example the given expression is x = 3 + 4;

Here two operators are used. One is addition operator and another is assignment operator. As addition operator has the higher priority than assignment operator, addition operator executes first, then assignment operator executes. So the addition of 3 and 4 is 7, then 7 is assigned in the variable x with the help of assignment operator.

Programming Example 3:

Here we see an example of arithmetic expression or how subtraction operator is executed in the expression:

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <stdio.h>

int main ()

{

    int  x ;                // declaring a variable.
    x = 34 ;           // arithmetic expression is used.
    printf ( » %d n «, x ) ;
    return 0 ;

}

Output:

Explanation:

In this programming example the given expression is x = 3 – 4;

Here two operators are used. One is subtraction operator and another is assignment operator. As subtraction operator has the higher priority than assignment operator, subtraction operator executes first, then assignment operator executes. So the subtraction of 3 and 4 is -1, then -1 is assigned in the variable x with the help of assignment operator.

Programming Example 4:

Here we see an example of arithmetic expression. How multiplication operator is executed in the expression?

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <stdio.h>

int main ()

{

    int  x ;            // declaring a variable.
    x = 3 * 4 ;     // arithmetic expression is used.
    printf ( » %d n «, x ) ;
    return 0 ;

}

Output:

Explanation:

In this programming example the given expression is x = 3 * 4;

Here two operators are used. One is multiplication operator and another is assignment operator. As multiplication operator has the higher priority than assignment operator, multiplication operator executes first, then assignment operator executes. So the multiplication of 3 and 4 is 12, then 12 is assigned in the variable x with the help of assignment operator.

Programming Example 5:

Here we see an example of arithmetic expression. How different arithmetic operators are executed in the expression?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <stdio.h>

int main ()

{

    int  x= 0 ;
    x = 34 + 2115 *3 / 4 ;
    printf ( » The value of expression is: %d n «, x ) ;

return 0 ;

}

Output:

Explanation:

In this programming example the given expression is x = 34 + 21 – 15 *3 / 4;

Here all arithmetic operators are used.  As the addition, subtraction, division and multiplication operators are used in the given expression, higher priority operator executes first, and then other operator executes. So, multiplication and division operator execute first. After that addition and subtraction operators execute as they belong to the less priority level.

Conclusion:

From the above discussion of arithmetic operator in operator groups, we have come to this conclusion that arithmetic operator is one of the most important operator to operator different types of mathematical data. Through arithmetic operator we can easily solve different types of mathematical operation.



Source link