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

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Logical Operators in C and C++

Author:Ritwik Joshi

Keywords: Logical Operators, Video Tutorial


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Logical operators in C and C++.
Slide 2 In this tutorial we will learn about:
  • Logical Operators like
  • Logical AND
    • Eg- expression1 && expression2
  • Logical OR
    • Eg- expression1 || expression2
  • Logical NOT
    • Eg- !(expression1)

  • We will do these with the help of examples.
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


Non zero = True


Zero = False

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


Expressions using logical operators return 1 for true and 0 for 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


int a,b,c;

Inside the main block, this statement declares the variables a,b and c as integers.
Highlight


printf("Enter the values of a,b

and c \n");

The printf statement prompts the user to enter the values of a,b and c.
Highlight


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

&c);

The scanf statement takes input from the user for the variables a, b and c.
Highlight


if((a > b) && (a > c))

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


else if (b > c)

Next (b>c) is evaluated.


Highlight


printf("b is greatest \n");


If the condition is true, then b is greatest is displayed on the screen.
Highlight


else

printf("c is greatest \n");

Otherwise c is greatest is displayed on the screen.
Highlight


if(a == 0 || b == 0 || c == 0)


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


printf("The product of a, b

and c is zero \n");


This printf statement is executed, if, either of a, b or c is 0.
Highlight


return 0;

}

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 space logical.c space -o space log Press Enter

Type

./log Press Enter

To compile the code, type

gcc space logical.c space -o space log

Press Enter.

To execute, type ./log

Press Enter.

Output: I will enter the values as 0 34 and 567.

The output is displayed as here,

c is greatest.

And 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 file.

The 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 space -o space log1

Press Enter.

To execute, type

./log1

Press Enter.

Output: I will enter the values as 0 34 and 567.

So we see the output is similar to the C program.

You should try executing this program with different sets of inputs too.

Errors


Now let us see an error which we can come across.

Let us switch back to the editor.

Delete


if(a > b) && (a > c)

Suppose here we forgot the brackets.

Delete this and this.

Let us 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.

Compile and execute as before.

So, it is working now.

Let us now summarize the tutorial.

Summary: In this tutorial we learnt about
  • && Logical AND
    • eg. ((a > b) && (a > c))
  • || Logical OR
    • eg. (a == 0 || b == 0 || c == 0)


Assignment: 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

  • 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


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