Difference between revisions of "C-and-C++/C2/Nested-If-And-Switch-Statement/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script: Nested if-switch loops''' '''Author:''' Chaitanya Mokashi '''Keywords: If, Switch, Nested loops''' {| style="border-spacing:0;" | style="border-top:0.002…')
 
Line 8: Line 8:
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
| style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center>
+
! <center>Visual Cue</center>
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
+
! <center>Narration</center>
  
 
|-
 
|-
Line 19: Line 19:
  
 
Learning Objectives
 
Learning Objectives
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial we will learn about, how to use,
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial, we will learn about, how to use,
  
nested if statement. And
+
nested if statement.  
  
switch statements in C and C++ program.
+
And
 +
 
 +
switch statements.
  
 
We will see this with the help of an examples.
 
We will see this with the help of an examples.
Line 46: Line 48:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| I have already written the program in the text editor.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| I have already written the program.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let’s have a look at the program in detail
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let’s have a look.
  
 
|-
 
|-
Line 57: Line 59:
 
'''//Program to study nested if-switch loops'''
 
'''//Program to study nested if-switch loops'''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this program we will learn to check the range of integers.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this program we will learn to check the range of integers.
 +
 +
Note that our filename is nested-if.c
 +
 +
Let me explain the code now.
  
 
|-
 
|-
Line 68: Line 74:
  
 
'''int main()'''
 
'''int main()'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Our main program starts from here.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our main function.
  
 
|-
 
|-
Line 74: Line 80:
  
 
'''int x, y<nowiki>;</nowiki>'''
 
'''int x, y<nowiki>;</nowiki>'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here, we have defined two integer variable x and y.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Inside the main function we have declared two integer variables x and y.
  
 
|-
 
|-
Line 82: Line 88:
  
 
'''scanf("%d", &y);'''
 
'''scanf("%d", &y);'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we take the value of y as input from the user.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we prompt users to enter a number between the range of 0 to 39.
  
We prompt users to enter a number between the range of 1 to 29.
+
We take the value of y as input from the user.
  
 
|-
 
|-
Line 96: Line 102:
  
 
'''}'''
 
'''}'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we check the condition if y/10<nowiki>==0</nowiki>.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our if condition.
 +
 
 +
Here we check the condition if '''y/10==0.'''
  
 
If the condition is true
 
If the condition is true
  
We print '''"you have entered number between the range of 1-9.'''
+
We print '''"you have entered number between the range of 0-9.'''
  
 
|-
 
|-
Line 112: Line 120:
  
 
'''}'''
 
'''}'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here, we will check for another condition whether '''y''' is in the range of '''10-19.'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our else-if condition.
 +
 
 +
Here, we check that '''y/10==1.'''
  
 
If the condition is true.
 
If the condition is true.
  
We print '''you have entered number between the range of 10-19.'''
+
We print '''you have entered number in the range of 10-19.'''
  
 
|-
 
|-
Line 128: Line 138:
  
 
'''}'''
 
'''}'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now, we check another condition whether '''y''' is in the range of 2'''0-29.'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this else-if condition we check whether the number is in the range of 2'''0-29.'''
  
If the condition is true.
 
  
'''We print you have entered number between the range of 20-29.'''
 
  
  
 +
|-
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''else if(y/10==3) '''
  
 +
'''{ '''
 +
 +
'''printf("you have entered number between 30 to 39 \n"); '''
 +
 +
'''}'''
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And here we will see that the number is in the range of '''30-39'''
  
 
|-
 
|-
Line 145: Line 161:
  
 
'''}'''
 
'''}'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| If all of the above conditions are false
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our else condition.
 +
 
 +
If all of the above conditions are false
  
 
We print''' number not in range.'''
 
We print''' number not in range.'''
 +
 +
|-
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight
 +
 +
'''return 0;'''
 +
 +
'''}'''
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is our return statement.
 +
 +
 +
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal.
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let’s open the terminal.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute the program.
 +
 
 +
Please open the terminal window.
  
 
By pressing '''Ctrl+Alt+T '''simultaneously on your keyboard.
 
By pressing '''Ctrl+Alt+T '''simultaneously on your keyboard.
Line 158: Line 189:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type:
  
gcc nested.c -o nested
+
'''gcc nested-if.c -o nested'''
  
 
Type
 
Type
  
./output
+
'''./nested'''
 +
 
  
  
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To execute
  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type '''“gcc”''' space '''“nested.c”''' space hyphen '''“-o”'''
+
Type '''“gcc”''' space '''“nested-if.c”''' space hyphen '''“-o”'''
  
 
space '''“nested”.'''
 
space '''“nested”.'''
Line 180: Line 213:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We see,
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We see,
  
Enter an integer between 1 to 29.
+
Enter an integer between 0 to 39.
  
 
Let's enter 12.
 
Let's enter 12.
Line 192: Line 225:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The output is:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The output is:
  
you have entered number between 10-19.
+
'''you have entered a number between 10-19.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type: 35
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type: 35
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let's execute it again. Press the up arrow key, press enter.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us enter another number.
 +
 
 +
