Difference between revisions of "C-and-C++/C2/Logical-Operators/English"
Nancyvarkey (Talk | contribs) |
|||
Line 17: | Line 17: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2 | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about: | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about: | ||
+ | *Logical Operators | ||
+ | *Logical AND | ||
+ | ** Eg- '''expression1 && expression2''' | ||
+ | * Logical OR | ||
+ | ** Eg- '''expression1 || expression2''' | ||
+ | * Logical NOT | ||
+ | ** Eg- '''!(expression1) | ||
+ | ''' | ||
+ | *We will do these with the help of examples. | ||
|- | |- | ||
Line 36: | Line 45: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to the '''logical operators'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to the '''logical operators'''. | ||
− | In '''C '''and '''C++,''' '''true''' is any value other than 0. | + | In '''C '''and '''C++,''' '''true''' is any value, other than 0. |
− | + | Non-zero means''' true'''. | |
− | ''' | + | Zero means '''false''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Line 62: | Line 67: | ||
'''int a,b,c; ''' | '''int a,b,c; ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Inside the main block | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Inside the main block, this statement declares the variables '''a''','''b''' and '''c''' as '''integers'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 91: | Line 94: | ||
'''printf("a is greatest \n"); ''' | '''printf("a is greatest \n"); ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, we are comparing the values of '''a''' with '''b''' and '''c''' to find the greatest. |
− | To compare simultaneously, we use the '''logical | + | To compare simultaneously, we use the '''logical AND '''operator. |
− | 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. |
− | The expression is not evaluated further on encountering a false condition. | + | The expression is not evaluated further on encountering a '''false''' condition. |
− | 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'''. |
− | 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. |
− | This statement is be evaluated if the previous condition is true. | + | This statement is be evaluated if the previous condition is '''true'''. |
|- | |- | ||
Line 111: | Line 114: | ||
'''else if (b > c) ''' | '''else if (b > c) ''' | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next '''(b>c) '''is evaluated. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next '''(b>c) '''is evaluated. | ||
− | |||
− | |||
Line 123: | Line 124: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If the condition is true, then '''b is greatest''' is displayed on the screen. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If the condition is '''true''', then '''b is greatest''' is displayed on the screen. |
|- | |- | ||
Line 144: | Line 145: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We now come to the '''logical OR '''operator. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We now come to the '''logical OR '''operator. | ||
− | 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. |
− | The expression is not evaluated further on encountering a true condition. | + | The expression is not evaluated further on encountering a '''true''' condition. |
− | So, if '''a | + | So, if '''a == zero''', then the remaining two expressions won't be evaluated. |
|- | |- | ||
Line 160: | Line 161: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This '''printf''' statement is executed if either of '''a''', '''b''' or '''c''' is 0. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This '''printf''' statement is executed, if, either of '''a''', '''b''' or '''c''' is 0. |
|- | |- | ||
Line 180: | Line 181: | ||
I have saved my file as '''logical.c''' | I have saved my file as '''logical.c''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| press '''Ctrl, Alt and T '''keys''' '''simultaneously. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| press '''Ctrl, Alt and T '''keys''' '''simultaneously. | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the terminal by pressing '''Ctrl, Alt and T '''keys simulataneously. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the '''terminal''' by pressing '''Ctrl, Alt and T '''keys simulataneously. |
|- | |- | ||
Line 196: | Line 194: | ||
'''./log''' | '''./log''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the code type | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the code, type |
'''gcc logical.c -o log''' | '''gcc logical.c -o log''' | ||
− | + | Press '''Enter.''' | |
− | '''./log''' | + | For executing the code, type '''./log''' |
Press '''Enter.''' | Press '''Enter.''' | ||
Line 208: | Line 206: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output: | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will enter the values as 0 34 567. |
− | + | ||
− | + | ||
− | + | ||
− | I will enter the values as | + | |
− | + | ||
− | 0 | + | |
− | + | ||
− | 34 | + | |
− | + | ||
− | 567 | + | |
The output is displayed as, | The output is displayed as, | ||
− | c is greatest. | + | '''c is greatest.''' |
− | The product of a, b and c is zero. | + | '''The product of a, b and c is zero.''' |
You should try executing this program with different sets of inputs. | You should try executing this program with different sets of inputs. | ||
Line 230: | Line 218: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, let's write the same program in '''C++''' |
|- | |- | ||
Line 278: | Line 266: | ||
} | } | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now to make the same program in C++, we make a few changes. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now to make the same program in''' C++''', we make a few changes. |
− | There's a change in the header | + | There's a change in the header files. |
− | + | '''using''' statement has been used. | |
Also there is a difference in output and input statements. | Also there is a difference in output and input statements. | ||
− | The operators behave in the same way as they did in C. | + | The operators behave in the same way as they did in '''C'''. |
− | + | ||
− | + | ||
Line 301: | Line 287: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| press '''Ctrl, Alt and T '''keys''' '''simultaneously. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| press '''Ctrl, Alt and T '''keys''' '''simultaneously. | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the terminal by pressing '''Ctrl, Alt and T '''keys simulataneously. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the '''terminal''' by pressing '''Ctrl, Alt and T '''keys simulataneously. |
|- | |- | ||
Line 311: | Line 297: | ||
'''./log''' | '''./log''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program type | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program, type |
'''g++ logical.cpp -o log1''' | '''g++ logical.cpp -o log1''' | ||
− | + | Press '''Enter.''' | |
+ | |||
+ | To execute the program, type | ||
'''./log1''' | '''./log1''' | ||
− | + | Press '''Enter.''' | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output: | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|I will enter the values as 0 34 567. |
− | + | We see the output is similar to the '''C''' program. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | We see the output is similar to the C program. | + | |
You should try executing the program with different sets of inputs too. | You should try executing the program with different sets of inputs too. | ||
Line 341: | Line 319: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Errors''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Errors''' | ||
− | |||
Line 353: | Line 330: | ||
'''if(a > b) && (a > c)''' | '''if(a > b) && (a > c)''' | ||
− | |||
− | |||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Suppose here we forgot the brackets. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Suppose here we forgot the brackets. | ||
Line 362: | Line 337: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let see what will happen | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let see what will happen. Save the program. |
− | Come back to the terminal | + | Come back to the '''terminal'''. |
− | Compile and execute as before | + | Compile and execute as before. |
|- | |- | ||
Line 372: | Line 347: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the error: | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the error: | ||
− | Expected identifier before '(' token. | + | '''Expected identifier before '(' token.''' |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because we have two different expressions here | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because we have two different expressions here. |
− | We have to evaluate them as one expression using AND operator. | + | We have to evaluate them as one expression using '''AND''' operator. |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us go back to our program and fix the error | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us go back to our program and fix the error. |
|- | |- | ||
Line 388: | Line 363: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us insert the brackets here and here. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us insert the brackets here and here. | ||
− | Click on '''Save''' | + | Click on '''Save'''. |
− | Come back to the terminal. | + | Come back to the '''terminal'''. |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile and execute as before | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile and execute as before. |
− | So it is working now. | + | So, it is working now. |
Let us now summarize the tutorial. | Let us now summarize the tutorial. | ||
Line 404: | Line 379: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we learnt about the logical operators | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we learnt about the logical operators | ||
− | && Logical AND | + | *&& Logical AND |
− | + | **eg. '''((a > b) && (a > c)) ''' | |
− | eg. '''((a > b) && (a > c)) ''' | + | *|| Logical OR |
− | + | **eg. '''(a == 0 || b == 0 || c == 0) ''' | |
− | + | ||
− | || Logical OR | + | |
− | + | ||
− | eg. '''(a == 0 || b == 0 || c == 0) ''' | + | |
− | + | ||
− | + | ||
Revision as of 17:57, 13 December 2013
Title of script: Logical Operators in C and C++
Author:Ritwik Joshi
Keywords: Logical Operators, Video Tutorial
|
|
Slide 1 | Welcome to the spoken tutorial on Logical operators in C and C++. |
Slide 2 | In this tutorial we will learn about:
|
Slide 3 | To record this tutorial, I am using:
Ubuntu 11.10 as the operating system gcc and g++ Compiler version 4.6.1 on Ubuntu. |
Slide 4
|
Let us start with the introduction to the logical operators.
In C and C++, true is any value, other than 0. Non-zero means true. Zero means false
|
Now I'll explain the logical operators with the help of an example. | |
Switch to logical.c in gedit | Here is the program for logical operators in C. |
Highlight
|
Inside the main block, this statement declares the variables a,b and c as integers. |
Highlight
and c \n"); |
The printf statement prompts the user to enter the values of a,b and c. |
Highlight
&c); |
The scanf statement takes input from the user for the variables a, b and c. |
Highlight
printf("a is greatest \n"); |
Here, we are comparing the values of a with b and c to find the greatest.
To compare simultaneously, we use the logical AND operator. Here, all of the conditions have to be true for logical AND to return a true value. The expression is not evaluated further on encountering a false condition. So, the expression (a>c) is evaluated only if (a>b) is true. If a is less than b, then the expression won't be evaluated further. This statement is be evaluated if the previous condition is true. |
Highlight
|
Next (b>c) is evaluated.
|
Highlight
|
If the condition is true, then b is greatest is displayed on the screen. |
Highlight
printf("c is greatest \n"); |
Otherwise c is greatest is displayed on the screen. |
Highlight
|
We now come to the logical OR operator.
Here, any one of the conditions has to be true for logical OR to return a true value. The expression is not evaluated further on encountering a true condition. So, if a == zero, then the remaining two expressions won't be evaluated. |
Highlight
and c is zero \n");
|
This printf statement is executed, if, either of a, b or c is 0. |
Highlight
} |
Coming to the end of the program.
return 0 and ending curly bracket. |
Click on Save | Now save the program.
Save it with extension .c I have saved my file as logical.c |
press Ctrl, Alt and T keys simultaneously. | Open the terminal by pressing Ctrl, Alt and T keys simulataneously. |
Type
gcc logical.c -o log Type ./log |
To compile the code, type
gcc logical.c -o log Press Enter. For executing the code, type ./log Press Enter. |
Output: | I will enter the values as 0 34 567.
The output is displayed as, c is greatest. The product of a, b and c is zero. You should try executing this program with different sets of inputs. |
Now, let's write the same program in C++ | |
Open the C++ file. | I have already made the program and will take you through it.
Here is the code in C++. |
//Logical operators in C++
#include <iostream> using namespace std; int main() { int a,b,c; cout <<"Enter the values of a,b and c \n"; cin >>a >>b >>c; if((a > b) && (a > c)) cout <<"a is greatest \n"; else if (b > c) cout <<"b is greatest \n"; else cout <<"c is greatest \n"; if(a == 0 || b == 0 || c == 0) cout <<"The product of a, b and c is zero \n"; return 0; } |
Now to make the same program in C++, we make a few changes.
There's a change in the header files. using statement has been used. Also there is a difference in output and input statements. The operators behave in the same way as they did in C.
|
Click on Save.
point to .cpp |
Click on Save.
Make sure the file is saved with extension .cpp |
press Ctrl, Alt and T keys simultaneously. | Open the terminal by pressing Ctrl, Alt and T keys simulataneously. |
On the terminal type
g++ logical.cpp -o log to execute ./log |
To compile the program, type
g++ logical.cpp -o log1 Press Enter. To execute the program, type ./log1 Press Enter. |
Output: | I will enter the values as 0 34 567.
We see the output is similar to the C program. You should try executing the program with different sets of inputs too. |
Errors
|
Now let us see an error which we can come across.
Let's switch to the editor. |
Delete
|
Suppose here we forgot the brackets.
Delete this and this. |
Let see what will happen. Save the program.
Come back to the terminal. Compile and execute as before. | |
Highlight Error | We see the error:
Expected identifier before '(' token. |
This is because we have two different expressions here.
We have to evaluate them as one expression using AND operator. | |
Now let us go back to our program and fix the error. | |
Let us insert the brackets here and here.
Click on Save. Come back to the terminal. | |
Let us compile and execute as before.
So, it is working now. Let us now summarize the tutorial. | |
Summary: | In this tutorial we learnt about the logical operators
|
Assignment: | Write a program that takes two numbers as input from the user.
Check whether the two numbers are equal or not using NOT operator. Hint: (a != b) |
Slide 5
About the Spoken Tutorial Project
|
* Watch the video available at the following link
|
Slide 6
Spoken Tutorial Workshops The Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team * Conducts workshops using spoken tutorials
|
Slide 7
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
|
Remain on previous slide
No slide for this part |
This is Ritwik Joshi from IIT Bombay.
Thank you for joining. |