What is an Operator?

Operator :
An operator is a symbol that performs a certain operation on a variable or expression. So far, we have used the = sign, which calls an assignment operator that assigns a value or expression from the right-hand side of the equals sign to a variable on the left-hand side.
The simplest form of other kinds of operators is an arithmetic operator such as +, -, *, /, and %. These operators operate on a variable such as int and float. 

Let's look at some of the use cases of these operators:

#include <iostream>
#include <conio.h>  
// Program prints out value of a + b and x + y to screen
 int main()
 {    
int a = 8;   int b = 12;
std::cout << "Value of a + b is : " << a + b << std::endl;   
float x = 7.345f;   float y = 12.8354;
std::cout << "Value of x + y is : " << x + y << std::endl; 
getch(); 
return 0;
}

The output of this is as follows:  
                                                                                                                                      
Value of a+b is : 20                                                                                                                                Value of x+y is :20.1804

Let's look at examples for other operations as well:

#include <iostream>   
#include <conio.h>
// Program prints out values to screen

int main() 
  {    
int a = 36;   int b = 5;   
std::cout <<"Value of a + b is :" <<a+b<<std::endl; 
std::cout <<"Value of a - b is : " << a - b << std::endl;             std::cout <<"Value of a * b is : " << a * b <<std::endl;            std::cout <<"Value of a / b is : " << a / b << std::endl;            std::cout <<"Value of a % b is : " << a % b <<std::endl;              getch();  
return 0;
}                                                                                                                                                                

The output is as shown as follows                                                                                                             
value of a+b is:41
value of a-b is:31
value of a*b is:180
value of a/b is:7
value of a%b is:1

+, -, *, and / are self- explanatory. However, there is one more arithmetic operator: %, which is called the modulo operator. It returns the remainder of a division. How many times is 5 contained in 36? 7 times with a remainder of 1. That's why the result is 1. Apart from the arithmetic operator, we also have an increment/decrement operator.

In programming, we increment variables often. You can do a=a+1; to increment and a=a-1; to decrement a variable value. Alternatively, you can even do a+=1 and a-=1; to increment and decrement, but in C++ programming there is an even shorter way of doing that, which is by using the ++ and -- signs to increment and decrement the value of a variable by 1.

#include <iostream>
#include <conio.h>
// Program prints out values to screen
int main()
{
int a = 36;   int b = 5;
std::cout << "Value of ++a is : " << ++a <<std::endl;
std::cout << "Value of --b is : " << --b <<std::endl;                
std::cout << "Value of a is : " << a <<std::endl;  
std::cout << "Value of b is : " << b << std::endl; 
getch();
return 0;
}


The output of this is as follows:

Value of ++a is :  37
Value of –b   is  :   4
Value of a     is  :  37
Value of b     is  :   4
Consequently,  the ++ or -- operator increments the value permanently. If the ++ is to the left of the variable, it is called a pre-increment operator. If it is put afterward, it is called a post-increment operator. There is a slight difference between the two

 If we put the ++ on the other side, we get the following output:

Value of a++is :  36
Value of b --is:   5
Value of a    is:   37
Value of b   is  :  4

In this case, a and b are incremented and decremented in the next line. So, when you print the values, it prints out the correct result. It doesn't make a difference here, as it is a simple example, but overall it does make a difference and it is good to understand this difference. In this book, we will mostly be using post-increment operators. In fact, this is how C++ got its name; it is an increment of C. 


                                      Logical operator


                                                Comparison operator
  

  What are the Statements in C/C++ language?

A program may not always be linear. Depending on your requirements, you might have to branch out or bifurcate, repeat a set of code, or make a decision. For this, there are conditional statements and loops. In a conditional statement, you will check whether a condition is true. If it is, you will go ahead and execute the statement. The first of the conditional statements is the if statement.
The syntax for this looks as follows:

If (condition) statement;

Let's look at how to use this in code in the following code. Let's use one of the comparison operators here:

#include<iostream>                                                    #include <conio.h>                                                     // Program prints out values to screen 

int main() 
 {
int a = 36; 
int b = 5;

if (a > b)
std::cout << a << " is greater than " << b << std::endl;
getch();  
return 0; 
}

The output is as follows:  36 is greater than 5

We check the condition if a is greater than b, and if the condition is true, then we print out the statement. we have the if...else statement, which is a statement that basically executes the alternate statement.                                                                                                                                                            
The syntax looks like this:
  
if (condition) statement1; else statement2

Let's look at it in code: 
                                                 
#include <iostream> 
#include <conio.h>
// Program prints out values to screen
int main()
{
int a = 2;   int b = 28;                                              if (a >b) 
std::cout << a << " is greater than "<< b<< std::endl;
else
std::cout << b << " is greater than "<< a<< std::endl;
getch();
return 0;
 


Here, the values of a and b are changed so that b is greater than a:28 is greater than 2. One thing to note is that after the if and else conditions, C++ will execute a single line of the statement. If there are multiple statements after the if or else, then the statements need to be in curly brackets, as shown:

if (a > b) 
{  
std::cout << a << " is greater than "<<b<<std::endl;    
 }         
else
 {  
std::cout << b << " is greater than"<<a<<std::endl;     
 }


You can also have if statements after using else if:

#include <iostream> 
#include <conio.h>
// Program prints out values to screen                                       int main() {  
 int a = 28;   int b =28;                                                     if (a > b)
{ std::cout << a << " is greater than " << b << std::endl; 
}                                                                          else   
if (a ==b)                                                                  { 
std::cout << a << " is equal to " << b << std::endl; 
 }   
else   
   {  
std::cout << b << " is greater than " << a <<std::endl;                      }
 getch();                                                                    return 0;
}              

OUTPUT
28 is equal to 28
           


 What is the Iteration in C/C++?
     
Iteration is the process of calling the same statement repeatedly. C++ has three iteration statements: the while, do...while, and for statements. Iteration is also commonly referred to as loops.  
The while loop syntax looks like the following:  

while (condition) statement; 
Let's look at it in action:

#include <iostream> 
#include <conio.h> 
// The program prints out values to screen  
 int main()   
int a = 10;   int n = 0; 
 while (n < a) 
 {    
std::cout << "value of n is: " << n << std::endl;
 n++; 
 getch();           
return 0; 
 }                                                                                                                                                                                                        

  Here is the output of this code:

value of n is :0
value of n is :1
value of n is :2
value of n is :3
value of n is :4
value of n is :5
value of n is :6
value of n is :7
value of n is :8
value of n is :9

Here, the value of n is printed to the console until the condition is met. The do-while statement is almost the same as a while statement except, in this case, the statement is executed first and then the condition is tested

The syntax is as follows:

do statement while (condition);

You can give it a go yourself and check the result.
The loop that is most commonly used in programming is the for loop. 

The syntax for this looks as follows

for (initialization; continuing condition; update) statement;

The for loop is very self-contained. In while loops, we have to initialize n outside the while loop, but in the for loop, the initialization is done in the declaration of the for loop itself.
Here is the same example as the while loop but with the for loop:

#include <iostream> 
#include <conio.h> 
// Program prints out values to screen
int main()
{
for (int n = 0; n < 10; n++) 
std::cout << "value of n is: " << n << std::endl; 
 getch();
return 0; 
}

The output is the same as the while loop but look how compact the code is compared to the while loop. Also, the n is scoped locally to the for loop body.  
We can also increment n by 2 instead of 1, as shown:

#include<iostream>                                                  #include <conio.h>
// Program prints out values to screen
int main()
{
for (int n = 0; n < 10; n+=2) 
std::cout << "value of n is: " << n << std::endl;
getch();                                                                  
return 0;   
}

Here is the output of this code:       

The value of n is :  0
The value of n is :  2
The value of n is :  4
The value of n is :  6
The value of n is :  8                                                                                                                                                                               

                                                                                        

                                                                                                                                                                                                                                           


`

Post a Comment

Previous Post Next Post