Difference between revisions of "C-and-C++/C2/Increment-And-Decrement-Operators/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 23: Line 23:
 
Increment and decrement operators
 
Increment and decrement operators
  
++ eg. a++ which is postfix increment operator.
+
++ eg. a++ which is a postfix increment operator.
  
++a which is prefix increment operator.
+
++a which is a prefix increment operator.
  
- - eg. a- - which is postfix decrement operator.
+
- - eg. a- - is a postfix decrement operator.
  
- -a which is prefix decrement operator.We will also learn about Type casting.
+
- -a is a prefix decrement operator.
 +
We will also learn about Type casting.
  
 
|-
 
|-
Line 158: Line 159:
  
 
'''gcc space incrdecr.c space -o space incr'''
 
'''gcc space incrdecr.c space -o space incr'''
 +
'''Press Enter'''
  
 
To execute the code, type
 
To execute the code, type
  
 
'''./incr'''
 
'''./incr'''
 +
'''Press Enter'''
  
 
|-
 
|-
Line 170: Line 173:
  
 
This is the output when you print a++
 
This is the output when you print a++
 
+
a's value is now 1
a's value is now 1''' '''
+
  
 
a's value is now 2  
 
a's value is now 2  
Line 178: Line 180:
  
 
We can see that the result is as discussed before.
 
We can see that the result is as discussed before.
 
 
a's value is 2
 
a's value is 2
  
a's value is now 2
+
a's value is now 2  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| y/*
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| y/*
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Now Coming back to the rest of the code.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Now Coming back to the rest of the program.
  
 
I will now explain the postfix and prefix decrement operators.
 
I will now explain the postfix and prefix decrement operators.
  
Remove the multilinecomment from here and here.
+
Remove the multilinecomments from here and here.
  
  
Line 249: Line 250:
  
 
gcc space incrdecr.c space -o space incr
 
gcc space incrdecr.c space -o space incr
 +
'''Press Enter'''
  
 
To execute type,
 
To execute type,
  
 
./incr
 
./incr
 +
'''Press Enter'''
  
 
|-
 
|-
Line 261: Line 264:
  
 
a's value is now 0  
 
a's value is now 0  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed as:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
 
+
a's value is 1
+
 
+
a's value is now 2
+
 
+
a's value is now 2
+
 
+
a's value is now 2
+
  
 
This is the output when you print a--
 
This is the output when you print a--
Line 283: Line 278:
 
a's value is now 0
 
a's value is now 0
  
So, now we see how the '''increment''' and  '''decrement''' operator works.
+
So, now we see how the '''increment''' and  '''decrement''' operators work.
  
 
|-
 
|-
Line 350: Line 345:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Notice that the header is different from the C file header.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Notice that the header is different from the C file header.
  
We have the '''using namespace '''statement here.
+
We have the '''using namespace '''statement also.
  
 
Also, note that the output statement in C++ is '''cout'''.
 
Also, note that the output statement in C++ is '''cout'''.
Line 460: Line 455:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''a''' is assigned the value 5.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''a''' is assigned the value of 5.
  
'''b''' is assigned the value 2.
+
'''b''' is assigned the value of 2.
  
 
We will perform operations on '''a''' and '''b'''.
 
We will perform operations on '''a''' and '''b'''.
Line 532: Line 527:
  
 
'''gcc space typecast.c space -o space type'''
 
'''gcc space typecast.c space -o space type'''
 +
Press '''Enter'''
  
 
to execute, type
 
to execute, type
  
 
'''./type'''
 
'''./type'''
 +
Press '''Enter'''
  
 
|-
 
|-
Line 553: Line 550:
 
In this tutorial we learnt,
 
In this tutorial we learnt,
  
To use the increment and decrement operators.
+
How to use the increment and decrement operators.
 
+
We learnt about the forms
'''Postfix forms: a++, a--'''
+
  
'''Prefix forms: ++a, --a'''
+
'''Postfix and Prefix'''
  
We also learnt about typecasting and how it is used.
+
Also we learnt about typecasting and how it is used.
  
 
|-
 
|-
Line 579: Line 575:
 
'''About the Spoken Tutorial Project'''  
 
'''About the Spoken Tutorial Project'''  
  
* Watch the video available at [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial http://spoken-tutorial.org/What_is_a_Spoken_Tutorial]  
+
* Watch the video available at the following link[http://spoken-tutorial.org/What_is_a_Spoken_Tutorial http://spoken-tutorial.org/What_is_a_Spoken_Tutorial]  
  
 
* It summarises the Spoken Tutorial project  
 
* It summarises the Spoken Tutorial project  
Line 609: Line 605:
 
* Conducts workshops using spoken tutorials  
 
* Conducts workshops using spoken tutorials  
 
* Gives certificates for those who pass an online test  
 
* Gives certificates for those who pass an online test  
* For more details, please write to contact at spoken hyphen tutorial dot org
+
* For more details, please write to contact@spoken hyphen tutorial dot org
  
  

Revision as of 13:56, 30 January 2014

Title of script: Increment and Decrement Operators in C and C++

Author: Ritwik Joshi

Keywords: Increment and Decrement Operators, Type casting, Video Tutorial


Visual Cue
Narration
Slide1 Welcome to the spoken tutorial on Increment and Decrement Operators in C and C++.
Slide 2


In this tutorial, we will learn about:

Increment and decrement operators

++ eg. a++ which is a postfix increment operator.

++a which is a prefix increment operator.

- - eg. a- - is a postfix decrement operator.

- -a is a prefix decrement operator. We will also learn about Type casting.

Slide 3


To record this tutorial, I am using:

Ubuntu 11.10 as the operating system and

gcc and g++ Compiler version 4.6.1 in Ubuntu.

Slide 4 The ++ operator increases the existing value of the operand by one.

a++ and ++a are equivalent to a = a + 1.

The -- operator decreases the existing value of the operand by one.

a-- and --a are equivalent to a = a - 1.

Switch to the file incrdecr.c in gedit I will now demonstrate the use of increment and decrement operators with the help of a C program.

I have already made the program, so I'll explain the code.

Here, we have the code for increment and decrement operators in C.

Highlight


int a=1;


Here, I have taken an integer variable a that holds the value 1.

This way we will be able to observe the changes in the value of a.

It will thus give us a better idea about the working of the operators.

Highlight


printf("a's value is %d\n",

a++);


Let's see how the postfix increment operator works.

The output of this printf statement is 1.

The value won't change.

This is because the postfix operation occurs after the operand is evaluated.

If an operation is performed on a++, it is performed on the current value of a.

After that the value of a is incremented.

printf("a's value is %d\n",

a);

Now if we see the value of a here, it has been incremented by 1.
Highlight

a = 1;

We again initialize a to 1 so as to reflect on the changes.
Highlight

printf(“a's value is now %d\n”,

++a);

We now come to the prefix increment operators

This printf statement prints 2 on the screen.

This is because a prefix operation occurs before the operand is evaluated.

So the value of a is first incremented by 1 and then it is printed.

We again print a's value to see that there are no further changes.

/*

*/

Now lets check by executing this code.

I will comment out the following lines.

Type /*

*/

Click on Save Click on Save.

I have saved my file as incrdecr.c

press Ctrl, Alt and T keys simultaneously. Open the terminal window by pressing Ctrl, Alt and T keys simultaneously.
Type


gcc incrdecr.c -o incr

Type

./incr

To compile, type the following on the terminal

gcc space incrdecr.c space -o space incr Press Enter

To execute the code, type

./incr Press Enter

Highlight

Output

The output is displayed on the screen,

This is the output when you print a++ a's value is now 1

a's value is now 2

This is the output when you print ++a.

We can see that the result is as discussed before. a's value is 2

a's value is now 2

y/* Now Coming back to the rest of the program.

I will now explain the postfix and prefix decrement operators.

Remove the multilinecomments from here and here.


Highlight

a=1

We now again assign the value of 1 to a.
Highlight

printf("a's value is %d\n”,

a--);

This printf statement outputs the value of 1 as explained previously.

A's value will be decremented after a-- is evaluated as its a postfix expression.

Highlight

printf("a's value is now %d\n”, a);

The next statement prints a's value as o.

A's value is now decemented by 1.

Highlight

printf("a's value is now %d\n”, --a);

We now have the prefix decrement operator.

Output of this printf statement will be 0.

As it is a prefix operation.

The prefix operation occurs before the operand is evaluated.

This printf statements output is 0.

No further changes are being made to a's value.

Type return 0;

And close the ending curly bracket

Click on Save.

Switch back to the terminal.

To compile type the following on the terminal;

gcc space incrdecr.c space -o space incr Press Enter

To execute type,

./incr Press Enter

Highlight


a's value is 1

a's value is now 0

This is the output when you print a--

a's value is now 1

a's value is now 0

This is the output when you print --a

a's value is now 0

a's value is now 0

So, now we see how the increment and decrement operators work.

If we want to write the same program in C++.

I can make a few changes to the above C code.

Let me go back to the editor.


//Increment and Decrement Operators

#include <iostream>

using namespace std;

int main()

{

int a=1;

cout <<"a's value is " <<a++ <<"\n";

cout <<"a's value is now " <<a <<"\n";

a=1;

cout <<"a's value is now " <<++a <<"\n";

cout <<"a's value is now " <<a <<"\n";

a=1;

cout <<"a's value is now " <<a-- <<"\n";

cout <<"a's value is now " <<a <<"\n";

a=1;

return 0;

}


Here is a C++ file with the necessary code.



Highlight

#include<iostream>


using namespace std;


cout

Notice that the header is different from the C file header.

We have the using namespace statement also.

Also, note that the output statement in C++ is cout.

So, apart from these differences, the two codes are very similar.

Save the file.

The file is saved with an extension .cpp

Type


g++ incrdecr.cpp -o incr


./ incr

Let's compile the code.

Open the terminal and type

g++ space incrdecr.cpp space -o incr Press Enter

To execute

Type

./incr

Press Enter

Output:


The output is displayed on the screen:

a's value is 1

a's value is now 2

a's value is now 2

a's value is now 2

a's value is now 1

a's value is now 0

a's value is now 0

a's value is now 0

So, we see the output is identical to the C program.

We now have the concept of typecasting.

It is implemented the same way in both C and C++.

Typecasting is a used to make a variable of one type, act like another type.

Slide 5

Highlight

(float)


Highlight

(int) a

Typecasting is done by enclosing the data type you want within parenthesis.

This cast is put in front of the variable you want to cast.

This typecast is valid for one single operation only.

Now a will behave as a float variable for a single operation.

Open incrdecr.c Here is an example I have already created.

I shall now explain the code.

Highlight

int a,b;

float c;

We first declare the variables a and b as integer and c as float.
Highlight

a=5;

b=2;


a is assigned the value of 5.

b is assigned the value of 2.

We will perform operations on a and b.

Highlight

c=a/b;

We divide a by b.

The result of division is stored in c.

Highlight

printf(“Value of c is %.2f\n”, c);

We have used %.2f to denote a precision of 2 decimal places.

The result displayed will be 2.00 against the expected result of 2.50.

The fractional part has been truncated as both the operands a and b are integers.

To perform real division one of the operands will have to be type cast to float.

Highlight

c=(float)a/b;

Here we are typecasting a to float.

c now holds the value of real division.

Highlight

printf(“Value of c is %.2f\n”, c);

Now the result of real division is displayed.

The answer is 2.50 as expected.

Highlight

return 0;

}

Type return 0;

and the close the ending curly bracket.

Click on Save.

Save the file with .c extension.

I have saved my file as typecast.c.

Open the terminal.


gcc typecast.c -o type

./type

Open the terminal.

To compile, type

gcc space typecast.c space -o space type Press Enter

to execute, type

./type Press Enter

Output The output is displayed on the screen.

Value of c is 2.00

Value of c is 2.50

looking at the two values we see the effects of typecasting.

Summary We will summarize the tutorial now.

In this tutorial we learnt,

How to use the increment and decrement operators. We learnt about the forms

Postfix and Prefix

Also we learnt about typecasting and how it is used.

Assignment: As an assignment:

Write a program to solve the following expression,

(a\b) + (c\d)

The values of a, b, c and d are taken as input from the user.

Use typecasting to perform real division.


Slide 6

About the Spoken Tutorial Project

  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


* Watch the video available at the following link
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


Slide 7

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken hyphen tutorial dot org


Slide 8

Acknowledgement

  • Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at
  • http://spoken-tutorial.org/NMEICT-Intro


Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
    More information on this Mission is available at
  • spoken hyphen tutorial dot org slash NMEICT hyphen Intro


Remain on previous slide

No slide for this part

This is Ritwik Joshi from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini