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…')
 
 
(17 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
| 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>
 
| 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>
 
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
 
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
 +
  
 
|-
 
|-
 
| 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;"| Slide 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;"| Slide 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;"| Welcome to the spoken tutorial on Nested if & Switch statements in C and 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;"| Welcome to the spoken tutorial on '''Nested if & Switch statements in C and C++.'''
  
 
|-
 
|-
Line 19: Line 20:
  
 
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, how to use,
  
nested if statement. And
+
nested if statement.  
  
switch statements in C and C++ program.
+
And
  
We will see this with the help of an examples.
+
switch statements.
 +
 
 +
We will do this with the help of some examples.
  
 
|-
 
|-
Line 31: Line 34:
  
 
System Requirements
 
System Requirements
 
 
  
 
| 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 record this tutorial, I am using,
 
| 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 record this tutorial, I am using,
Line 42: Line 43:
 
|-
 
|-
 
| 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;"| First we will learn, how to write nested if and switch loops with an example.
+
| 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;"| First we will learn, how to write nested if and switch statement with an example.
  
 
|-
 
|-
 
| 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 58:
 
'''//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 73:
  
 
'''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 79:
  
 
'''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.
  
 
|-
 
|-
 
| 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:
  
'''printf ("Enter an integer between the range of 1 to 29\n");'''
+
'''printf ("Enter a number between the range of 0 to 39\n");'''
  
 
'''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 93: Line 98:
 
'''{'''
 
'''{'''
  
'''printf("you have entered number between the range of 1-9\n");'''
+
'''printf("you have entered number between the range of 0-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;"| 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 whether '''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 in the range of 0-9.'''
  
 
|-
 
|-
Line 112: Line 119:
  
 
'''}'''
 
'''}'''
| 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 137:
  
 
'''}'''
 
'''}'''
| 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 '''20 to 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 to 39'''
  
 
|-
 
|-
Line 145: Line 160:
  
 
'''}'''
 
'''}'''
| 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;"|Now Let us execute the program.
  
By pressing '''Ctrl+Alt+T '''simultaneously on your keyboard.
+
Please open the terminal window.
 +
 
 +
By pressing '''Ctrl+Alt and T keys '''simultaneously on your keyboard.
  
 
|-
 
|-
 
| 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 space nested-if.c space -o space nested'''
  
 +
Press '''Enter'''
 
Type
 
Type
  
./output
+
'''./nested'''
 +
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;"| 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 '''“-o”'''
  
 
space '''“nested”.'''
 
space '''“nested”.'''
  
To execute the program,
+
Press '''Enter'''
  
 
type dot slash '''“./nested”'''
 
type dot slash '''“./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 a number between 0 to 39.
 
+
Let's enter 12.
+
  
Now Press '''Enter.'''
+
I will enter 12.
  
 
|-
 
|-
Line 190: Line 221:
  
 
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;"| The output is displayed as:
  
you have entered number between 10-19.
+
'''you have entered the number 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: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 240:
  
 
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 in the range of 0-9.'''
  
 
|-
 
|-
Line 221: Line 257:
  
  
| 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 263:
 
|-
 
|-
 
| 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 275:
  
 
'''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 294:
 
| 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.
 +
 
 +
Then we print '''you have entered the number in the range of 0-9.'''
  
  
Line 268: Line 310:
 
| 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 334:
 
| 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.
+
Here 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 30-39.'''
  
 
|-
 
|-
Line 312: Line 360:
  
 
'''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.
  
Here we print number not in range.
+
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.
  
It is suggested to write the default case before any other case.
 
  
 
|-
 
