C-and-C++/C2/Relational-Operators/English

From Script | Spoken-Tutorial
Revision as of 16:00, 30 January 2014 by Ashwini (Talk | contribs)

Jump to: navigation, search

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 2.

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