C-and-C++/C2/Nested-If-And-Switch-Statement/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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