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

From Script | Spoken-Tutorial
Revision as of 12:18, 3 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 about, how to use,

nested if statement. And

switch statements in C and C++ program.

We will see this with the help of an examples.

Slide 3

System Requirements


To record this tutorial, I am using,

Ubuntu operating system version 11.10

gcc and g++ Compiler version 4.6.1 on Ubuntu.

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

//Program to study nested if-switch loops

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

#include <stdio.h>

This is our Header file.
Highlight:

int main()

Our main program starts from here.
Highlight:

int x, y;

Here, we have defined two integer variable x and y.
Highlight:

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

scanf("%d", &y);

Here we take the value of y as input from the user.

We prompt users to enter a number between the range of 1 to 29.

Highlight:

if(y/10==0)

{

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

}

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

If the condition is true

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

Highlight:

else if(y/10==1)

{

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

}

Here, we will check for another condition whether y is in the range of 10-19.

If the condition is true.

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

Highlight:

else if(y/10==2)

{

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

}

Now, we check another condition whether y is in the range of 20-29.

If the condition is true.

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



else

{

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

}

If all of the above conditions are false

We print number not in range.

Open the terminal. Let’s open the terminal.

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

Type:

gcc nested.c -o nested

Type

./output


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

space “nested”.

To execute the program,

type dot slash “./nested”

Press Enter.

Type: 12 We see,

Enter an integer between 1 to 29.

Let's enter 12.

Now Press Enter.

Highlight

Output

The output is:

you have entered number between 10-19.

Type: 35 Let's execute it again. Press the up arrow key, press enter.

Let's enter 35 this time.

Highlight

Output

The output is

number not in range.

The conditional execution can also be done in another way.

By using switch statement.

Let’s see how it is done.

Open switch.c


Let's see the same program using switch.

I have already opened the program.

Let's see how it works.
I have already written the code. Let's take a look.
Highlight

x=y/10;

Here, we divide the inputs by 10 and the result will be stored in x.

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 1:

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

If case 0 is satisfied.

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



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

We break the loop each time.

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

Highlight:

case 2:

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

break;

“case 1” means “if the value of x is 1”

If case 1 is satisfied.

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



Highlight:

case 3:

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

break;

“case 2” means “if the value of x is 2”

If case 2 is satisfied.

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



Highlight:

default:

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

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

Here we print number not in range.

It is suggested to write the default case before any other case.

Open terminal.

Compile.

Let’s compile the program.

Coming back to the terminal.

Execute. Let's execute as before.
Type: 1 Here it is displayed as:

enter a number between the range of 1 and 30.

Let's enter 1.

Highlight

output

We get the output as,

“you have entered the number between the range of 1 to 10”.

It is working same as the nested if program.

Now let’s execute the program in C++.
I will open the program and explain the code.
#include<iostream> We can see the header file is different.
Type:

using namespace std;

We are using standard namespace.



Change printf with cout<<

and scanf with cin

at all places

We have cout and cin function.
Click on Save Now Save the program.
Open terminal.

Type:

g++ nested.cpp -o nested2

Type:

./nested2

Let’s execute the program.

Come back to the terminal.

Type: 2 Here we see enter a number between 1 and 30.

Let's enter 20.

Here it is displayed as:

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

Open gedit Now let’s check the nested-if part.

Here is the nested-if program in C++.

There are basic changes like header file, cout function and cin function.

We can see that the syntax for nested-if statement is similar in both C and C++.

Save it.

Press the up arrow, press enter.

Save the program.

Come back to the terminal.

And compile and execute it as before.

Type: 3 Enter a number between 1 and 30.

We enter 15.

The output is

“you have entered number in the range of 10-19”.

Press up arrow,

Enter 10

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.

Now let us switch back to our slides.

Slide 5


Comparison between switch

and if.


The nested- if and switch do the same work.

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

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 if we have to write the conditional statement for each value of the variable.

Slide 6

Comparison between switch

and if.

Switch statement can only check the integer values

Nested if can check for both integer and fractional values.

Summary In this tutorial we learnt,

nested if statement.

Ex: else if(x<=20 || x>10)

switch statement.

Difference between nested-if and switch statements.

Slide 4

Assignment

Coming back to the slides,

As an assignment,

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

Slide 7

About the Spoken Tutorial Project


Watch the video available at the link shown

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

It summarizes the Spoken Tutorial project

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

Slide Number 8

Spoken Tutorial Workshops


The Spoken Tutorial Project Team,

Conducts workshops using spoken tutorials.

Gives certificates to those who pass an online test.

For more details, contact spoken-tutorial.org

Slide Number 9


Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

This is Chaitanya Mokashi from IIT Bombay signing off.

Thank you for joining.

Contributors and Content Editors

Ashwini