Let's execute again.  
 +
 
 +
Press the up arrow key, press enter.
 +
 
 +
I will give 5 this time.
 +
 
 +
 
  
Let's enter 35 this time.
 
  
 
|-
 
|-
Line 204: Line 244:
  
 
Output
 
Output
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The output is
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We see the output as
  
number not in range.
+
'''you have entered a number between 0-9.'''
  
 
|-
 
|-
Line 221: Line 261:
  
  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let's see the same program using '''switch'''.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We will see the same program using '''switch'''.
  
 
I have already opened the program.
 
I have already opened the program.
Line 227: Line 267:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let's see how it works.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let's switch back to our text editor.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| I have already written the code. Let's take a look.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| I have explained this in the previous program.
 +
 
 +
So I will move on to the switch statement.
  
 
|-
 
|-
Line 237: Line 279:
  
 
'''x=y/10;'''
 
'''x=y/10;'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here, we divide the inputs by 10 and the result will be stored in x.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here, we divide the inputs ie '''y/10.'''
 +
 
 +
And the result will be stored in the variable '''x.'''
  
The quotient will be stored in x.
+
That means the quotient will be stored in '''x'''.
  
 
With the help of the quotient we can identify the range of the number.
 
With the help of the quotient we can identify the range of the number.
Line 254: Line 298:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight:
  
'''case 1:'''
+
'''case 0:'''
  
 
'''printf("you have entered a number in the range of 1-9\n");'''
 
'''printf("you have entered a number in the range of 1-9\n");'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| If case 0 is satisfied.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is '''case''' '''0'''
  
We print '''you have entered a number in the range of 1-9.'''
+
If case 0 is satisfied.
 +
 
 +
We print '''you have entered a number in the range of 0-9.'''
  
  
Line 268: Line 314:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We add '''break '''to come out of the loop if the case is satisfied.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We add '''break '''to come out of the loop if the case is satisfied.
  
We '''break '''the loop each time.  
+
We need to '''break '''the loop each time.  
  
It is because only one condition can be true at a time.
+
It is because only one condition can be '''true''' at a time.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight:
  
'''case 2:'''
+
'''case 1:'''
  
 
'''printf("you have entered a number in the range of 10-20\n");'''
 
'''printf("you have entered a number in the range of 10-20\n");'''
  
 
'''break;'''
 
'''break;'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| “case 1” means “if the value of '''x '''is 1”
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is '''case 1.'''
  
If case 1 is satisfied.
+
'''case 1''' means, if the value of '''x '''is 1
  
 
We print '''you have entered a number in the range of 10-19.'''
 
We print '''you have entered a number in the range of 10-19.'''
Line 292: Line 338:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight:
  
'''case 3:'''
+
'''case 2:'''
  
 
'''printf("you have entered a number in the range of 20-30\n");'''
 
'''printf("you have entered a number in the range of 20-30\n");'''
  
 
'''break;'''
 
'''break;'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| “case 2” means “if the value of '''x '''is 2”
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is '''case 2.'''
 
+
If case 2 is satisfied.
+
  
 
We print '''you have entered a number in the range of 20-29.'''
 
We print '''you have entered a number in the range of 20-29.'''
 +
 +
 +
 +
 +
|-
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''case 3:'''
 +
 +
'''printf("you have entered a number in the range of 30-39\n");'''
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is '''case 3.'''
 +
 +
'''Here we check whether the number in the range of 20-29.'''
  
  
Line 312: Line 367:
  
 
'''printf ("number not in range\n");'''
 
'''printf ("number not in range\n");'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Default case specifies what needs to be done if none of the above cases are satisfied.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is the default case.
 +
 
 +
Default case specifies what needs to be done if none of the above cases are satisfied.
 +
 
 +
Here we print '''number not in range'''.
 +
 
 +
 
 +
 
 +
 
 +
|-
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| return 0;
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is our return statement.
 +
 
  
Here we print number not in range.
 
  
It is suggested to write the default case before any other case.
 
  
 
|-
 
|-
Line 322: Line 387:
  
 
Compile.  
 
Compile.  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let’s compile the program.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute the program.
  
Coming back to the terminal.
+
Switch back to the terminal.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Execute.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Execute.
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let's execute as before.
+
 
 +
 
 +
'''gcc switch.c -o switch'''
 +
 
 +
'''./switch'''
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type:
 +
 
 +
'''“gcc”''' space '''“switch.c”''' space hyphen '''“-o”'''
 +
 
 +
space '''“switch”.'''
 +
 
 +
'''Press Enter'''
 +
 
 +
Type: dot slash '''“./switch”'''
 +
 
 +
'''Press Enter'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type: 1
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type: 1
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here it is displayed as:
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| enter a number between the range of 0 and 39
  
