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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 8: Line 8:
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
! <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>
! <center>Narration</center>
+
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
 +
 
  
 
|-
 
|-
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.  
 
nested if statement.  
Line 33: 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 108: Line 107:
 
If the condition is true
 
If the condition is true
  
We print '''"you have entered number between the range of 0-9.'''
+
We print '''"you have entered number in the range of 0-9.'''
  
 
|-
 
|-
Line 200: Line 199:
 
| 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;"| To execute
  
Type '''“gcc”''' space '''“nested-if.c”''' space hyphen '''“-o”'''
+
Type '''“gcc”''' space '''“nested-if.c”''' space '''“-o”'''
  
 
space '''“nested”.'''
 
space '''“nested”.'''
Line 217: Line 216:
  
 
I will enter 12.
 
I will enter 12.
 
Now Press '''Enter.'''
 
  
 
|-
 
|-
Line 224: 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 a number between 10-19.'''
+
'''you have entered the number in the range of 10-19.'''
  
 
|-
 
|-
Line 234: Line 231:
 
Let's execute again.  
 
Let's execute again.  
  
Press the up arrow key, press enter.
+
Press the up arrow key, '''press enter.'''
  
 
I will give 5 this time.
 
I will give 5 this time.
Line 304: Line 301:
 
If case 0 is satisfied.
 
If case 0 is satisfied.
  
Then we print '''you have entered a number in the range of 0-9.'''
+
Then we print '''you have entered the number in the range of 0-9.'''
  
  
Line 313: 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 need to '''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.
Line 355: Line 352:
 
| 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.'''
 
| 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.'''
+
'''Here we check whether the number in the range of 30-39.'''
 
+
 
+
 
+
  
 
|-
 
|-
Line 371: Line 365:
  
 
Here we print '''number not in range'''.
 
Here we print '''number not in range'''.
 
 
  
  
Line 378: Line 370:
 
| 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: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.
 
| 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.
 
 
  
  
Line 399: Line 389:
 
| 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:
 
| 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”'''
+
'''“gcc”''' space '''“switch.c”''' space '''“-o”'''
  
 
space '''“switch”.'''
 
space '''“switch”.'''
Line 432: Line 422:
  
 
Note that our filename is '''nested-if.cpp'''
 
Note that our filename is '''nested-if.cpp'''
 
 
 
  
 
|-
 
|-
Line 451: Line 438:
  
 
using '''namespace std'''
 
using '''namespace std'''
 
 
  
  
Line 475: 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 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;"| Let us execute the code.
  
 
Come back to the terminal.
 
Come back to the terminal.
Line 507: Line 492:
 
| 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 the switch 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 let's the switch program in c++
  
Come back to the text editor.
+
Come back to our text editor.
 
+
 
+
  
  
Line 525: Line 508:
  
 
|-
 
|-
| 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.''' 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;"| '''Let us execute.
 +
 
 +
''' Type
 
'''g++ space switch.cpp space -o space switch1'''
 
'''g++ space switch.cpp space -o space switch1'''
 +
'''Press Enter'''
 
'''./switch1'''
 
'''./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;"| Come back to 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.
 +
Come back to the terminal.
  
 
Type:
 
Type:
Line 560: Line 548:
 
| 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.
 
| 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.
  
We will see the comparison between nested-if and switch statement.
+
We will see the comparison between switch and nested-if statement.
  
 
|-
 
|-
Line 572: 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;"| Switch statement is evaluated according to the result of the expression .
+
| 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.
  
 
Nested-if statement is run only if the result of the expression is true .
 
Nested-if statement is run only if the result of the expression is true .
Line 594: Line 582:
 
|-
 
|-
 
| 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.
Line 603: Line 591:
  
 
Ex: switch(x)
 
Ex: switch(x)
 +
And
  
 
Difference between nested-if and switch statements.
 
Difference between nested-if and switch statements.
Line 619: 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 633: Line 620:
  
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
 
  
  
Line 642: 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 653: 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
  
 
|-
 
|-

Revision as of 13:30, 30 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 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 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 an integer 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 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.

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 and 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 and 39

I will enter 40

The output is displayed as:

number not in range.

Open gedit Now let's 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 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 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