Difference between revisions of "C-and-C++/C2/Logical-Operators/English"
(Created page with ''''Title of script''': Logical Operators in C and C++ '''Author:Ritwik Joshi ''' '''Keywords: Logical Operators, Video Tutorial''' {| style="border-spacing:0;" | style="bord…') |
|||
Line 17: | Line 17: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2 | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about: | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about: | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- |
Revision as of 12:42, 3 May 2013
Title of script: Logical Operators in C and C++
Author:Ritwik Joshi
Keywords: Logical Operators, Video Tutorial
|
| |
Slide 1 | Welcome to the spoken tutorial on Logical operators in C and C++. | |
Slide 2 | In this tutorial we will learn about: | |
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
|
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 Non zero = True zero means false Zero = 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
|
Inside the main block
This statement declares the variables a,b and c as integers. | |
Highlight
and c \n"); |
The printf statement prompts the user to enter the values of a,b and c. | |
Highlight
&c); |
The scanf statement takes input from the user for the variables a, b and c. | |
Highlight
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
|
Next (b>c) is evaluated.
| |
Highlight
|
If the condition is true, then b is greatest is displayed on the screen. | |
Highlight
printf("c is greatest \n"); |
Otherwise c is greatest is displayed on the screen. | |
Highlight
|
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
and c is zero \n");
|
This printf statement is executed if either of a, b or c is 0. | |
Highlight
} |
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 logical.c -o log Type ./log |
To compile the code type
gcc logical.c -o log For executing the code type ./log Press Enter. | |
Output: | Here it is displayed as,
Enter the values of a,b and c I will enter the values as, 0 34 567 The output is displayed as, c is greatest. The product of a, b and c is zero. You should try executing this program with different sets of inputs. | |
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. 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 -o log1 to execute the program type ./log1 press Enter. | |
Output: | Here it is displayed as,
Enter the values of a,b and c I will enter the values as 0 34 567 We see the output is similar to the C program. You should try executing the program with different sets of inputs too. | |
Errors
|
Now let us see an error which we can come across.
Let's switch to the editor. | |
Delete
|
Suppose here we forgot the brackets.
Delete this and this. | |
Let see what will happen
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. | ||
Let us compile and execute as before
So it is working now. Let us now summarize the tutorial. | ||
Summary: | In this tutorial we learnt about the logical operators
&& Logical AND eg. ((a > b) && (a > c))
|
Logical OR
eg. (a == 0 || b == 0 || c == 0)
|
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
|
* Watch the video available at the following link
| |
Slide 6
Spoken Tutorial Workshops The Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team * Conducts workshops using spoken tutorials
| |
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
| |
Remain on previous slide
No slide for this part |
This is Ritwik Joshi from IIT Bombay.
Thank you for joining. |