enter a number between the range of 1 and 30.
+
I will enter 35.
 
+
Let's enter 1.
+
  
 
|-
 
|-
Line 342: Line 420:
  
 
'''output'''
 
'''output'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We get the output as,
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The output is displayed as,
  
“you have entered the number between the range of 1 to 10”.
+
“you have entered the number between the range of 0 to 39”.
 
+
It is working same as the nested '''if program.'''
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let’s execute the program in '''C++'''.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we will see how to execute the program in '''C++'''.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| I will open the program and explain the code.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Switch back to the text editor.
 +
 
 +
Note that our filename is '''nested-if.cpp'''
 +
 
 +
 
 +
 
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''<nowiki>#include<iostream></nowiki>'''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''<nowiki>#include<iostream></nowiki>'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We can see the '''header file''' is different.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here the logic and implementation are same.
 +
 
 +
There are a few changes like:
 +
 
 +
The header file as iostream in place of stdio.h
  
 
|-
 
|-
Line 364: Line 449:
  
 
'''using namespace std;'''
 
'''using namespace std;'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We are using standard '''namespace'''.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have included the using statement.
 +
 
 +
using '''namespace std'''
  
  
Line 375: Line 462:
  
 
at all places
 
at all places
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We have '''cout''' and '''cin''' function.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And the cout and cin function in place of printf and scanf.
 +
 
 +
You can see that the rest of the code is similar to our C program.
  
 
|-
 
|-
Line 394: Line 483:
  
 
Come back to the terminal.
 
Come back to the terminal.
 +
 +
Type:
 +
 +
'''g++ nested-if.cpp -o nested1'''
 +
 +
'''Press Enter'''
 +
 +
Type:
 +
 +
'''./nested1'''
 +
 +
'''Press Enter'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type: 2
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type: 2
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we see '''enter a number between 1 and 30.'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''enter a number between 0 and 39'''
  
Let's enter '''20.'''
+
Let's enter 4'''0.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here it is displayed as:
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The output is displayed as:
  
'''“you have entered number in the range of 10-20 ”.'''
+
'''number not in range.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open gedit
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open gedit
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let’s check the nested-if part.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let see the switch program in c++
 +
 
 +
Come back to the text editor.
 +
 
 +
 
  
Here is the nested-if program in C++.
 
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| There are basic changes like '''header file''', '''cout''' function and '''cin''' function.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here the logic and implementation are same.
 +
 
 +
You can see that the header file is iostream.
 +
 
 +
Here is the using statement.
 +
 
 +
And we have changed the cout and cin function.
  
We can see that the syntax for nested-if statement is similar in both '''C''' and '''C++.'''
+
Rest of the code is similar to our switch.c program.
 +
 
 +
|-
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Let us execute.'''
  
 
|-
 
|-
Line 423: Line 537:
  
 
Press the up arrow, press enter.
 
Press the up arrow, press enter.
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Save the program.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Come back to the terminal.
  
Come back to the terminal.
+
Type:
  
And compile and execute it as before.
+
'''g++ switch.cpp -o switch1'''
 +
 
 +
'''Press Enter'''
 +
 
 +
'''Type:'''
 +
 
 +
'''./switch1'''
 +
 
 +
'''Press Enter'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type: 3
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type: 3
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Enter a number between 1 and 30.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''enter a number between 0 and 39'''
  
We enter 15.
+
I will enter '''25.'''
  
The output is
+
The output is displayed as:
  
“you have entered number in the range of 10-19”.
+
'''“you have entered number in the range of 20-29”.'''
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Press up arrow,
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slides
  
Enter 10
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let’s execute it again, and enter '''40'''.
 
  
'''“The number is not in range.”'''
 
  
We can see that the output is same as the C program.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let us switch back to our slides.
  
Now let us switch back to our slides.
+
We will see the comparison between nested-if and switch statement.
  
 
|-
 
|-
Line 461: Line 580:
  
  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The nested- '''if '''and '''switch '''do the same work.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Switch statement is evaluated according to the result of the expression .
  