|-
Line 322: Line 376:
  
 
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 '''“-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 0 to 39
  
enter a number between the range of 1 and 30.
+
I will enter 35.
 
+
Let's enter 1.
+
  
 
|-
 
|-
Line 342: Line 409:
  
 
'''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 30 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 435:
  
 
'''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 446:
  
 
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.
| 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;"| Click on '''Save'''
+
| 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 '''Save''' the program.
+
  
 
|-
 
|-
Line 391: Line 460:
  
 
'''./nested2'''
 
'''./nested2'''
| 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 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 code.
  
 
Come back to the terminal.
 
Come back to the terminal.
 +
 +
Type:
 +
 +
'''g++ space nested-if.cpp space -o space 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 to 39'''
  
Let's enter '''20.'''
+
I will enter '''40'''
  
 
|-
 
|-
 
| 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's see the switch program in c++
 +
 
 +
Come back to our 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.
  
We can see that the syntax for nested-if statement is similar in both '''C''' and '''C++.'''
+
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.
  
 
|-
 
|-
| 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;"| Save it.
+
| 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.
  
Press the up arrow, press enter.
+
''' Type
| 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.
+
'''g++ space switch.cpp space -o space switch1'''
 +
'''Press Enter'''
 +
'''./switch1'''
 +
'''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 execute.
 
Come back to the terminal.
 
Come back to the terminal.
  
And compile and execute it as before.
+
Type:
 +
 
 +
'''g++ space switch.cpp space -o space 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 to 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 switch and nested-if statement.
  
 
|-
 
|-
Line 461: Line 560:
  
  
| 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 572:
  
 
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;"|Let us summarise. 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)
 +
And
  
 
Difference between nested-if and switch statements.
 
Difference between nested-if and switch statements.
Line 497: Line 599:
  
 
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;"|As an assignment,
  
As an assignment,
+
Write a program to check whether the age of the employee is between 20-60  
 
+
Write a program to check whether the age of the employee is between 20 to 60.
+
  
 
|-
 
|-
Line 508: Line 608:
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
  
 
+
| 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;"| Watch the video available at the link shown below
 
+
| 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;"| Watch the video available at the link shown
+
  
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
Line 522: Line 620:
  
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
 
  
  
Line 531: Line 628:
 
Gives certificates to those who pass an online test.
 
Gives certificates to those who pass an online test.
  
For more details, contact spoken-tutorial.org
+
For more details,please write to contact@spoken-tutorial.org
  
 
|-
 
|-
Line 542: Line 639:
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India
 
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/ http://spoken-tutorial.org]\NMEICT-Intro
+
More information on this Mission is available at the link shown below: [http://spoken-tutorial.org/ http://spoken-tutorial.org]\NMEICT-Intro
  
 
|-
 
|-
 
| 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.
  
 
|}
 
|}

Latest revision as of 15:34, 31 January 2014

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, how to use,

nested if statement.

And

switch statements.

We will do this with the help of some 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 statement 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 a number between the range of 0 to 39\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 0-9\n");

}

This is our if condition.

Here we check whether y/10==0.

If the condition is true

We print "you have entered number in 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 to 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 to 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. Now Let us execute the program.

Please open the terminal window.

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

Type:

gcc space nested-if.c space -o space nested

Press Enter Type

./nested Press Enter


To execute

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

space “nested”.

Press Enter

type dot slash “./nested”

Press Enter.

Type: 12 We see,

Enter a number between 0 to 39.

I will enter 12.

Highlight

Output

The output is displayed as:

you have entered the number in the range of 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 in the range of 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.

Then we print you have entered the 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.

Here 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 30-39.

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 “-o”

space “switch”.

Press Enter

Type: dot slash “./switch”

Press Enter

Type: 1 enter a number between 0 to 39

I will enter 35.

Highlight

output

The output is displayed as,

“you have entered the number between the range of 30 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.

Open terminal.

Type:

g++ nested.cpp -o nested2

Type:

./nested2

Let us execute the code.

Come back to the terminal.

Type:

g++ space nested-if.cpp space -o space nested1

Press Enter

Type:

./nested1

Press Enter

Type: 2 enter a number between 0 to 39

I will enter 40

The output is displayed as:

number not in range.

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

Come back to our 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.

Type g++ space switch.cpp space -o space switch1 Press Enter ./switch1 Press Enter

Let us execute.

Come back to the terminal.

Type:

g++ space switch.cpp space -o space switch1

Press Enter

Type:

./switch1

Press Enter

Type: 3 enter a number between 0 to 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 switch and nested-if 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 Let us summarise. In this tutorial, we learnt,

nested if statement.

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

switch statement.

Ex: switch(x) And

Difference between nested-if and switch statements.

Slide 4

Assignment

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 below

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,please write to 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 the link shown below: 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