Difference between revisions of "C-and-C++/C2/Logical-Operators/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | || | + | || Time |
− | || | + | || Narration |
|- | |- | ||
− | | 00 | + | | 00:02 |
| Welcome to the spoken tutorial on '''Logical operators in C and C++.''' | | Welcome to the spoken tutorial on '''Logical operators in C and C++.''' | ||
|- | |- | ||
− | | 00 | + | | 00:08 |
|In this tutorial we will learn about:Logical operators like Logical AND eg. expression1 && expression2 | |In this tutorial we will learn about:Logical operators like Logical AND eg. expression1 && expression2 | ||
|- | |- | ||
− | | 00 | + | | 00:16 |
| Logical OR | | Logical OR | ||
Line 18: | Line 18: | ||
|- | |- | ||
− | | 00 | + | | 00:21 |
|Logical NOT | |Logical NOT | ||
Line 24: | Line 24: | ||
|- | |- | ||
− | |00 | + | |00:25 |
| We will do this with the help of examples. | | We will do this with the help of examples. | ||
|- | |- | ||
− | | 00 | + | | 00:28 |
| To record this tutorial, I am using:'''Ubuntu 11.10''' as the operating system | | To record this tutorial, I am using:'''Ubuntu 11.10''' as the operating system | ||
|- | |- | ||
− | |00 | + | |00:33 |
|'''gcc''' and '''g++ Compiler '''version '''4.6.1 on''' '''Ubuntu.''' | |'''gcc''' and '''g++ Compiler '''version '''4.6.1 on''' '''Ubuntu.''' | ||
|- | |- | ||
− | | 00 | + | | 00:39 |
| Let us start with the introduction to the '''logical operators'''. | | Let us start with the introduction to the '''logical operators'''. | ||
|- | |- | ||
− | | 00 | + | | 00:43 |
|In '''C '''and '''C++,''' '''true''' is any value other than 0. | |In '''C '''and '''C++,''' '''true''' is any value other than 0. | ||
|- | |- | ||
− | | 00 | + | | 00:48 |
|non zero means true | |non zero means true | ||
|- | |- | ||
− | | 00 | + | | 00:50 |
|And zero means false | |And zero means false | ||
|- | |- | ||
− | | 00 | + | | 00:53 |
|Expressions using logical operators return 1 for '''true''' and 0 for '''false'''. | |Expressions using logical operators return 1 for '''true''' and 0 for '''false'''. | ||
|- | |- | ||
− | | 00 | + | | 00:58 |
| Now I'll explain the logical operators with the help of an example. | | Now I'll explain the logical operators with the help of an example. | ||
|- | |- | ||
− | | 01 | + | | 01:03 |
| Here is the program for '''logical operators''' in '''C'''. | | Here is the program for '''logical operators''' in '''C'''. | ||
|- | |- | ||
− | | 01 | + | | 01:08 |
| Inside the main block | | Inside the main block | ||
|- | |- | ||
− | | 01 | + | | 01:10 |
|This statement declares the variables '''a''','''b''' and '''c''' as integers. | |This statement declares the variables '''a''','''b''' and '''c''' as integers. | ||
|- | |- | ||
− | | 01 | + | | 01:16 |
| The '''printf''' statement prompts the user to enter the values of '''a''','''b''' and '''c'''. | | The '''printf''' statement prompts the user to enter the values of '''a''','''b''' and '''c'''. | ||
|- | |- | ||
− | |01 | + | |01:21 |
| The '''scanf''' statement takes input from the user for variables '''a''', '''b''' and''' c'''. | | The '''scanf''' statement takes input from the user for variables '''a''', '''b''' and''' c'''. | ||
|- | |- | ||
− | | 01 | + | | 01:28 |
|| Here, We are comparing the values of '''a''' with '''b''' and '''c''' to find the greatest. | || Here, We are comparing the values of '''a''' with '''b''' and '''c''' to find the greatest. | ||
|- | |- | ||
− | | 01 | + | | 01:33 |
|To compare simultaneously, we use the '''logical''' '''AND '''operator. | |To compare simultaneously, we use the '''logical''' '''AND '''operator. | ||
|- | |- | ||
− | | 01 | + | | 01:38 |
|Here, all of the conditions have to be true for '''logical AND '''to return a true value. | |Here, all of the conditions have to be true for '''logical AND '''to return a true value. | ||
|- | |- | ||
− | | 01 | + | | 01:43 |
|The expression is not evaluated further on encountering a false condition. | |The expression is not evaluated further on encountering a false condition. | ||
|- | |- | ||
− | | 01 | + | | 01:49 |
|So, the expression '''(a>c) '''is evaluated only if '''(a>b)''' is true. | |So, the expression '''(a>c) '''is evaluated only if '''(a>b)''' is true. | ||
|- | |- | ||
− | | 01 | + | | 01:56 |
|If a is less than b, then the expression won't be evaluated further. | |If a is less than b, then the expression won't be evaluated further. | ||
|- | |- | ||
− | | 02 | + | | 02:02 |
|This statement is evaluated if the previous condition is true. | |This statement is evaluated if the previous condition is true. | ||
|- | |- | ||
− | | 02 | + | | 02:07 |
| Next '''(b>c) '''is evaluated. | | Next '''(b>c) '''is evaluated. | ||
|- | |- | ||
− | |02 | + | |02:10 |
| If the condition is true, then '''b is greatest''' is displayed on the screen. | | If the condition is true, then '''b is greatest''' is displayed on the screen. | ||
|- | |- | ||
− | | 02 | + | | 02:16 |
| Otherwise '''c is greatest '''is displayed on the screen. | | Otherwise '''c is greatest '''is displayed on the screen. | ||
|- | |- | ||
− | | 02 | + | | 02:21 |
| We now come to the '''logical OR '''operator. | | We now come to the '''logical OR '''operator. | ||
|- | |- | ||
− | | 02 | + | | 02:24 |
|Here, any one of the conditions has to be true for '''logical OR '''to return a true value. | |Here, any one of the conditions has to be true for '''logical OR '''to return a true value. | ||
|- | |- | ||
− | | 02 | + | | 02:30 |
|The expression is not evaluated further on encountering a true condition. | |The expression is not evaluated further on encountering a true condition. | ||
|- | |- | ||
− | | 02 | + | | 02:35 |
|So, if '''a''' == zero, then the remaining two expressions won't be evaluated. | |So, if '''a''' == zero, then the remaining two expressions won't be evaluated. | ||
|- | |- | ||
− | | 02 | + | | 02:43 |
| This '''printf''' statement is executed if either of '''a''', '''b''' or '''c''' is 0. | | This '''printf''' statement is executed if either of '''a''', '''b''' or '''c''' is 0. | ||
|- | |- | ||
− | | 02 | + | | 02:49 |
| Coming to the end of the program. '''return 0''' and ending curly bracket. | | Coming to the end of the program. '''return 0''' and ending curly bracket. | ||
|- | |- | ||
− | | 02 | + | | 02:54 |
| Now save the program. | | Now save the program. | ||
|- | |- | ||
− | | 02 | + | | 02:57 |
|'''Save''' it with extension '''.c''' | |'''Save''' it with extension '''.c''' | ||
|- | |- | ||
− | | 03 | + | | 03:00 |
|I have saved my file as '''logical.c''' | |I have saved my file as '''logical.c''' | ||
|- | |- | ||
− | | 03 | + | | 03:03 |
| Open the terminal by pressing '''Ctrl, Alt and T '''keys simulataneously. | | Open the terminal by pressing '''Ctrl, Alt and T '''keys simulataneously. | ||
|- | |- | ||
− | | 03 | + | | 03:08 |
| To compile the code type '''gcc space logical dot c space minus o space log''' Press enter | | To compile the code type '''gcc space logical dot c space minus o space log''' Press enter | ||
|- | |- | ||
− | | 03 | + | | 03:23 |
|To execute type '''./log''' | |To execute type '''./log''' | ||
|- | |- | ||
− | | 03 | + | | 03:27 |
|Press '''Enter.''' | |Press '''Enter.''' | ||
|- | |- | ||
− | | 03 | + | | 03:29 |
|I will enter the values as, | |I will enter the values as, | ||
Line 184: | Line 184: | ||
|- | |- | ||
− | | 03 | + | | 03:39 |
|The output is displayed as, | |The output is displayed as, | ||
|- | |- | ||
− | | 03 | + | | 03:42 |
|c is greatest. | |c is greatest. | ||
|- | |- | ||
− | | 03 | + | | 03:45 |
|The product of a, b and c is zero. | |The product of a, b and c is zero. | ||
|- | |- | ||
− | | 03 | + | | 03:50 |
|You should try executing this program with different sets of inputs. | |You should try executing this program with different sets of inputs. | ||
|- | |- | ||
− | |03 | + | |03:55 |
| Now Let's write the same program in C++ | | Now Let's write the same program in C++ | ||
|- | |- | ||
− | | 03 | + | | 03:59 |
| I have already made the program and will take you through it. | | I have already made the program and will take you through it. | ||
|- | |- | ||
− | | 04 | + | | 04:03 |
|Here is the code in '''C++'''. | |Here is the code in '''C++'''. | ||
|- | |- | ||
− | | 04 | + | | 04:06 |
| | Now to make the same program in C++, we make a few changes. | | | Now to make the same program in C++, we make a few changes. | ||
|- | |- | ||
− | | 04 | + | | 04:11 |
|There's a change in the header file. | |There's a change in the header file. | ||
|- | |- | ||
− | | 04 | + | | 04:14 |
|Using statement has been used. | |Using statement has been used. | ||
|- | |- | ||
− | | 04 | + | | 04:18 |
|Also there is a difference in output and input statements. | |Also there is a difference in output and input statements. | ||
|- | |- | ||
− | | 04 | + | | 04:21 |
|The operators behave in the same way as they did in C. | |The operators behave in the same way as they did in C. | ||
|- | |- | ||
− | | 04 | + | | 04:25 |
| Click on '''Save'''. | | Click on '''Save'''. | ||
|- | |- | ||
− | | 04 | + | | 04:27 |
| Make sure the file is saved with extension '''.cpp''' | | Make sure the file is saved with extension '''.cpp''' | ||
|- | |- | ||
− | | 04 | + | | 04:31 |
| Open the terminal by pressing '''Ctrl, Alt and T '''keys simulataneously. | | Open the terminal by pressing '''Ctrl, Alt and T '''keys simulataneously. | ||
|- | |- | ||
− | |04 | + | |04:36 |
|To compile the program type '''g++ logical.cpp space minus o space log1'''. Press Enter | |To compile the program type '''g++ logical.cpp space minus o space log1'''. Press Enter | ||
|- | |- | ||
− | | 04 | + | | 04:49 |
|to execute type '''./log1''' | |to execute type '''./log1''' | ||
|- | |- | ||
− | | 04 | + | | 04:53 |
|press '''Enter.''' | |press '''Enter.''' | ||
|- | |- | ||
− | | 04 | + | | 04:56 |
|I will enter the values as | |I will enter the values as | ||
Line 268: | Line 268: | ||
|- | |- | ||
− | | 05 | + | | 05:02 |
|So we see the output is similar to the C program. | |So we see the output is similar to the C program. | ||
|- | |- | ||
− | | 05 | + | | 05:05 |
|You should try executing the program with different sets of inputs too. | |You should try executing the program with different sets of inputs too. | ||
|- | |- | ||
− | |05 | + | |05:10 |
| Now let us see an error which we can come across. | | Now let us see an error which we can come across. | ||
|- | |- | ||
− | | 05 | + | | 05:12 |
|Let's switch to the editor. | |Let's switch to the editor. | ||
|- | |- | ||
− | | 05 | + | | 05:16 |
| Suppose here we forgot the brackets. | | Suppose here we forgot the brackets. | ||
|- | |- | ||
− | | 05 | + | | 05:20 |
|Delete this and this. | |Delete this and this. | ||
|- | |- | ||
− | | 05 | + | | 05:26 |
| Let see what will happen, Save the program. | | Let see what will happen, Save the program. | ||
|- | |- | ||
− | | 05 | + | | 05:30 |
|Come back to the terminal | |Come back to the terminal | ||
|- | |- | ||
− | | 05 | + | | 05:32 |
|Compile and execute as before | |Compile and execute as before | ||
|- | |- | ||
− | | 05 | + | | 05:38 |
| We see the error: | | We see the error: | ||
|- | |- | ||
− | | 05 | + | | 05:41 |
|Expected identifier before '(' token. | |Expected identifier before '(' token. | ||
|- | |- | ||
− | | 05 | + | | 05:45 |
| This is because we have two different expressions here | | This is because we have two different expressions here | ||
|- | |- | ||
− | | 05 | + | | 05:48 |
|We have to evaluate them as one expression using AND operator. | |We have to evaluate them as one expression using AND operator. | ||
|- | |- | ||
− | | 05 | + | | 05:53 |
| Now let us go back to our program and fix the error | | Now let us go back to our program and fix the error | ||
|- | |- | ||
− | | 05 | + | | 05:57 |
| Let us insert the brackets here and here. | | Let us insert the brackets here and here. | ||
|- | |- | ||
− | | 06 | + | | 06:04 |
|Click on '''Save''' | |Click on '''Save''' | ||
|- | |- | ||
− | | 06 | + | | 06:06 |
|Come back to the terminal. | |Come back to the terminal. | ||
|- | |- | ||
− | | 06 | + | | 06:09 |
| Compile and execute as before | | Compile and execute as before | ||
|- | |- | ||
− | | 06 | + | | 06:14 |
|So it is working now. | |So it is working now. | ||
|- | |- | ||
− | | 06 | + | | 06:22 |
|Let us now summarize the tutorial. | |Let us now summarize the tutorial. | ||
|- | |- | ||
− | | 06 | + | | 06:24 |
| In this tutorial we learnt about Logical AND eg. '''((a > b) && (a > c)) ''' | | In this tutorial we learnt about Logical AND eg. '''((a > b) && (a > c)) ''' | ||
|- | |- | ||
− | | 06 | + | | 06:32 |
|Logical OR | |Logical OR | ||
Line 360: | Line 360: | ||
|- | |- | ||
− | | 06 | + | | 06:39 |
|Assignment | |Assignment | ||
|- | |- | ||
− | | 06 | + | | 06:41 |
|Write a program that takes two numbers as input from the user. | |Write a program that takes two numbers as input from the user. | ||
|- | |- | ||
− | | 06 | + | | 06:44 |
|Check whether the two numbers are equal or not using '''NOT''' operator. Hint: '''(a != b)''' | |Check whether the two numbers are equal or not using '''NOT''' operator. Hint: '''(a != b)''' | ||
|- | |- | ||
− | | 06 | + | | 06:54 |
| Watch the video available at the following link | | Watch the video available at the following link | ||
|- | |- | ||
− | | 06 | + | | 06:57 |
|It summarises the Spoken Tutorial project | |It summarises the Spoken Tutorial project | ||
|- | |- | ||
− | | 06 | + | | 06:59 |
|If you do not have good bandwidth, you can download and watch it | |If you do not have good bandwidth, you can download and watch it | ||
Line 386: | Line 386: | ||
|- | |- | ||
− | | 07 | + | | 07:03 |
| The Spoken Tutorial Project Team * Conducts workshops using spoken tutorials | | The Spoken Tutorial Project Team * Conducts workshops using spoken tutorials | ||
|- | |- | ||
− | | 07 | + | | 07:07 |
| Gives certificates for those who pass an online test | | Gives certificates for those who pass an online test | ||
|- | |- | ||
− | | 07 | + | | 07:11 |
| For more details, please write to contact at spoken hyphen tutorial dot org | | For more details, please write to contact at spoken hyphen tutorial dot org | ||
|- | |- | ||
− | | 07 | + | | 07:18 |
| Spoken Tutorial Project is a part of the Talk to a Teacher project | | Spoken Tutorial Project is a part of the Talk to a Teacher project | ||
|- | |- | ||
− | | 07 | + | | 07:21 |
|It is supported by the National Mission on Education through ICT, MHRD, Government of India | |It is supported by the National Mission on Education through ICT, MHRD, Government of India | ||
|- | |- | ||
− | | 07 | + | | 07:27 |
| More information on this Mission is available at | | More information on this Mission is available at | ||
|- | |- | ||
− | | 07 | + | | 07:30 |
| spoken hyphen tutorial dot org slash NMEICT hyphen Intro | | spoken hyphen tutorial dot org slash NMEICT hyphen Intro | ||
Line 415: | Line 415: | ||
|- | |- | ||
− | | 07 | + | | 07:37 |
| This is Ritwik Joshi from IIT Bombay. | | This is Ritwik Joshi from IIT Bombay. | ||
Revision as of 12:52, 23 June 2014
Time | Narration |
00:02 | Welcome to the spoken tutorial on Logical operators in C and C++. |
00:08 | In this tutorial we will learn about:Logical operators like Logical AND eg. expression1 && expression2 |
00:16 | Logical OR
eg. expression1 or expression2 |
00:21 | Logical NOT
eg. not (Expression1) |
00:25 | We will do this with the help of examples. |
00:28 | To record this tutorial, I am using:Ubuntu 11.10 as the operating system |
00:33 | gcc and g++ Compiler version 4.6.1 on Ubuntu. |
00:39 | Let us start with the introduction to the logical operators. |
00:43 | In C and C++, true is any value other than 0. |
00:48 | non zero means true |
00:50 | And zero means false
|
00:53 | Expressions using logical operators return 1 for true and 0 for false. |
00:58 | Now I'll explain the logical operators with the help of an example. |
01:03 | Here is the program for logical operators in C. |
01:08 | Inside the main block |
01:10 | This statement declares the variables a,b and c as integers. |
01:16 | The printf statement prompts the user to enter the values of a,b and c. |
01:21 | The scanf statement takes input from the user for variables a, b and c. |
01:28 | Here, We are comparing the values of a with b and c to find the greatest. |
01:33 | To compare simultaneously, we use the logical AND operator. |
01:38 | Here, all of the conditions have to be true for logical AND to return a true value. |
01:43 | The expression is not evaluated further on encountering a false condition. |
01:49 | So, the expression (a>c) is evaluated only if (a>b) is true. |
01:56 | If a is less than b, then the expression won't be evaluated further. |
02:02 | This statement is evaluated if the previous condition is true. |
02:07 | Next (b>c) is evaluated. |
02:10 | If the condition is true, then b is greatest is displayed on the screen. |
02:16 | Otherwise c is greatest is displayed on the screen. |
02:21 | We now come to the logical OR operator. |
02:24 | Here, any one of the conditions has to be true for logical OR to return a true value. |
02:30 | The expression is not evaluated further on encountering a true condition. |
02:35 | So, if a == zero, then the remaining two expressions won't be evaluated. |
02:43 | This printf statement is executed if either of a, b or c is 0. |
02:49 | Coming to the end of the program. return 0 and ending curly bracket. |
02:54 | Now save the program. |
02:57 | Save it with extension .c |
03:00 | I have saved my file as logical.c |
03:03 | Open the terminal by pressing Ctrl, Alt and T keys simulataneously. |
03:08 | To compile the code type gcc space logical dot c space minus o space log Press enter |
03:23 | To execute type ./log |
03:27 | Press Enter.
|
03:29 | I will enter the values as,
0 34 567 |
03:39 | The output is displayed as, |
03:42 | c is greatest. |
03:45 | The product of a, b and c is zero. |
03:50 | You should try executing this program with different sets of inputs. |
03:55 | Now Let's write the same program in C++ |
03:59 | I have already made the program and will take you through it. |
04:03 | Here is the code in C++. |
04:06 | Now to make the same program in C++, we make a few changes. |
04:11 | There's a change in the header file. |
04:14 | Using statement has been used. |
04:18 | Also there is a difference in output and input statements. |
04:21 | The operators behave in the same way as they did in C.
|
04:25 | Click on Save. |
04:27 | Make sure the file is saved with extension .cpp |
04:31 | Open the terminal by pressing Ctrl, Alt and T keys simulataneously. |
04:36 | To compile the program type g++ logical.cpp space minus o space log1. Press Enter |
04:49 | to execute type ./log1 |
04:53 | press Enter.
|
04:56 | I will enter the values as
0 34 567 |
05:02 | So we see the output is similar to the C program. |
05:05 | You should try executing the program with different sets of inputs too. |
05:10 | Now let us see an error which we can come across. |
05:12 | Let's switch to the editor. |
05:16 | Suppose here we forgot the brackets. |
05:20 | Delete this and this. |
05:26 | Let see what will happen, Save the program. |
05:30 | Come back to the terminal |
05:32 | Compile and execute as before |
05:38 | We see the error: |
05:41 | Expected identifier before '(' token. |
05:45 | This is because we have two different expressions here |
05:48 | We have to evaluate them as one expression using AND operator. |
05:53 | Now let us go back to our program and fix the error |
05:57 | Let us insert the brackets here and here. |
06:04 | Click on Save |
06:06 | Come back to the terminal. |
06:09 | Compile and execute as before |
06:14 | So it is working now. |
06:22 | Let us now summarize the tutorial. |
06:24 | In this tutorial we learnt about Logical AND eg. ((a > b) && (a > c))
|
06:32 | Logical OR
eg. (a == 0 || b == 0 || c == 0)
|
06:39 | Assignment |
06:41 | Write a program that takes two numbers as input from the user. |
06:44 | Check whether the two numbers are equal or not using NOT operator. Hint: (a != b) |
06:54 | Watch the video available at the following link |
06:57 | It summarises the Spoken Tutorial project |
06:59 | If you do not have good bandwidth, you can download and watch it
|
07:03 | The Spoken Tutorial Project Team * Conducts workshops using spoken tutorials |
07:07 | Gives certificates for those who pass an online test |
07:11 | For more details, please write to contact at spoken hyphen tutorial dot org |
07:18 | Spoken Tutorial Project is a part of the Talk to a Teacher project |
07:21 | It is supported by the National Mission on Education through ICT, MHRD, Government of India |
07:27 | More information on this Mission is available at |
07:30 | spoken hyphen tutorial dot org slash NMEICT hyphen Intro
|
07:37 | This is Ritwik Joshi from IIT Bombay.
Thank you for joining. |