Switch statement is evaluated according to the result of the expression.
+
Nested-if statement is run only if the result of the expression is true .
  
If statement is run only if the result of the expression is true.
+
In switch we treat various values of the variable as cases  
 
+
In '''switch '''we treat various values of the variable as cases.
+
 
+
In '''if '''we have to write the conditional statement for each value of the variable.
+
  
 
|-
 
|-
Line 477: Line 592:
  
 
and if.
 
and if.
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Switch statement can only check the integer values
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In nested-if we have to write the conditional statement for each value of the variable .
  
Nested if can check for both integer and fractional values.
+
Switch statement can only check the integer values .
 +
 
 +
Nested-if can check for both integer and fractional values .
 +
 
 +
This brings us to the end of this tutorial.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Summary
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Summary
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial we learnt,
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial, we learnt,
  
nested if statement.
+
'''nested if''' statement.
  
<nowiki>Ex: else if(x<=20 || x>10)</nowiki>
+
Ex: else if(y/10==0)
  
switch statement.
+
'''switch''' statement.
 +
 
 +
Ex: switch(x)
  
 
Difference between nested-if and switch statements.
 
Difference between nested-if and switch statements.
Line 497: Line 618:
  
 
Assignment
 
Assignment
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Coming back to the slides,
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| You are already on slides.As an assignment,
 
+
As an assignment,
+
  
Write a program to check whether the age of the employee is between 20 to 60.
+
Write a program to check whether the age of the employee is between 20-60  
  
 
|-
 
|-
Line 546: Line 665:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is Chaitanya Mokashi from IIT Bombay signing off.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is Chaitanya MokashiScript has been contributed by Chaitanya Mokashi. This is Ashwini Patil.. from IIT Bombay signing off.
  
 
Thank you for joining.
 
Thank you for joining.
  
 
|}
 
|}

Revision as of 16:25, 26 September 2013

Title of script: Nested if-switch loops

Author: Chaitanya Mokashi

Keywords: If, Switch, Nested loops


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Nested if & Switch statements in C and C++.
Slide 2

Learning Objectives

In this tutorial, we will learn about, how to use,

nested if statement.

And

switch statements.

We will see this with the help of an examples.

Slide 3

System Requirements


To record this tutorial, I am using,

Ubuntu operating system version 11.10

gcc and g++ Compiler version 4.6.1 on Ubuntu.

First we will learn, how to write nested if and switch loops with an example.
I have already written the program.
Let’s have a look.
Highlight:

//Program to study nested if-switch loops

In this program we will learn to check the range of integers.

Note that our filename is nested-if.c

Let me explain the code now.

Highlight:

#include <stdio.h>

This is our Header file.
Highlight:

int main()

This is our main function.
Highlight:

int x, y;

Inside the main function we have declared two integer variables x and y.
Highlight:

printf ("Enter an integer between the range of 1 to 29\n");

scanf("%d", &y);

Here we prompt users to enter a number between the range of 0 to 39.

We take the value of y as input from the user.

Highlight:

if(y/10==0)

{

printf("you have entered number between the range of 1-9\n");

}

This is our if condition.

Here we check the condition if y/10==0.

If the condition is true

We print "you have entered number between the range of 0-9.

Highlight:

else if(y/10==1)

{

printf("you have entered number between the range of 10-19\n");

}

This is our else-if condition.

Here, we check that y/10==1.

If the condition is true.

We print you have entered number in the range of 10-19.

Highlight:

else if(y/10==2)

{

printf("you have entered number between 20 to 29 \n");

}

In this else-if condition we check whether the number is in the range of 20-29.



else if(y/10==3)

{

printf("you have entered number between 30 to 39 \n");

}

And here we will see that the number is in the range of 30-39
else

{

printf("you have entered wrong number\n");

}

This is our else condition.

If all of the above conditions are false

We print number not in range.

Highlight

return 0;

}

And this is our return statement.



Open the terminal. Let us execute the program.

Please open the terminal window.

By pressing Ctrl+Alt+T simultaneously on your keyboard.

Type:

gcc nested-if.c -o nested

Type

./nested


To execute

Type “gcc” space “nested-if.c” space hyphen “-o”

space “nested”.

To execute the program,

type dot slash “./nested”

Press Enter.

Type: 12 We see,

Enter an integer between 0 to 39.

Let's enter 12.

Now Press Enter.

Highlight

Output

The output is:

you have entered a number between 10-19.

Type: 35 Let us enter another number.

Let's execute again.

Press the up arrow key, press enter.

I will give 5 this time.



Highlight

Output

We see the output as

you have entered a number between 0-9.

The conditional execution can also be done in another way.

