Professional Documents
Culture Documents
PF Lab Report 6
PF Lab Report 6
Name
Roll No:
Class:
Lab name:Programming Fundamental
Experiment No.6
Control Structures: Nested If & Switch Statements
Introduction:
Nested if:
'Nested if' in C++ is using more than one if statements in the same scope. The if statement is
a decision-making statement that allows taking decisions based upon the condition
specified. When there is more than one condition and they are dependent on one another,
then if statement can be nested. The nested if statement allows checking multiple
conditions one after another. Nested if statement is formed by using one if statement into
another. The number of using them is not limited but the inner if statement will only
execute when it’s outer if statement is true.
Switch statements:
The switch statement is a multiway branch statement. It provides an easy way to dispatch
execution to different parts of code based on the value of the expression. Switch is a control
statement that allows a value to change control of execution.
Switch case statements are a substitute for long if statements that compare a variable to
severalvalues. The basic format for using switch case is outlined below. The value of the
variable given into switch is compared to the value following each of the cases, and when
one value matches the value of the variable, the computer continues executing the program
from that point
Objectives:
(1)To provides an easy way to dispatch execution to different parts of code based on the
value of the expression.
(2)Nested loops are useful when for each pass through the outer loop, you need to repeat
some action on the data in the outer loop.
Procedure:
Nested loop:
An if statement can be followed by an optional else if...else statement, which is very useful
to test various conditions using single if...else if statement.
When using if , else if , else statements there are few points to keep in mind.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of he remaining else if's or else's will be tested.
if(boolean_expression 1)
elseif( boolean_expression 2)
}
elseif( boolean_expression 3)
else
Switch Case:
The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding code after the matching label is executed. For
example, if the value of the variable is equal to constant2, the code after case constant2: is
executed until the break statement is encountered.
Syntax:
switch (expression) {
case constant1:
// code to be executed if
break;
case constant2:
// code to be executed if
default:
// code to be executed if
Any year is input by the user. Write a program to determine whether the year is a leap year
or not.
Coding of exercise -1
#include<iostream>
int main ()
int year;
cout<<"Enter year"<<endl;
cin>>year;
else if (year%400==0)
else
Output:
Exercise-2
Write a C++ to perform arithmetic Operations using switch case.
1. Read two numbers ‘a’ and ‘b’ and 'Read your choice of operator ch.
4. If ch= ‘*’ then calculate mul = a * b and display the multiplication result.
5. If ch= ‘/’ then calculate div = a / b and display the division result.
Coding of Exercise-2:
#include<iostream>
int main ()
char operators;
cin>>operators;
int a ,b;
cout<<"Enter value of a=";
cin>>a;
cin>>b;
switch(operators){
case'+':
cout<<a<<"+"<<b<<"="<<a+b<<endl;
break;
case'-':
cout<<a<<"-"<<b<<"="<<a-b<<endl;
break;
case'*':
cout<<a<<"*"<<b<<"="<<a*b<<endl;
break;
case'/':
cout<<a<<"/"<<b<<"="<<a/b<<endl;
break;
case'%':
cout<<a<<"%"<<b<<"="<<a%b;
break;
default:
return 0;
Output :
Issues:
I was initallizing input numbers in float in exercise 2 .So in modulus operation it was giving
error.I intiallize them as int and my problem was excluded.
Application:
(1)to allow the value of a variable or expression to change the control flow of program
execution via search and map.
(2)Software of the ATM machine is in a loop to process transaction after transaction until
you acknowledge that you have no more to do.
(3)Software program in a mobile device allows user to unlock the mobile with 5 password
attempts.
Conclusion:
Nested loops are useful when for each pass through the outer loop, you need to repeat
some action on the data in the outer loop.
Post Lab
Exercise –2 (Use switch)
Write a program in C++ using if else operator to find the grade of a student.
else Grade F
Coding of Exercise:
#include<iostream>
int main ()
char grade;
int numbers;
cin>>numbers;
if(numbers<0 || numbers>100)
switch(numbers)
case 100:
case 90:
grade = 'A';
break;
case 75:
grade = 'B';
break;
case 60:
grade = 'C';
break;
case 45:
grade = 'D';
break;
default:
grade = 'F';
return 0;
Output: