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="…') |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 34: | Line 34: | ||
Equal to: eg. a == b | Equal to: eg. a == b | ||
− | Not equal to: eg. a != b | + | And Not equal to: eg. a != b |
|- | |- | ||
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 ''' | + | *'''gcc''' and '''g++ Compiler '''version '''4.6.1 '''on '''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 95: | Line 85: | ||
'''printf("Enter the values of a and b\n"); ''' | '''printf("Enter the values of a and b\n"); ''' | ||
| 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 prompts the user to enter the values of a and 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 prompts the user to enter the values of a and b. | ||
− | |||
− | |||
Line 122: | Line 110: | ||
'''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 123: | ||
| 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 132: | ||
'''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 156: | Line 144: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's execute the code till here. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's execute the code till here. | ||
− | First comment out | + | First comment out the following. |
Type /* <nowiki>*/</nowiki>''' ''' | Type /* <nowiki>*/</nowiki>''' ''' | ||
Line 164: | Line 152: | ||
| 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 180: | Line 168: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile, type the following on the terminal | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile, type the following on the terminal | ||
− | '''gcc relational.c -o rel''' | + | '''gcc space relational.c space -o space rel''' |
Press '''Enter'''. | Press '''Enter'''. | ||
Line 194: | Line 182: | ||
'''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 displayed: '''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 196: | ||
/* | /* | ||
− | | 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 216: | ||
'''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 236: | ||
'''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 244: | ||
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 253: | ||
'''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 285: | Line 265: | ||
<nowiki>*/</nowiki> | <nowiki>*/</nowiki> | ||
− | | 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 coming back to rest of the code. |
− | Delete the multiline comments. | + | Delete the multiline comments from here and here. |
|- | |- | ||
Line 294: | Line 274: | ||
'''if(a == b)''' | '''if(a == b)''' | ||
− | | 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;"| We now have the '''equal to '''operator. |
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 313: | ||
| 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 321: | ||
|- | |- | ||
| 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 330: | ||
'''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 not equal to 3''' | |
− | + | ||
− | + | ||
− | + | ||
− | 8 is not equal to 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 411: | ||
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 419: | ||
|- | |- | ||
| 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 | + | Please make sure the file is saved with extension '''.cpp''' |
I have saved my file as '''relational.cpp''' | I have saved my file as '''relational.cpp''' | ||
Line 461: | Line 435: | ||
| 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 space -o space rel1''' |
− | + | To execute, type | |
− | + | ||
− | + | ||
'''./ rel1''' | '''./ rel1''' | ||
+ | '''Press Enter''' | ||
|- | |- | ||
| 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. |
− | + | The output is displayed. | |
− | + | We see that the output is same as the one in '''C''' program. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | We see that the output is same as in | + | |
|- | |- | ||
Line 492: | Line 456: | ||
| 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 466: | ||
| 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 472: | ||
|- | |- | ||
| 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'''. |
|- | |- | ||
| 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 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 fix the error. |
− | Type an equal to sign. | + | Type an '''equal to''' sign. |
Click on '''Save''' | Click on '''Save''' | ||
Line 526: | Line 490: | ||
|- | |- | ||
| 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;"| Switch back to | + | | 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. | |
The output is now correct. | The output is now correct. | ||
Line 534: | Line 498: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Summary | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Summary | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us now summarize the tutorial. |
In this tutorial, we learnt | In this tutorial, we learnt |
Latest revision as of 15:57, 5 February 2014
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 And 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 the following. 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 space relational.c space -o space 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 displayed: 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
/* */ |
Now coming back to rest of the code.
Delete the multiline comments from here and here. |
Highlight
|
We now 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 not equal to 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 extension .cpp I have saved my file as relational.cpp | |
Type
|
Let's compile the code.
Open the terminal and type g++ relational.cpp space -o space rel1 To execute, type ./ rel1 Press Enter |
I enter a as 8 and b as 3.
The output is displayed. We see that the output is same as the one in 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. | |
Now Let us fix the error.
Type an equal to sign. Click on Save | |
Switch back to the terminal
Compile and execute as before. The output is now correct. | |
Summary | Let us now 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. |