By using switch statement.

Let’s see how it is done.

Open switch.c


We will see the same program using switch.

I have already opened the program.

Let's switch back to our text editor.
I have explained this in the previous program.

So I will move on to the switch statement.

Highlight

x=y/10;

Here, we divide the inputs ie y/10.

And the result will be stored in the variable x.

That means the quotient will be stored in x.

With the help of the quotient we can identify the range of the number.

Highlight:

switch(x)

{

Here, we tell the switch command that the variable to be checked is x.
Highlight:

case 0:

printf("you have entered a number in the range of 1-9\n");

This is case 0

If case 0 is satisfied.

We print you have entered a number in the range of 0-9.



break; We add break to come out of the loop if the case is satisfied.

We need to break the loop each time.

It is because only one condition can be true at a time.

Highlight:

case 1:

printf("you have entered a number in the range of 10-20\n");

break;

This is case 1.

case 1 means, if the value of x is 1

We print you have entered a number in the range of 10-19.



Highlight:

case 2:

printf("you have entered a number in the range of 20-30\n");

break;

This is case 2.

We print you have entered a number in the range of 20-29.



case 3:

printf("you have entered a number in the range of 30-39\n");

And this is case 3.

Here we check whether the number in the range of 20-29.



Highlight:

default:

printf ("number not in range\n");

This is the default case.

Default case specifies what needs to be done if none of the above cases are satisfied.

Here we print number not in range.



return 0; And this is our return statement.



Open terminal.

Compile.

Let us execute the program.

Switch back to the terminal.

Execute.


gcc switch.c -o switch

./switch

Type:

“gcc” space “switch.c” space hyphen “-o”

space “switch”.

Press Enter

Type: dot slash “./switch”

Press Enter

Type: 1 enter a number between the range of 0 and 39

I will enter 35.

Highlight

output

The output is displayed as,

“you have entered the number between the range of 0 to 39”.

Now we will see how to execute the program in C++.
Switch back to the text editor.

Note that our filename is nested-if.cpp



#include<iostream> Here the logic and implementation are same.

There are a few changes like:

The header file as iostream in place of stdio.h

Type:

using namespace std;

Here we have included the using statement.

using namespace std



Change printf with cout<<

and scanf with cin

at all places

And the cout and cin function in place of printf and scanf.

You can see that the rest of the code is similar to our C program.

Click on Save Now Save the program.
Open terminal.

Type:

g++ nested.cpp -o nested2

Type:

./nested2

Let’s execute the program.

Come back to the terminal.

Type:

g++ nested-if.cpp -o nested1

Press Enter

Type:

./nested1

Press Enter

Type: 2 enter a number between 0 and 39

Let's enter 40.

The output is displayed as:

number not in range.

Open gedit Now let see the switch program in c++

Come back to the text editor.



Here the logic and implementation are same.

You can see that the header file is iostream.

Here is the using statement.

And we have changed the cout and cin function.

Rest of the code is similar to our switch.c program.

Let us execute.
Save it.

Press the up arrow, press enter.

Come back to the terminal.

Type:

g++ switch.cpp -o switch1

Press Enter

Type:

./switch1

Press Enter

Type: 3 enter a number between 0 and 39

I will enter 25.

The output is displayed as:

“you have entered number in the range of 20-29”.

Slides


Now let us switch back to our slides.

We will see the comparison between nested-if and switch statement.

Slide 5


Comparison between switch

and if.


Switch statement is evaluated according to the result of the expression .

Nested-if statement is run only if the result of the expression is true .

In switch we treat various values of the variable as cases

Slide 6

Comparison between switch

and if.

In nested-if we have to write the conditional statement for each value of the variable .

Switch statement can only check the integer values .

Nested-if can check for both integer and fractional values .

This brings us to the end of this tutorial.

Summary In this tutorial, we learnt,

nested if statement.

Ex: else if(y/10==0)

switch statement.

Ex: switch(x)

Difference between nested-if and switch statements.

Slide 4

Assignment

You are already on slides.As an assignment,

Write a program to check whether the age of the employee is between 20-60

Slide 7

About the Spoken Tutorial Project


Watch the video available at the link shown

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

It summarizes the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it.

Slide Number 8

Spoken Tutorial Workshops


The Spoken Tutorial Project Team,

Conducts workshops using spoken tutorials.

Gives certificates to those who pass an online test.

For more details, contact spoken-tutorial.org

Slide Number 9


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

This is Chaitanya MokashiScript has been contributed by Chaitanya Mokashi. This is Ashwini Patil.. from IIT Bombay signing off.

Thank you for joining.

Contributors and Content Editors

Ashwini