0% found this document useful (0 votes)
22 views

Lect 09 Nested Loop Switch

The document discusses different looping constructs in C++ including using a sentinel value to terminate a loop, nested loops, and switch statements. It also provides examples and homework problems related to these topics.

Uploaded by

uh2683505
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lect 09 Nested Loop Switch

The document discusses different looping constructs in C++ including using a sentinel value to terminate a loop, nested loops, and switch statements. It also provides examples and homework problems related to these topics.

Uploaded by

uh2683505
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lecture 10:

 Use of sentinel value : 5.8, 5.9


 Nested loops: 5.10
 Switch statement
 Break, continue, and goto statements

// use a special value (sentinel) to terminate the loop


// find the average marks of BSIT, BSCS, BSDS, and BSSE students. We have marks on two
sheets of paper and we aren’t able to count at the start of the writing loop. so we need
a loop that terminates with sentinel values
int main()
{
int counter = 0;
double sum = 0;
double marks = 0;
do
{
cout << "Enter the marks, -1 as a sentinel value";
cin >> marks;
sum += marks;
counter++;
} while (marks >= 0);

cout << "Average marks: << sum / counter << "\n";

return 0;
}

Home work1: Find some more elegant and efficient way to terminate the loop
Nested loop:

- Average test score


- Program asks for the no of students and the number of test scores per student.

int main()
{
cout << "This program average the test score \n";
int noOfStds = 0;
int noOfTests = 0;
double score = 0.0;
double total;
double average = 0.0;
cout << "For how many student do you have scores: ";
cin >> noOfStds;
cout << "How many test scores does each student have: ";
cin >> noOfTests;

for (int i = 0; i < noOfStds; i++)


{
total = 0;
for (int j = 0; j < noOfTests; j++)
{
cout << "please enter score "<< j + 1 << " for student " << i + 1
<<": ";
cin >> score;
total += score;

}
average = total / noOfTests;
cout << "Average score of student " << i + 1 << " is " << average << endl;
}

return 0;
}

Homework 2:

Homework 3:

Homework 4: simulate clock complete

Home work5: In a class of 15 students got the grade of each student, and then count how many
students got A, B, C, and D grades.
Homework 6: Write a program that reads an integer (5 digits or fewer) and determines and
prints how many digits in the integer are 7s.
Multiway decisions:

// multiway decisions
//1 only use if the condition
//2 use the nested if else
//3 use the if else if statement
//4 use the switch statement

char grade;
cout << "Enter the grades: ";
cin >> grade;
if (grade == 'A' || grade == 'a')
{
cout << "Excellent !";
}
if (grade == 'B' || grade == 'b')
{
cout << "very good !";
}

if (grade == 'C' || grade == 'c')


{
cout << "good !";
}
if (grade == 'D' || grade == 'd')
{
cout << "poor !";
}
if (grade == 'F')
{
cout << "Fail !";
}
if (grade == ‘E’ || grade >= 'F')
{
cout << "Enter the valid grade";
}

return 0;
}
Negative of just using if statement: It is very computationally expensive.

Solution: use the nested if-else or if else if statement.

3.

// if else if statement
//3 use the if else if statement
//4 use the switch statement

char grade;
cout << "Enter the grades: ";
cin >> grade;

if (grade == 'A' || grade == 'a')


{

cout << "Excellent !";


}

else if (grade == 'B' || grade == 'b')

{
cout << "very good !";

else if (grade == 'C' || grade == 'c')


{

cout << "good !";


}
else if (grade == 'D' || grade == 'd')
{
cout << "poor !";
}

else if (grade == 'F' || grade == 'f')


{
cout << "Fail !";
}

else
{
cout << "Enter the valid grade";
}

;;;;;;;;;;;;;;;;;

//4 use the switch statement:

- make the program more readable and elegant


Syntax:
}
Exmaple :
int main()
{
char grade;
printf("Enter the grades: ");
scanf("%c", &grade);
switch (grade)
{
case 'A':
case 'a':
printf("Excellent! \n");
break;
case'B':
case'b':
printf("very good !\n");
break;
case 'C':
case 'c':
printf("good !\n");
break;
case 'D':
case 'd':
printf("poor !\n");
break;

case 'F':
case 'f':
printf("Fail !\n");
break;
default:
printf("Invalid grade, Please Enter the Grade A to D, and F\n");
}

Discuss the following:


i. Deals with upper case and lower case differently
As we don’t use || Or operator, so we have to write both cases, that is case
‘A’: case ‘b’
ii. Use of break, interrupts to control the flow of program
iii. default case
iv. It is the optional statement, but adds elegance to the program

// Positive of switch statement:

- It is an elegant structure that makes a program more readable to handle the multiway
decisions
- It deals with the integral data (int, short, long or char=> ASCII codes)
- It is an optional statement as we may use if else if statement
// limitations of switch

- Not handle the decimal values


- Not handle the compound expression; that have logical operators like && and ||

//Flow chart of switch

Use of break.

- Switch statement
- Inside the loops
o Example of guess game, can use to exit from the loop
o In the case of a nested loop, it can be used to exit from the innermost loop
Use of continue.

- Control goes to condition


- In a while and do while loop use continue statement after the increment/decrement the
value
- For loop it itself does the increment/decrement first and then control goes to condition
Use of goto

- Unconditional jump
- Never use it
- It makes tracing and debugging and maintaining program
- Make a spaghetti program
- It is in C for completeness, but not use it
TIP

- Minimum use of break and continue

Homework7: Using switch statement:

Sample ouput:

You might also like