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

From Script | Spoken-Tutorial
Jump to: navigation, search
(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…')
 
 
(4 intermediate revisions by 2 users not shown)
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:
 
+
*Logical Operators like
Logical operators like
+
*Logical AND
 
+
** Eg- '''expression1 && expression2'''
&& Logical AND
+
* Logical OR
 
+
** Eg- '''expression1 || expression2'''
eg. expression1 && expression2
+
* Logical NOT
 
+
** Eg- '''!(expression1)
|| Logical OR  
+
'''
 
+
*We will do these with the help of examples.
eg. expression1 || expression2
+
 
+
! Logical NOT
+
 
+
eg. !(Expression1)
+
 
+
We will do this with the help of examples.
+
  
 
|-
 
|-
Line 52: Line 45:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to the '''logical operators'''.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to the '''logical operators'''.
  
In '''C '''and '''C++,''' '''true''' is any value other than 0.
+
In '''C '''and '''C++,''' '''true''' is any value, other than 0.
  
non zero means true
+
Non-zero means''' true'''.
  
'''Non zero = True'''
+
Zero means '''false'''
 
+
zero means false
+
 
+
'''Zero = False'''
+
  
  
Line 78: Line 67:
  
 
'''int a,b,c; '''
 
'''int a,b,c; '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Inside the main block
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Inside the main block, this statement declares the variables '''a''','''b''' and '''c''' as '''integers'''.
 
+
This statement declares the variables '''a''','''b''' and '''c''' as integers.
+
  
 
|-
 
|-
Line 107: Line 94:
  
 
'''printf("a is greatest \n"); '''
 
'''printf("a is greatest \n"); '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, We are comparing the values of '''a''' with '''b''' and '''c''' to find the greatest.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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.
+
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.
+
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.
+
The expression is not evaluated further on encountering a '''false''' condition.
  
So, the expression '''(a>c) '''is evaluated only if '''(a>b)''' is true.
+
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.
+
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.  
+
This statement is be evaluated if the previous condition is '''true'''.  
  
 
|-
 
|-
Line 127: Line 114:
 
'''else if (b > c) '''
 
'''else if (b > c) '''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next '''(b>c) '''is evaluated.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next '''(b>c) '''is evaluated.
 
 
  
  
Line 139: Line 124:
  
  
| 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 '''b is greatest''' is displayed on the screen.
+
| 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 '''b is greatest''' is displayed on the screen.
  
 
|-
 
|-
Line 160: Line 145:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We now come to the '''logical OR '''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 come to the '''logical OR '''operator.
  
Here, any one of the conditions has to be true for '''logical OR '''to return a true value.
+
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.
+
The expression is not evaluated further on encountering a '''true''' condition.
  
So, if '''a''' == zero, then the remaining two expressions won't be evaluated.
+
So, if '''a == zero''', then the remaining two expressions won't be evaluated.
  
 
|-
 
|-
Line 176: Line 161:
  
  
| 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 either of '''a''', '''b''' or '''c''' is 0.
+
| 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, either of '''a''', '''b''' or '''c''' is 0.
  
 
|-
 
|-
Line 196: Line 181:
  
 
I have saved my file as '''logical.c'''
 
I have saved my file as '''logical.c'''
 
 
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| press '''Ctrl, Alt and T '''keys''' '''simultaneously.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| press '''Ctrl, Alt and T '''keys''' '''simultaneously.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the terminal by pressing '''Ctrl, Alt and T '''keys simulataneously.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the '''terminal''' by pressing '''Ctrl, Alt and T '''keys simulataneously.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
  
'''gcc logical.c -o log'''
+
'''gcc space logical.c space -o space log'''
 +
'''Press Enter'''
  
 
Type
 
Type
  
 
'''./log'''
 
'''./log'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the code type
+
'''Press Enter'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the code, type
  
'''gcc logical.c -o log'''
+
'''gcc space logical.c space -o space log'''
  
For executing the code type
+
Press '''Enter.'''
  
'''./log'''
+
To execute, type '''./log'''
  
 
Press '''Enter.'''
 
Press '''Enter.'''
Line 224: Line 208:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output:
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here it is displayed as,
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will enter the values as 0 34 and 567.
 
+
Enter the values of a,b and c
+
 
+
I will enter the values as,
+
 
+
0  
+
 
+
34  
+
 
+
567  
+
  
The output is displayed as,
+
The output is displayed as here,
  
c is greatest.  
+
'''c is greatest.'''
  
The product of a, b and c is zero.
+
And '''The product of a, b and c is zero.'''
  
 
You should try executing this program with different sets of inputs.
 
You should try executing this program with different sets of inputs.
Line 246: Line 220:
 
|-
 
|-
 
| 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's write the same program in C++
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, let's write the same program in '''C++'''
  
 
|-
 
|-
Line 294: Line 268:
  
 
}  
 
}  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now to make the same program in C++, we make a few changes.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now to make the same program in''' C++''', we make a few changes.
  
 
There's a change in the header file.
 
There's a change in the header file.
  
Using statement has been used.
+
The '''using''' statement has been used.
  
 
Also there is a difference in output and input statements.
 
Also there is a difference in output and input statements.
  
The operators behave in the same way as they did in C.
+
The operators behave in the same way as they did in '''C'''.
 
+
 
+
  
  
Line 317: Line 289:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| press '''Ctrl, Alt and T '''keys''' '''simultaneously.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| press '''Ctrl, Alt and T '''keys''' '''simultaneously.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the terminal by pressing '''Ctrl, Alt and T '''keys simulataneously.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Open the '''terminal''' by pressing '''Ctrl, Alt and T '''keys simulataneously.
  
 
|-
 
|-
Line 327: Line 299:
  
 
'''./log'''
 
'''./log'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the program, type
  
'''g++ logical.cpp -o log1'''
+
'''g++ logical.cpp space -o space log1'''
  
to execute the program type
+
Press '''Enter.'''
 +
 
 +
To execute, type
  
 
'''./log1'''
 
'''./log1'''
  
press '''Enter.'''
+
Press '''Enter.'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output:
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here it is displayed as,
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|I will enter the values as 0 34 and 567.
  
Enter the values of a,b and c
+
So we see the output is similar to the '''C''' program.
  
I will enter the values as
+
You should try executing this program with different sets of inputs too.
 
+
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.
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Errors'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Errors'''
 
  
  
 
| 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.
  
Let's switch to the editor.
+
Let us switch back to the editor.
  
 
|-
 
|-
Line 369: Line 332:
  
 
'''if(a > b) && (a > c)'''
 
'''if(a > b) && (a > c)'''
 
 
  
 
| 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 forgot the brackets.
 
| 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 forgot the brackets.
Line 378: Line 339:
 
|-
 
|-
 
| 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 see what will happen
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see what will happen.  Save the program.
  
Come back to the terminal
+
Come back to the '''terminal'''.
  
Compile and execute as before
+
Compile and execute as before.
  
 
|-
 
|-
Line 388: Line 349:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the error:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the error:
  
Expected identifier before '(' token.
+
'''Expected identifier before '(' token.'''
  
 
|-
 
|-
 
| 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;"| This is because we have two different expressions here
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because we have two different expressions here.
  
We have to evaluate them as one expression using AND operator.
+
We have to evaluate them as one expression using '''AND''' operator.
  
 
|-
 
|-
 
| 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;"| Now let us go back to our program and 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 go back to our program and fix the error.
  
 
|-
 
|-
Line 404: Line 365:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us insert the brackets here and here.  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us insert the brackets here and here.  
  
Click on '''Save'''
+
Click on '''Save'''.
  
Come back to the terminal.
+
Come back to the '''terminal'''.
  
 
|-
 
|-
 
| 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 compile and execute as before
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Compile and execute as before.
  
So it is working now.
+
So, it is working now.
  
 
Let us now summarize the tutorial.
 
Let us now summarize the tutorial.
Line 418: Line 379:
 
|-
 
|-
 
| 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;"| In this tutorial we learnt about the logical operators
+
| 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 learnt about
 
+
*&& Logical AND  
&& Logical AND  
+
**eg. '''((a > b) && (a > c)) '''
 
+
*|| Logical OR
eg. '''((a > b) && (a > c)) '''
+
**eg. '''(a == 0 || b == 0 || c == 0) '''
 
+
 
+
|| Logical OR
+
 
+
eg. '''(a == 0 || b == 0 || c == 0) '''
+
 
+
 
+
  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Assignment:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Assignment:
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Write a program that takes two numbers as input from the user.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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.
 
Check whether the two numbers are equal or not using '''NOT''' operator.

Latest revision as of 16:34, 5 February 2014

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