NIOS Computer Science: Chapter 9 – Control Statements Part 6

Download PDF of This Page (Size: 214K)

Image of while loop

Image of While Loop

Image of while loop

Example

Write a program that will accept a one character grade code and depending on what grade code is input, display the Basic salary according to the table given below:

Table show Grade and Baseic salary
Table show Grade and Baseic salary

Grade

Basic salary

A

15000

B

12000

C

10000

D

9000

# include < iostream.h>

void main ( )

{

char grade;

cout << “Enter Grade” << “\n”;

cin >> grade;

switch (grade)

{

case ‘A’ : cout << “Basic salary is 15000”;

break;

case ‘B’ : cout << “Basic salary is 12000;”

break;

case ‘C’ : cout << “Basic salary is 10000”;

break;

case ‘D’ : cout << “Basic salary is 9000”;

break;

default : cout << “Invalid grade code”;

}

}

Loop Construct

It is also called a Repetitive control structure. Sometimes we require a set of statements to be executed a number of times by changing the value of one or more variables each time to obtain a different result.

This type of program execution is called looping. C++ provides the following constructs.

Image of Loop construct

Image of Loop Construct

Image of Loop construct