C-and-C++/C2/Increment-And-Decrement-Operators/English-timed

From Script | Spoken-Tutorial
Revision as of 12:24, 3 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time' Narration


00.02 Welcome to the spoken tutorial on Increment and Decrement Operators in C and C++.
00.09 In this tutorial, we will learn about:
00.11 Increment and decrement operators

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

++a which is prefix increment operator.

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

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

00.36 To record this tutorial, I am using: Ubuntu 11.10 as the operating system
00.41 gcc and g++ Compiler version 4.6.1 in Ubuntu.
00.49 The ++ operator increases the existing value of the operand by one.
00.54 a++ and ++a are equivalent to a = a + 1.
01.01 The -- operator decreases the existing value of the operand by one.
01.07 a-- and --a are equivalent to a = a - 1.
01.14 I will now demonstrate the use of increment and decrement operators with the help of a C program.
01.20 I have already made the program, so I'll explain the code.
01.26 Here, we have the code for increment and decrement operators in C.
01.30 Here, I have taken an integer variable a that holds the value 1.
01.36 This way we will be able to observe the changes in the value of a.
01.40 It will thus give us a better idea about the working of the operators.
01.48 Let's see how the postfix increment operator works.
01.52 The output of this printf statement is 1.
01.56 The value will change.
01.58 This is because the postfix operation occurs after the operand is evaluated.
02.05 If an operation is performed on a++, it is performed on the current value of a.
02.11 After that the value of a is incremented.
02.18 Now if we see the value of a here, it has been incremented by 1.
02.28 We again initialize a to 1 so as to reflect on the changes.
02.36 We now come to the prefix increment operators
02.39 This printf statement prints 2 on the screen.
02.43 This is because a prefix operation occurs before the operand is evaluated.
02.50 So the value of a is first incremented by 1 and then it is printed.
02.59 We again print a's value to see that there are no further changes.
03.04 Now lets check by executing this code.
03.08 I will comment out the following lines. Type /* ,*/
03.19 Click on Save.
03.23 I have saved my file as incrdecr.c.
03.30 Open the terminal window by pressing Ctrl, Alt and T keys simultaneously.
03.36 To compile, type the following on the terminal gcc incrdecr.c -o incr. Press enter.
03.52 To execute the code, type ./incr'. Press enter.
03.59 The output is displayed on the screen,
04.02 This is the output when you print a++


04.06 This is the output when you print ++a.
04.10 We can see that the result is as discussed before.


04.13 Now Coming back to the rest of the code.
04.17 I will now explain the postfix and prefix decrement operators.
04.22 remove the multiline comment from here and here .


04.29 We now again assign the value of 1 to a.
04.36 This printf statement outputs the value of 1 as explained previously.
04.42 A's value will be decremented after a-- is evaluated as its a postfix expression.
04.51 This printf statements output the value of 1 as explained previously


04.57 A's value is now decremented by 1 after a-- is evaluated .As its a post fix expression
05.03 The next statement prints a's value as o.
05.07 A's value has now decremeted being1.
05.10 We now have the prefix decrement operator.
05.14 Output of this printf statement would be 0.
05.17 As it is a prefix operation.
05.21 The prefix operation occurs before the operand is evaluated.
05.25 This printf statements output is 0.
05.28 No further changes have being made to a's value.
05.31 Type return 0; And close the ending curly bracket
05.37 Click on Save.
05.40 Switch back to the terminal.
05.43 To compile type the following on the terminal; gcc incrdecr.c -o incr. Press Enter.
05.58 To execute type, ./incr.Press Enter.


06.08 This is the output when you print a--


06.12 This is the output when you print --a


06.15 So, now we see how the increment and decrement operator works.
06.21 If we want to write the same program in C++.
06.23 I can make a few changes to the above C code.
06.26 Let me go back to the editor.
06.29 Here is the C++ file with the necessary code.


06.33 Notice that the header is different from the C file header.
06.37 We have the using namespace statement here.
06.40 Also, note that the output statement in C++ is cout.
06.45 So, apart from these differences, the two codes are very similar.
06.49 Save the file. The file is saved with an extension .cpp
06.56 Let's compile the code.
06.58 Open the terminal and type g++ incrdecr.cpp -o incr. Press Enter.
07.16 To execute Type ./ incr.Press Enter.
07.23 The output is displayed on the screen:


07.27 So, we see the output is identical to the C program.
07.31 We now have the concept of typecasting.
07.33 It is implemented the same way in both C and C++.
07.38 Typecasting is a used to make a variable of one type, act like another type.
07.43 Typecasting is done by enclosing the data type you want within parenthesis.
07.49 This cast is put in front of the variable you want to cast.
07.54 This typecast is valid for one single operation only.
07.58 Now a will behave as a float variable for a single operation.
08.03 Here is an example I have already created.
08.06 I shall now explain the code.
08.11 We first declare the variables a and b as integer and c as float.
08.16 a is assigned the value 5. b is assigned the value 2.
08.22 We will perform operations on a and b.
08.26 We divide a by b. The result of division is stored in c.
08.30 We have used %.2f to denote a precision of 2 decimal places.
08.35 The result displayed will be 2.00 against the expected result of 2.50.
08.41 The fractional part has been truncated as both the operands a and b are integers.
08.47 To perform real division one of the operands will have to be type cast to float.
08.51 Here we are typecasting a to float. c now holds the value of real division.
08.57 Now the result of real division is displayed. The answer is 2.50 as expected.
09.03 Type return 0; and close the ending curly bracket.
09.07 Click on Save. Save the file with .c extension.
09.11 I have saved my file as typecast.c.
09.15 Open the terminal.
09.17 To compile, type gcc typecast.c -o type.Press Enter.
09.33 to execute, type ./type.Press Enter.
09.41 The output is displayed on the screen.


09.44 looking at the two values we see the effects of typecasting.
09.48 We will summarize the tutorial now.
09.50 In this tutorial we learnt,
09.52 How to use the increment and decrement operators.
09.56 We learn't about the form Postfix and Prefix


10.00 Also we learnt about typecasting and how it is used.
10.04 As an assignment:
10.05 Write a program to solve the following expression, (a\b) + (c\d)
10.12 The values of a, b, c and d are taken as input from the user.
10.17 Use typecasting to perform real division.
10.21 Watch the video available at the following link
10.24 It summarises the Spoken Tutorial project
10.27 If you do not have good bandwidth, you can download and watch it


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


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


11.11 This is Ritwik Joshi from IIT Bombay.

Thank you for joining.