Uploaded by nammalwarsai

Session 12

advertisement
Department of BES-1
COMPUTATIONAL THINKING FOR
STRUCTURED DESIGN
Topic:
Nested if-else, Switch
Session - 12
CREATED BY K. VICTOR BABU
AIM OF THE SESSION
•
To familiarize students with the Nested if-else, Switch statement.
INSTRUCTIONAL OBJECTIVES
This Session is designed to:
•
•
•
Describe the use of conditional statements like nested if else, switch cases
Demonstrate the use of decision making statements using nested if else and switch cases
Practical implementation of nested if else statements and switch cases
LEARNING OUTCOMES
At the end of this session, you should be able to:
• Learn the procedure of execution of nested if else and switch cases statements.
•Solving the problem by using different nested if else and switch statements.
•Get clear logic about nested if else and switch statements.
CREATED BY K. VICTOR BABU
INTRODUCTION
This Session will discuss about …
• Nested if else and switch case statements.
•Different programs to get clear idea about nested if else and switch cases
statements.
•Programs for implementation and execution of nested if else and switch cases
statements.
CREATED BY K. VICTOR BABU
Session Description
Nested if-else statement:
In nested if else statement an if statement present inside another if statement. First of all outer if statement is checked, if the condition found true ,then the
control will enter inside the outer if block. Then control will check inner if expression, check the condition if it is again found true then execution of inner
if block starts, otherwise the else block associated with inner if statement will be executed. Again we have to remember a point that if the outer if
statements itself found false, then the control will flow into outer else block.
Syntax:
if( condition )//outer if statement execute only if condition found true
{
if(Condition)//inner if statement execute only inner if condition found true
{
Statement1;
Statdement2;
}
else //inner else statement of inner if execute only inner if condition found false
{
Statement3;
Statement4;
}
}
else //outer else statement of outer if execute only outer if condition found false
{
Statement5;
Statement6;
}
CREATED BY K. VICTOR BABU
Flow chart of nested if else statement
Session Description
Switch Statements in C
Switch is a decision control statement. It has expression part and case value parts. The value of each case statement
must be unique. At first logical expression will be evaluated and the result of that will be compared with each case
present inside switch statement. If any case found matched then block of statements associated with that case will be
executed. There is also a default statement present inside switch statements, it will be executed if and only if all the
case fells to execute.
Switch Case statement works on menu driven paradigm.
It is a substitution of if else if ladder.
Inside case statement we can take only int and char data type only.
There is no limit to case statements and the values of each case statement must be unique one.
Each of the case statement has a break statement, but we remember that it is optional one.
CREATED BY K. VICTOR BABU
Session Description
Program
EXAMPLE:
Flow chart for check whether a year is leap year or not: #include <stdio.h>
int main()
{
int y;
printf(“enter the year to check leap year ?”);
scanf(“\n%d”,&y);
if(y % 4 == 0)
{ if(y % 100 == 0)
{ if(y % 400 == 0)
{
printf("enter year is a leap year");
}else{
printf("enter year is not a leap year");}
}else{
printf("enter year is a leap year");
}
}else{
printf("enter year is not a leap year");
}
}
CREATED BY K. VICTOR BABU
Session Description
Example: Flow chart for finding week day name based
on day number
Flow chart for switch case statement
CREATED BY K. VICTOR BABU
ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION
1. C program on switch case to find out week day name based on week day number(e.g.1-Sunday,2Monday …)
2. C program to determine whether the given number is even or odd and if it is even,whether it is divisible
by 4 or not,and if it is odd ,whether it is divisible by 7 or not.
3. C program on switch case to perform basic arithmetic operations like *,/,%,+,- etc. based on character
enter by the user
4. BMI Calculation: A fitness trainer wants to calculate the Body Mass Index (BMI) of their clients. The
BMI is calculated using the formula BMI = weight / (height * height), where weight is in kilograms and
height is in meters. The BMI can be used to determine whether a person is underweight, normal,
overweight, or obese. If the BMI is less than 18.5, the person is underweight. If the BMI is between 18.5
and 24.9, the person is normal. If the BMI is between 25 and 29.9, the person is overweight. If the BMI is
greater than or equal to 30, the person is obese. Write a C program that takes the weight and height of a
person as input and calculates the BMI using an if-else-if ladder. Then, it should print whether the person
is underweight, normal, overweight, or obese based on their BMI.
CREATED BY K. VICTOR BABU
EXAMPLES
1. “Switch Case in C Programming: A Comprehensive Guide”: in this article how the controltransfer from one case to
another case based on the expression. There may be any number of cases that a programmer wants. We can replaced
nested if else statements with the switch statements. We get a clear idea about the execution,transfer of control flows
about switch case statements. We also get the clear idea about break statements, continue statements and differences
between them.
2. “if else in c, some important programming exercises”:In this article we will learn how the decision making statements
work based on the expression provided inside the if expression.the control will transfer from one statement to other
statement until the condition becomes true.
CREATED BY K. VICTOR BABU
SUMMARY
This Session discussed about …
1. The control flow based on the conditional statements true
or false.
2. Differentiate between switch case statement and nested if
else if statements.
CREATED BY K. VICTOR BABU
A. Compilation Error
B. Runtime Error
C. welcomes to c class
D. practice again
SELF-ASSESSMENT QUESTIONS
1.Find out the output of the following program:
#include<stdio.h>
Int main()
{
int j = 3;
if(j == (5, 3))
printf("welcomes to c class");
else
printf("practice again");
return 0;
}
A. Compilation Error
B. Runtime Error
C. welcomes to c class
D. practice again
CREATED BY K. VICTOR BABU
SELF-ASSESSMENT QUESTIONS
2. Find out the output of the following program:
#include<stdio.h>
int main()
{Int i = 1;
switch(i)
{
default:
printf("Build your career here");
case 1:
printf("welcomes to KLU ");
}
return 0;
}
A. Compilation Error
B. Build your career here
C. Welcomes to KLU
D. Welcomes to KLU Build your career here
CREATED BY K. VICTOR BABU
TERMINAL QUESTIONS
1. C program to check whether a character is VOWEL or CONSONANT using
switch.
2. C program to find number of days in a month using switch case.
CREATED BY K. VICTOR BABU
REFERENCES FOR FURTHER LEARNING OF THE SESSION
Reference Books:
1. Mark Allen Weiss, Data Strucutres and Algorithm Analysis in C, 2008, 3rd Edition, Pearson
Education.
2. Horowitz, Sahni, Anderson Freed, “Fundamentals of Data Structures in C”, 2nd Edition – 2007.
3.
Robert Cruse, C.L.Tondo, Bruce Leung, Shashi Mogalla, “Data Strctures and Program Design in
C”, 4th Edition-2007
Sites and Web links:
1. www.codechef.com
2. www.spoj.com
CREATED BY K. VICTOR BABU
THANK YOU
Team – CTSD
CREATED BY K. VICTOR BABU
Download