Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 9

LAB REPORT

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.

Syntax:The syntax of an if...else if...else statement in C++ is:

if(boolean_expression 1)

// Executes when the boolean expression 1 is true

elseif( boolean_expression 2)

// Executes when the boolean expression 2 is true

}
elseif( boolean_expression 3)

// Executes when the boolean expression 3 is true

else

// executes when the none of the above condition is true.

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.

If there is no match, the code after default: is executed.

Syntax:

switch (expression) {

case constant1:

// code to be executed if

// expression is equal to constant1;

break;

case constant2:

// code to be executed if

// expression is equal to constant2;


break;

default:

// code to be executed if

// expression doesn't match any constant

Exercise attempted in lab:


Exercise-1

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>

using namespace std;

int main ()

int year;

cout<<"Enter year"<<endl;

cin>>year;

if( (year%4 ==0) && (year %100 != 0))

cout<<"It is a leap year"<<endl;

else if (year%400==0)

cout<<"It is a Leap Year";

else

{cout<<"It is not a leap year "<<endl;}


return 0;

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.

2. If ch = ‘+’ then calculate add = a + b and display the addition result.

3. If ch= ‘- ‘then calculate sub = a – b and display the subtraction result.

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.

6. If ch=’%’ then calculate mod = a % b and display the modulus result.

7. Otherwise display invalid operator.

Coding of Exercise-2:
#include<iostream>

using namespace std;

int main ()

char operators;

cout<<"Enter the operator:";

cin>>operators;

int a ,b;
cout<<"Enter value of a=";

cin>>a;

cout<<"Enter value of b=";

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:

cout<<" invalid operator.";

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.

(4)You put your favorite song on a repeat mode.

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.

In computer programming languages, a switch statement is a type of selection control


mechanism used to allow the value of a variable or expression to change the control flow of
program execution via search and map.

Post Lab
Exercise –2 (Use switch)
Write a program in C++ using if else operator to find the grade of a student.

The details are as follow

marks >= 90 Grade A


marks >= 75 Grade B

marks >= 60 Grade C

marks >= 45 Grade D

else Grade F

Coding of Exercise:
#include<iostream>

using namespace std;

int main ()

char grade;

int numbers;

cout<<"Enter the numbers:";

cin>>numbers;

if(numbers<0 || numbers>100)

cout << "Invalid Score" << endl;

return 0; // stop execution

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';

cout << "Grade = " << grade << endl;

return 0;

Output:

You might also like