Difference between revisions of "C-and-C++/C2/Relational-Operators/English"
(Created page with ''''Title of script''':Advanced Operators in C and C++ '''Author: Ritwik Joshi''' '''Keywords: Relational Operators, Video Tutorial''' {| style="border-spacing:0;" | style="…') |
Nancyvarkey (Talk | contribs) |
||
Line 43: | Line 43: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using: | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using: | ||
− | '''Ubuntu 11.10''' as the operating system and | + | *'''Ubuntu 11.10''' as the operating system and |
− | '''gcc''' and '''g++ Compiler '''version '''4.6.1 '''in '''Ubuntu.''' | + | *'''gcc''' and '''g++ Compiler '''version '''4.6.1 '''in '''Ubuntu.''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 64: | Line 60: | ||
Relational operators are used to compare integer and floating point numbers. | Relational operators are used to compare integer and floating point numbers. | ||
− | Expressions using relational operators '''return''' | + | Expressions using relational operators '''return 0''' for '''false''' and '''1''' for '''true'''. |
− | + | ||
− | ''' | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 122: | Line 112: | ||
'''printf("%d is greater than %d \n",a,b); ''' | '''printf("%d is greater than %d \n",a,b); ''' | ||
− | | 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 the above condition is true. | + | | 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 the above condition is '''true'''. |
− | If the above condition is '''false '''then | + | If the above condition is '''false, '''then it is skipped. |
The control then jumps to the next statement. | The control then jumps to the next statement. | ||
Line 135: | Line 125: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We now have the '''less than''' 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 have the '''less than''' operator. | ||
− | This too compares the operands. | + | This, too, compares the operands. |
− | It '''returns | + | It '''returns true''' when '''a''' is less than '''b'''. |
|- | |- | ||
Line 144: | Line 134: | ||
'''printf("%d is less than %d \n",a,b); ''' | '''printf("%d is less than %d \n",a,b); ''' | ||
− | | 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 | + | | 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 when the above condition is '''true.''' |
It is skipped otherwise. | It is skipped otherwise. | ||
Line 164: | Line 154: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save'''. | ||
− | I have saved my file as relational.c | + | I have saved my file as '''relational.c''' |
|- | |- | ||
Line 194: | Line 184: | ||
'''8 is greater than 3'''. | '''8 is greater than 3'''. | ||
− | | 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 enter '''a''' as 8 and '''b '''as 3. |
− | + | The output is: '''8 is greater than 3.''' | |
− | + | ||
− | The output is: | + | |
− | + | ||
− | 8 is greater than 3. | + | |
You can try executing this code with different values of '''a '''and '''b'''. | You can try executing this code with different values of '''a '''and '''b'''. | ||
Line 212: | Line 198: | ||
/* | /* | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Coming back to | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Coming back to the code. |
− | + | ||
− | + | ||
− | + | Delete the comment from here and put it here. | |
|- | |- | ||
Line 234: | Line 218: | ||
'''printf("%d is less than or equal to %d \n",a,b);''' | '''printf("%d is less than or equal to %d \n",a,b);''' | ||
− | | 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 the above condition is true. | + | | 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 the above condition is '''true'''. |
− | If the above condition is '''false '''then it is skipped. | + | If the above condition is '''false, '''then it is skipped. |
The control then jumps to the next statement. | The control then jumps to the next statement. | ||
Line 254: | Line 238: | ||
'''printf("%d is greater than or equal to %d \n",a,b);''' | '''printf("%d is greater than or equal to %d \n",a,b);''' | ||
− | | 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 this printf statement will be executed. | + | | 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 this '''printf statement''' will be executed. |
|- | |- | ||
Line 262: | Line 246: | ||
Click on '''Save'''. | Click on '''Save'''. | ||
− | Switch back to the terminal. | + | Switch back to the '''terminal'''. |
Compile and execute as before. | Compile and execute as before. | ||
Line 271: | Line 255: | ||
'''8 is greater than or equal to 3''' | '''8 is greater than or equal to 3''' | ||
− | | 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 enter '''a''' as 8 and '''b '''as 3. |
− | + | ||
− | I enter '''a''' as 8 and '''b '''as 3. | + | |
The output is displayed: | The output is displayed: | ||
− | 8 is greater than or equal to 3 | + | '''8 is greater than or equal to 3''' |
|- | |- | ||
Line 287: | Line 269: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Coming back to 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;"| Coming back to rest of the code. | ||
− | Delete the multiline comments. | + | Delete the multiline comments from here and here. |
|- | |- | ||
Line 298: | Line 280: | ||
It is denoted by double '''equal (==) '''signs. | It is denoted by double '''equal (==) '''signs. | ||
− | This operator '''returns | + | This operator '''returns true''' when both operands are equal to one another. |
|- | |- | ||
Line 333: | Line 315: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Coming to the end of the program. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Coming to the end of the program. | ||
− | ''' | + | '''return 0;''' |
|- | |- | ||
Line 341: | Line 323: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch back to the '''terminal'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch back to the '''terminal'''. | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch back to the terminal. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch back to the '''terminal'''. |
Compile and execute as before. | Compile and execute as before. | ||
Line 350: | Line 332: | ||
'''8 is not equal to 3''' | '''8 is not equal to 3''' | ||
− | | 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;"|Enter '''a''' as 8 and '''b '''as 3. |
− | + | ||
− | Enter '''a''' as 8 and '''b '''as 3. | + | |
The output is displayed on the screen: | The output is displayed on the screen: | ||
− | 8 is greater than 3 | + | '''8 is greater than 3''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | So, we see how the relational | + | So, we see how the relational operators work. |
Try executing this code with different set of inputs. | Try executing this code with different set of inputs. | ||
Line 437: | Line 413: | ||
Also we have the '''using '''statement here. | Also we have the '''using '''statement here. | ||
− | The output statement in C++ is '''cout'''. | + | The output statement in '''C++''' is '''cout'''. |
− | And the input statement in C++ is''' cin.''' | + | And the input statement in '''C++''' is''' cin.''' |
So, apart from these differences, the two codes are very similar. | So, apart from these differences, the two codes are very similar. | ||
Line 445: | Line 421: | ||
|- | |- | ||
| 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;"| Click on | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save'''. |
Please make sure the file is saved with the extension '''.cpp''' | Please make sure the file is saved with the extension '''.cpp''' | ||
Line 461: | Line 437: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's compile the code. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's compile the code. | ||
− | Open the terminal and type | + | Open the '''terminal''' and type |
'''g++ relational.cpp -o rel1''' | '''g++ relational.cpp -o rel1''' | ||
− | + | To execute, type | |
− | + | ||
− | + | ||
'''./ rel1''' | '''./ rel1''' | ||
Line 473: | Line 447: | ||
|- | |- | ||
| 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;"|I enter '''a''' as 8 and '''b '''as 3. |
− | + | ||
− | I enter '''a''' as 8 and '''b '''as 3. | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | The output is displayed. | ||
− | We see that the output is same as in the '''C''' | + | We see that the output is same as in the '''C''' program. |
|- | |- | ||
Line 492: | Line 457: | ||
| 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 see an error which we can come across. | | 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 see an error which we can come across. | ||
− | Come back to our program | + | Come back to our program. |
|- | |- | ||
| 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;"| Suppose here we replace the double equal to sign with the single equal to. | + | | 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 replace the double '''equal to''' sign with the single '''equal to'''. |
|- | |- | ||
Line 502: | Line 467: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save.''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save.''' | ||
− | Come back to out terminal. | + | Come back to out '''terminal'''. |
Compile and execute as before. | Compile and execute as before. | ||
Line 508: | Line 473: | ||
|- | |- | ||
| 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;"| Here we see it is showing 3 is equal to 3. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we see it is showing '''3 is equal to 3'''. |
− | Come back to our program | + | Come back to our program. |
This is because here we have an assignment operator. | This is because here we have an assignment operator. | ||
− | So value of b is assigned to a. | + | So value of '''b''' is assigned to '''a'''. |
|- | |- | ||
Line 520: | Line 485: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us 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;"| Let us fix the error. | ||
− | Type an equal to sign. | + | Type an '''equal to''' sign. |
Click on '''Save''' | Click on '''Save''' |
Revision as of 17:02, 13 December 2013
Title of script:Advanced Operators in C and C++
Author: Ritwik Joshi
Keywords: Relational Operators, Video Tutorial
|
|
Slide 1 | Welcome to the spoken tutorial on Relational Operators in C and C++. |
Slide 2
|
In this tutorial, we will learn about:
Relational operators like Less than: eg. a < b Greater than: eg. a > b Less than or equal to: eg. a <= b Greater than or equal to: eg. a >= b Equal to: eg. a == b Not equal to: eg. a != b |
Slide 3
|
To record this tutorial, I am using:
|
Slide 4
0 when False 1 when True |
Let us begin with an introduction.
Relational operators are used to compare integer and floating point numbers. Expressions using relational operators return 0 for false and 1 for true. |
Now I will demonstrate the relational operators with the help of a C program. | |
Switch to relational.c in gedit | I have already made the program.
So, I'll open the editor and explain the code. |
Highlight
|
First, we declare two variables a and b. |
Highlight
|
This printf statement prompts the user to enter the values of a and b.
|
Highlight
|
This scanf statement takes input for the variables a and b. |
Highlight
|
Now we have the greater than operator.
This operator compares the two operands on either side of the operator. It returns true if a is greater than b. |
Highlight
|
This printf statement is executed, if the above condition is true.
If the above condition is false, then it is skipped. The control then jumps to the next statement. |
Highlight
|
We now have the less than operator.
This, too, compares the operands. It returns true when a is less than b. |
Highlight
|
This printf statement is executed when the above condition is true.
It is skipped otherwise. |
Type
/* */ |
Let's execute the code till here.
First comment out these lines. Type /* */ |
Click on Save. | Click on Save.
I have saved my file as relational.c |
press Ctrl, Alt and T keys simultaneously. | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously. |
Type
gcc relational.c -o rel Type ./rel |
To compile, type the following on the terminal
gcc relational.c -o rel Press Enter. To execute the code, type ./rel Press Enter. |
Highlight
8 is greater than 3. |
I enter a as 8 and b as 3.
The output is: 8 is greater than 3. You can try executing this code with different values of a and b. |
Delete
/* Retype /* |
Coming back to the code.
Delete the comment from here and put it here. |
Highlight
|
Now we have the less than or equal to operator.
This operator compares the two operands on either side of the operator. It returns true if a is less than or equal to b. |
Highlight
|
This printf statement is executed if the above condition is true.
If the above condition is false, then it is skipped. The control then jumps to the next statement. |
Highlight
|
Next comes the greater than or equal to operator.
It compares a and b and returns true if a is greater than or equal to b. |
Highlight
|
If the condition is true, then this printf statement will be executed. |
Click on Save. | Now let's execute the code till here.
Click on Save. Switch back to the terminal. Compile and execute as before. |
Highlight
|
I enter a as 8 and b as 3.
The output is displayed: 8 is greater than or equal to 3 |
Delete
/* */ |
Coming back to rest of the code.
Delete the multiline comments from here and here. |
Highlight
|
Now we have the equal to operator.
It is denoted by double equal (==) signs. This operator returns true when both operands are equal to one another. |
Highlight
|
This printf statement executes when a is equal to b.
If not, the control then jumps on to the next statement. |
Highlight
|
Similarly, we have the not equal to operator.
This operator returns true when the operands are not equal to one another. |
Highlight
|
This printf statment will execute when a is not equal to b. |
Highlight
return 0; } |
Coming to the end of the program.
return 0; |
Click on Save. | Click on Save. |
Switch back to the terminal. | Switch back to the terminal.
Compile and execute as before. |
Highlight
|
Enter a as 8 and b as 3.
The output is displayed on the screen: 8 is greater than 3 So, we see how the relational operators work. Try executing this code with different set of inputs. |
Now, writing a smilar program in C++ is quite easy.
There are a few differences in the syntax. I have already made the code in C++. | |
Switch to relational.cpp
#include <iostream> using namespace std; int main() { int a, b; cout <<"Enter the values of a and b \n"; cin >>a >>b; if(a > b) cout <<a <<" is greater than " <<b <<"\n"; else if(a < b) cout <<a <<" is less than " <<b <<"\n"; if(a <= b) cout <<a <<" is less than or equal to " <<b <<"\n"; if(a >= b) cout <<a <<" is greater than or equal to " <<b <<"\n"; if(a == b) cout <<a <<" is equal to " <<b <<"\n"; else if (a != b) cout <<a <<" is not equal to "<< b <<" \n"; return 0; } |
Here is the code for relational operators in C++. |
Highlight
#include<iostream>
|
Notice that the header is different.
Also we have the using statement here. The output statement in C++ is cout. And the input statement in C++ is cin. So, apart from these differences, the two codes are very similar. |
Click on Save.
Please make sure the file is saved with the extension .cpp I have saved my file as relational.cpp | |
Type
|
Let's compile the code.
Open the terminal and type g++ relational.cpp -o rel1 To execute, type ./ rel1 |
I enter a as 8 and b as 3.
The output is displayed. We see that the output is same as in the C program. | |
Now let us see an error which we can come across.
Come back to our program. | |
Suppose here we replace the double equal to sign with the single equal to. | |
Click on Save.
Come back to out terminal. Compile and execute as before. | |
Here we see it is showing 3 is equal to 3.
Come back to our program. This is because here we have an assignment operator. So value of b is assigned to a. | |
Let us fix the error.
Type an equal to sign. Click on Save | |
Switch back to our terminal
Let us compile and execute as before. The output is now correct. | |
Summary | Let's summarize the tutorial.
In this tutorial, we learnt Relational operators like Less than: eg. a <b Greater than: eg. a>b Less than or equal to: eg. a<=b Greater than or equal to: eg. a>=b Equal to: eg. a==b Not equal to: eg. a!=b |
Assignment: | As an assignment
Write a program that takes the marks of three students as input. Compare the marks to see which student has scored the highest. Check also if two or more students have scored equal marks. |
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
|
Slide 7
Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
Remain on previous slide
No slide for this part |
This is Ritwik Joshi from IIT Bombay.
Thank you for joining. |