C-and-C++/C2/Increment-And-Decrement-Operators/English-timed
From Script | Spoken-Tutorial
Revision as of 15:33, 27 November 2013 by PoojaMoolya (Talk | contribs)
| Time' | Narration
|
| 00.01 | Welcome to the spoken tutorial on Increment and Decrement Operators in C and C++. |
| 00.08 | In this tutorial, we will learn about: |
| 00.10 | Increment and decrement operators |
| 00.12 | |
| 00.18 | |
| 00.22 | - - . a- - is a postfix decrement operator.
|
| 00.27 | - -a is a prefix decrement operator. |
| 00.31 | We will also learn about Type casting. |
| 00.35 | To record this tutorial, I am using: Ubuntu 11.10 as the operating system |
| 00.40 | gcc and g++ Compiler version 4.6.1 in Ubuntu. |
| 00.48 | The ++ operator increases the existing value of the operand by one.
|
| 00.54 | a++ and ++a are equivalent to a = a + 1. |
| 01.00 | The -- operator decreases the existing value of the operand by one. |
| 01.06 | a-- and --a are equivalent to a = a - 1. |
| 01.13 | I will now demonstrate the use of increment and decrement operators with the help of a C program. |
| 01.19 | I have already made the program, so I'll explain the code. |
| 01.25 | 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.35 | This way we will be able to observe the changes in the value of a. |
| 01.39 | It will thus give us a better idea about the working of the operators. |
| 01.47 | Let's see how the postfix increment operator works. |
| 01.51 | The output of this printf statement is 1. |
| 01.55 | The value wont change. |
| 01.57 | This is because the postfix operation occurs after the operand is evaluated. |
| 02.04 | If an operation is performed on a++, it is performed on the current value of a. |
| 02.10 | After that the value of a is incremented. |
| 02.17 | Now if we see the value of a here, it has been incremented by 1. |
| 02.27 | We again initialize a to 1 so as to reflect on the changes. |
| 02.35 | We now come to the prefix increment operators |
| 02.38 | This printf statement prints 2 on the screen. |
| 02.42 | This is because a prefix operation occurs before the operand is evaluated. |
| 02.49 | So the value of a is first incremented by 1 and then it is printed. |
| 02.58 | We again print a's value to see that there are no further changes. |
| 03.03 | Now lets check by executing this code. |
| 03.07 | I will comment out the following lines. Type /*, */ |
| 03.19 | Click on Save. |
| 03.22 | I have saved my file as incrdecr.c. |
| 03.29 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously. |
| 03.35 | To compile, type the following on the terminal gcc space incrdecr dot c space minus o space incr. Press enter. |
| 03.51 | To execute the code, type ./incr. Press enter. |
| 03.59 | The output is displayed on the screen, |
| 04.01 | This is the output when you print a++ |
| 04.06 | This is the output when you print ++a. |
| 04.09 | We can see that the result is as discussed before.
|
| 04.13 | Now Coming back to the rest of the program. |
| 04.16 | I will now explain the postfix and prefix decrement operators. |
| 04.21 | Remove the multiline comments from here and here .
|
| 04.29 | We now again assign the value of 1 to a. |
| 04.35 | This printf statement outputs the value of 1 as explained previously. |
| 04.40 | A's value will be decremented after a-- is evaluated as its a postfix expression.
|
| 04.47 | The next statement prints a's value as o. |
| 04.51 | A's value has now being decremeted by 1. |
| 04.54 | We now have the prefix decrement operator. |
| 04.58 | Output of this printf statement would be 0. |
| 05.00 | As it is a prefix operation. |
| 05.05 | The prefix operation occurs before the operand is evaluated. |
| 05.09 | This printf statements output is 0. |
| 05.11 | No further changes have being made to a's value. |
| 05.15 | Type return 0; And close the ending curly bracket |
| 05.21 | Click on Save. |
| 05.24 | Switch back to the terminal. |
| 05.27 | To compile type the following on the terminal; gcc space incrdecr dot c space minus o space incr. Press Enter. |
| 05.42 | Execute type, ./incr.Press Enter. |
| 05.52 | This is the output when you print a--
|
| 05.56 | This is the output when you print --a
|
| 05.59 | So, now we see how the increment and decrement operator works. |
| 06.05 | If we want to write the same program in C++. |
| 06.07 | I can make a few changes to the above C code. |
| 06.10 | Let me go back to the editor. |
| 06.13 | Here is the C++ file with the necessary code.
|
| 06.16 | Notice that the header is different from the C file header. |
| 06.20 | We have the using namespace statement also. |
| 06.24 | Also, note that the output statement in C++ is cout. |
| 06.28 | So, apart from these differences, the two codes are very similar. |
| 06.33 | Save the file. The file is saved with an extension .cpp |
| 06.40 | Let's compile the code. |
| 06.42 | Open the terminal and type g++ space incrdecr dot cpp space minus o space incr. Press Enter. |
| 07.00 | Execute Type ./ incr.Press Enter. |
| 07.07 | The output is displayed on the screen:
|
| 07.10 | So, we see the output is identical to the C program. |
| 07.15 | We now have the concept of typecasting. |
| 07.17 | It is implemented the same way in both C and C++. |
| 07.22 | Typecasting is a used to make a variable of one type, act like another type. |
| 07.27 | Typecasting is done by enclosing the data type you want within parenthesis. |
| 07.33 | This cast is put in front of the variable you want to cast. |
| 07.38 | This typecast is valid for one single operation only. |
| 07.42 | Now a will behave as a float variable for a single operation. |
| 07.47 | Here is an example that I have already created. |
| 07.50 | I shall now explain the code. |
| 07.54 | We first declare variables a and b as integer and c as float. |
| 08.00 | a is assigned the value of 5. b is assigned the value of 2. |
| 08.06 | We will perform operations on a and b. |
| 08.10 | We divide a by b. The result of division is stored in c. |
| 08.14 | We have used %.2f to denote a precision of 2 decimal places. |
| 08.20 | The result displayed will be 2.00 against the expected result of 2.50. |
| 08.25 | The fractional part has been truncated as both the operands a and b are integers. |
| 08.31 | To perform real division one of the operands will have to be type cast to float. |
| 08.35 | Here we are typecasting a to float. c now holds the value of real division. |
| 08.41 | Now the result of real division is displayed. The answer is 2.50 as expected. |
| 08.47 | Type return 0; and close the ending curly bracket. |
| 08.51 | Click on Save. Save the file with .c extension. |
| 08.55 | I have saved my file as typecast.c. |
| 08.59 | Open the terminal. |
| 09.01 | To compile, type gcc space typecast dot c space minus o space type.Press Enter. |
| 09.17 | to execute, type ./type.Press Enter. |
| 09.25 | The output is displayed on the screen.
|
| 09.27 | Looking at the two values we see the effects of typecasting. |
| 09.32 | We will summarize the tutorial now. |
| 09.34 | In this tutorial we learnt, |
| 09.36 | How to use the increment and decrement operators. |
| 09.40 | We learn't about the forms, Postfix and Prefix
|
| 09.44 | Also we learnt about typecasting and how it is used. |
| 09.47 | As an assignment: |
| 09.49 | Write a program to solve the following expression, a divided by b plus c divided by d |
| 09.56 | The values of a, b, c and d are taken as input from the user. |
| 10.01 | Use typecasting to perform real division. |
| 10.05 | Watch the video available at the following link |
| 10.08 | It summarises the Spoken Tutorial project |
| 10.10 | If you do not have good bandwidth, you can download and watch it
|
| 10.15 | The Spoken Tutorial Project Team |
| 10.17 | Conducts workshops using spoken tutorials |
| 10.20 | Gives certificates for those who pass an online test |
| 10.24 | For more details, please write to contact at spoken hyphen tutorial dot org |
| 10.33 | Spoken Tutorial Project is a part of the Talk to a Teacher project |
| 10.37 | It is supported by the National Mission on Education through ICT, MHRD, Government of India |
| 10.44 | More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro |
| 10.55 | This is Ritwik Joshi from IIT Bombay.
Thank you for joining. |
Contributors and Content Editors
Ashwini, Devisenan, Jyotisolanki, Krupali, PoojaMoolya, Pratik kamble, Sandhya.np14