Difference between revisions of "C-and-C++/C2/Relational-Operators/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(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 '''in '''Ubuntu.'''
+
*'''gcc''' and '''g++ Compiler '''version '''4.6.1 '''on '''Ubuntu.'''
 
+
|-
+
| 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 I will demonstrate the use of these operators using a '''C '''program.
+
  
 
|-
 
|-
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''' 0 for false and 1 for true.
+
Expressions using relational operators '''return 0''' for '''false''' and '''1''' for '''true'''.
 
+
'''Return''' values:
+
 
+
0 when '''False'''
+
 
+
1 when''' 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 this '''printf''' statement 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 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''' '''true''' when '''a''' is less than '''b'''.
+
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 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 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 these lines.
+
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;"| 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;"| I enter '''a''' as 8 and '''b '''as 3.
  
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 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 the code.
 
+
Let us now remove the comment from here.
+
  
I'll put the comment here.
+
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;"| 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;"|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;"| 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;"|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;"| Now we have the '''equal to '''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 '''equal to '''operator.
  
 
It is denoted by double '''equal (==) '''signs.
 
It is denoted by double '''equal (==) '''signs.
  
This operator '''returns''' '''true''' when both operands are equal to one another.
+
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;'''
+
'''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;"| 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;"|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 not equal to 3'''
 
+
8 is greater than or equal to 3
+
 
+
8 is not equal to 3  
+
  
So, we see how the relational operaotors work.
+
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 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'''.
  
Please make sure the file is saved with the extension '''.cpp'''
+
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'''
  
Let's execute this code and see what results we get.
+
To execute, type
 
+
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;"| 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;"|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:
+
We see that the output is same as the one in '''C''' program.
 
+
8 is greater than 3
+
 
+
8 is greater than or equal to 3
+
 
+
8 is not equal to 3
+
 
+
 
+
We see that the output is same as in the '''C''' code.
+
  
 
|-
 
|-
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 our 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
  
Let us compile and execute as before.
+
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's summarize the tutorial.
+
| 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


Visual Cue
Narration
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:
  • Ubuntu 11.10 as the operating system and
  • gcc and g++ Compiler version 4.6.1 on Ubuntu.
Slide 4


Return values:

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


int a,b;

First, we declare two variables a and b.
Highlight


printf("Enter the values of a and b\n");

This printf statement prompts the user to enter the values of a and b.


Highlight


scanf("%d %d",&a,&b);

This scanf statement takes input for the variables a and b.
Highlight


if(a > b)

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


printf("%d is greater than %d \n",a,b);

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


else if(a < b)

We now have the less than operator.

This, too, compares the operands.

It returns true when a is less than b.

Highlight


printf("%d is less than %d \n",a,b);

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


if(a <= b)

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


printf("%d is less than or equal to %d \n",a,b);

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


else if(a >= b)

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


printf("%d is greater than or equal to %d \n",a,b);

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


8 is greater than or equal to 3

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


if(a == b)

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


printf("%d is equal to %d \n",a,b);

This printf statement executes when a is equal to b.

If not, the control then jumps on to the next statement.

Highlight


else if (a != b)

Similarly, we have the not equal to operator.

This operator returns true when the operands are not equal to one another.

Highlight


printf("%d is not equal to %d \n”,a,b);

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


8 is not equal to 3

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


//Relational Operators in C++

#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>


using namespace std;


cout

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


g++ relational.cpp -o rel


./rel

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

  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


* Watch the video available at the following link
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


Slide 6

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact at spoken hyphen tutorial dot org


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
  • More information on this Mission is available at
  • http://spoken-tutorial.org/NMEICT-Intro


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
    More information on this Mission is available at
  • spoken hyphen tutorial dot org slash NMEICT hyphen Intro


Remain on previous slide

No slide for this part

This is Ritwik Joshi from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey