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

Chapter 3

The document discusses different conditional statements in C++ including if, if/else, if/else if, and switch statements. The if statement executes code if a condition is true. If/else adds an else block that executes if the condition is false. If/else if provides multiple conditions that are checked sequentially. Nested if statements allow if blocks within other if blocks. Finally, the switch statement allows branching to different code blocks based on the value of a variable.

Uploaded by

mehari kiros
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chapter 3

The document discusses different conditional statements in C++ including if, if/else, if/else if, and switch statements. The if statement executes code if a condition is true. If/else adds an else block that executes if the condition is false. If/else if provides multiple conditions that are checked sequentially. Nested if statements allow if blocks within other if blocks. Finally, the switch statement allows branching to different code blocks based on the value of a variable.

Uploaded by

mehari kiros
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 74

CHAPTER Three

Making Decisions
THE
if,
if/else,
if/else if
STATEMENTS
THE if STATEMENT
The if statement can cause other statements to execute only
under certain conditions.
 If the expression inside the parentheses is true, the
statements inside the braces are executed. Otherwise, they
are skipped.
The following figure shows the general format of the if statement.
… the if statement
If the block of statements to be conditionally executed
contains only one statement, the braces can be omitted.
Example of if statement:
if (x == 100)
cout << "x is 100";

the cout statement will only be executed only if x is equal to100

If we want more than a single instruction to be executed in


case that condition is true we can specify a block of
instructions using curly braces { }:
if (x == 100)
{
cout << "x is ";
cout << x; }
… the if statement
Putting a semicolon after the if part will terminate the if
statement.
The block of statements will then always execute. For
example,
#include <iostream.h>
int main()
{
int x=4;
if(x>4); // The semicolon terminates the if statement.
{
cout<<"\nInside";
}
cout<<"\nOutside";
}
Output
Inside
Outside
… the if statement
 Without a set of braces, it only executes the next statement.
 For example, notice the following program.

if (average == 100)
cout << "Congratulations! "; // There are no braces.
cout << "That's a perfect score!\n"; // This is outside the if.
 If the condition (average == 100) is false, the Congratulations!
message will be skipped. That's a perfect score! was executed, as it
would be every time, regardless of whether average equals 100 or
not.
… the if statement
 Do not confuse the equality operator (==) with the assignment operator
(=), as in the following statement:

if (x = 2) // Caution here!
cout << "It is True!";
 This statement does not determine if x is equal to 2; it assigns x the
value 2!
 Furthermore, the cout statement will always be executed because the
expression x = 2 evaluates to 2, which C++ considers true.
 C++ stores the value true as 1. But it actually considers all nonzero
values, not just 1, to be true. Thus 2 represents a true condition.
… the if statement
 A relational expression has the value 1 when it is true and 0 when false.
 While 0 is considered false, all values other than 0 are considered true.
 Here is a summary of the rules you have seen so far:
 When a relational expression is true it has the value 1.
 When a relational expression is false it has the value 0.
 An expression that has the value 0 is considered false.
 An expression that has any value other than 0 is considered true.

 For example, the following is a legal if statement in C++:


if (value)
cout << "It is True!";
 If the variable, value, contains any number other than 0, the message “It is
True!” will be displayed.
… the if statement
 Here is another example:
if (x + y)
cout << "It is True!";
 In this statement the sum of x and y is tested.
 If the sum is 0, the expression is false; otherwise it is
true.
 You may also use the return value of a function call as a
conditional expression.
if (pow(a, b))
cout << "It is True!";
 This if statement uses the pow function to raise a to the power of
b.
 If the result is anything other than 0, the cout statement is
executed.
… the if statement
// This program shows an if-statement
#include <iostream.h>
void main()
{
int score1, score2, score3;
double average;
cout << "Enter 3 test scores and I will average them: ";
cin >> score1 >> score2 >> score3;
average = (score1 + score2 + score3) / 3.0;
cout << "Your average is " << average << endl;
if (average == 100)
{
cout << "Congratulations! ";
cout << "That's a perfect score!\n";
}
}
Output Enter 3 test scores and I will average them: 100 100 100[Enter]
Your average is 100.0
Congratulations! That's a perfect score!
THE if/else STATEMENT
THE if/else STATEMENT
 The if/else statement is an expansion of the if statement.
 The if/else statement will execute one set of statements when the if
expression is true, and another set when the expression is false.
 The following figure shows the general format of this statement and a
flowchart visually depicting how it works.
… the if/else Statement

 The if/else statement causes program execution to follow


one of two exclusive paths.
 If you don’t use braces the else part controls a single
statement.
 To execute more than one statements with the else part,
place these statements inside a set of braces.
… the if/else Statement
//Example Program 1
/*This program uses the modulus operator to determine if a number is odd
or even. */
#include <iostream.h>
int main()
{
int number;
cout << "Enter an integer and I will tell you if it\n";
cout << "is odd or even. ";
cin >> number;
if (number % 2 == 0) // If the number is evenly divisible by 2, it
cout << number << " is even.\n";
else
cout << number << " is odd.\n";
return 0;
}
Output
Enter an integer and I will tell you if it
is odd or even. 17[Enter]
17 is odd.
… the if/else Statement
// Example Program 2
/*This program makes sure that the divisor is not equal
to 0 before it performs a divide operation.*/
#include <iostream.h>
int main()
{ Output
double num1, num2, quotient;
cout << "Enter a number: ";
Enter a number: 10[Enter]
cin >> num1; Enter another number: 0[Enter]
cout << "Enter another number: "; Division by zero is not possible.
cin >> num2; Please run the program again and
if (num2 == 0)
{
enter a number other than zero.
cout << "Division by zero is not possible.\n";
cout << "Please run the program again and enter ";
cout << "a number other than zero.\n";
}
else
{
quotient = num1 / num2;
cout << "The quotient of " << num1 << " divided by ";
cout << num2 << " is " << quotient << ".\n";
}
return 0;
}
THE if/else if STATEMENT
THE if/else if STATEMENT
 The if/else if statement is a chain of if statements.
 They perform their tests, one after the other, until one of them is
found to be true.
 The following figure shows its format and a flowchart visually
depicting how it works.
… the if/else if Statement
 Each if statement in the structure depends on all the if statements
before it being false.
 The statements following a particular else if are executed when the
conditional expression following the else if is true and all previous
conditional expressions are false.
 A trailing else, placed at the end of an if/else if statement, provides
a default set of actions when none of the if expressions are true.
… the if/else if Statement
/* Example Program :This program uses an if/else if statement to assign a letter grade (A, B,
C, D, or F) to a numeric test score.*/
#include <iostream.h>
void main()
{
int testScore;
char grade;
cout << "Enter your numeric test score: ";
cin >> testScore;
if (testScore < 60)
grade = 'F';
else if (testScore < 70)
grade = 'D';
else if (testScore < 80)
grade = 'C';
else if (testScore < 90)
grade = 'B';
else if (testScore <= 100)
grade = 'A';
else
{
cout << "\nThe test score is an invalid score.\n";
cout << "Please enter a score that is between 0 and 100.\n";
}
cout << "\nYour grade is " << grade << ".\n";
}
Nested if
Statements
Nested if Statements

 A nested if statement is an if statement in the


conditionally executed code of another if statement.
 Anytime an if statement appears inside another if
statement, it is considered nested.
 Nested if statement is good for narrowing choices down
and categorizing data.
… Nested if Statements
/* Example Program: This program demonstrates a nested if statement.*/
#include <iostream.h>
int main()
{
char employed, recentGrad;
cout << "Answer the following questions with either Y for Yes or N for No.\n";
cout << "Are you employed? ";
cin >> employed;
cout << "Have you graduated from college in the past two years? ";
cin >> recentGrad;
if (employed == 'Y')
{ // Nested if
if (recentGrad == 'Y') // Employed and a recent graduate
{
cout << "You qualify for the special interest rate.\n";
}
else // Employed but not a recent graduate
{
cout << "You must have graduated from college in the past two years to qualify.\n";
}
}
else // Not employed
cout << "You must be employed to qualify.\n";
return 0;
}
SWITCH STATEMENT
switch Statement
 The switch statement lets the value of a variable or
expression determine where the program will branch to.
 The if/else if statement allows your program to branch into
one of several possible paths.
 It performs a series of tests (usually relational) and branches when
one of these tests is true.

 The switch statement tests the value of an integer


expression and then uses that value to determine which set
of statements to branch to.
…switch Statement
 Here is the format of the switch statement:

switch (integer expression) {


case constant expression:
// Place one or more statements here.
case constant expression:
// Place one or more statements here.
// Case statements may be repeated as many times as necessary.
case constant expression:
// Place one or more statements here.
default: // Place one or more statements here.
}
…switch Statement
 An integer expression can be either of the following:
 A variable of any of the integer data types (including char)
 An expression whose value is of any of the integer data types

 On the next line is the beginning of a block containing several case


statements.
 Each case statement is formatted in the following manner:

case constant expression:


// Place one or more statements here.
…switch Statement
 After the word case is a constant expression
 The constant expression can be either an integer literal or an integer
named constant.
 The constant expression
 cannot be a variable and
 it cannot be a Boolean expression such as x < 22 or n == 25.
 The case statement marks the beginning of a section of statements.
 case statements are branched to if the value of the switch expression
matches that of the case expression.
 The expressions of each case statement in the block must be unique.
…switch Statement

 An optional default section comes after all the case

statements.

 This section is branched to if none of the case expressions match

the switch expression.

 Thus it functions like a trailing else in an if/else if statement.


Example(This program demonstrates the use of a switch statement)

main()
{
char choice; Program Output
cout << "Enter A, B, or C: "; Enter A, B, or C: B [Enter]
cin >> choice; You entered B.
switch (choice) Program Output
{ Enter A, B, or C: F [Enter]
case 'A': You did not enter A, B, or C!
cout << "You entered A.\n";
break;
case 'B':
cout << "You entered B.\n";
break;
case 'C':
cout << "You entered C.\n";
break;
default:
cout << "You did not enter A, B, or C!\n";
}
}
…switch Statement
 A break statement is needed whenever you want to “break out of” a
switch statement.
 The case statements show the program where to start executing in the
block and the break statements show the program where to stop.
 Without the break statements, the program would execute all of the
lines from the matching case statement to the end of the block.
 the program “falls through” all of the statements below the one with
the matching case expression.
 The default section (or the last case section, if there is no default)
does not need a break statement. Put there anyway, for consistency.
Example (This program demonstrates how a switch statement works if
there are no break statements)
main() Program Output:
{ Enter A, B, or C: A[Enter]
char choice; You entered A.
cout << "Enter A, B, or C: "; You entered B.
cin >> choice; You entered C.
You did not enter A, B, or C!
switch (choice)
Program Output
{ Enter A, B, or C: C[Enter]
case 'A': You entered C.
cout << "You entered A.\n"; You did not enter A, B, or C!
case 'B':
cout << "You entered B.\n";
case 'C':
cout << "You entered C.\n";
default :
cout << "You did not enter A, B, or C!\n";
}
}
Example (The switch statement in this program uses the "fall through" feature
to catch both uppercase and lowercase letters entered by the user)
main()
{
char feedGrade;
cout << "Our dog food is available in three grades:\n";
cout << "A, B, and C. Which do you want pricing for? ";
cin >> feedGrade;
switch(feedGrade) Program Output
{ Our dog food is available in three grades:
case 'a': A, B, and C. Which do you want pricing for?
case 'A': b[Enter]
cout << "30 cents per pound.\n"; 20 cents per pound.
break; Program Output
case 'b': Our dog food is available in three grades:
case 'B': A, B, and C. Which do you want pricing for?
cout << "20 cents per pound.\n"; B[Enter]
break; 20 cents per pound.
case 'c':
case 'C':
cout << "15 cents per pound.\n";
break;
default :
cout << "That is an invalid choice.\n";
}
return 0;
}
THE CONDITIONAL OPERATOR
The Conditional Operator
 You can use the conditional operator to create short
expressions that work like if/else statements.
 The operator consists of the question-mark (?) and the
colon(:).
 Its format is

Condition ? expression : expression;


… the Conditional Operator
 Here is an example of a statement using the conditional operator:

x < 0 ? y = 10 : z = 20;
 This statement is called a conditional expression and consists of three sub-
expressions separated by the ? and : symbols.
 The expressions are x < 0, y = 10, and z = 20.
 Note: Since it takes three operands, the conditional operator is considered a
ternary operator.
 The conditional expression above performs the same operation as the
following if/else statement:
if (x < 0)
y = 10;
else
z = 20;
… the Conditional Operator
 The part that comes before the question mark is the expression to be
tested.
 It’s like the expression in the parentheses of an if statement.

 If the expression is true, the part of the statement between the ?


and the : is executed.
 Otherwise, the part after the : is executed.
 E.g. (x < 0) ? (y = 10) : (z = 20);
… the Conditional Operator
 If the first sub-expression is true, the value of the conditional
expression is the value of the second sub-expression. Otherwise it is
the value of the third sub-expression.
 Here is an example of an assignment statement using the value of a
conditional expression:

a = x > 100 ? 0 : 1;
 The value assigned to a will be either 0 or 1, depending upon whether
x is greater than 100.
 This statement could be expressed as the following if/else statement:
if (x > 100)
a = 0;
else
a = 1;
… the Conditional Operator

 Here is the statement with the conditional expression:


hours = hours < 5 ? 5 : hours;
 If the value in hours is less than 5, then 5 is stored in
hours.
 Otherwise hours is assigned the value it already has.
… the Conditional Operator
 For instance, consider the following statement:
cout << "Your grade is: " << (score < 60 ? "Fail." : "Pass.");
 If you were to use an if/else statement, this
statement would be written as follows:
if (score < 60)
cout << "Your grade is: Fail.";
else
cout << "Your grade is: Pass.";

 The parentheses are placed around the conditional


expression because the << operator has higher
precedence than the ?: operator.
 Without the parentheses, just the value of the
expression score < 60 would be sent to cout.
LOOPING
Introduction to Loops
 A loop is a control structure that causes a
statement or group of statements to
repeat.
 C++ has three looping control structures:
 the while loop,
 the do-while loop, and
 the for loop.
 The difference between each of these is
how they control the repetition.
The while Loop
 The while loop has two important parts:
 (1) an expression that is tested for a true or false value, and
 (2) a statement or block that is repeated as long as the
expression is true.
 The general format of the while loop:
Example-1(… the while Loop)
// custom countdown using while
#include <iostream.h>
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0)
{
cout << n << ", ";
--n;
}
cout << "FIRE!"; Output:
return 0; Enter the starting number > 8
} 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
Example-2(… the while Loop)
// This program demonstrates a simple while loop.
#include <iostream.h>
int main()
{
int number = 0;
cout << “Enter number after a number. Enter 99 to quit.\n";
while (number != 99)
cin >> number;
cout << "Done\n";
return 0;
}
… the while Loop
 This program repeatedly reads values from the keyboard
until the user enters 99.
 The loop controls this action by testing the variable
number.
 As long as number does not equal 99, the loop repeats.
 Each repetition is known as an iteration.
while Is a Pretest Loop
 The while loop is known as a pretest loop,
 which means it tests its expression before each iteration.
 Notice the variable definition of number in Example-2:
int number = 0;
number is initialized to 0.
 If number had been initialized to 99, as shown in the
following program segment, the loop would never
execute the cin statement:
int number = 99;
while (number != 99)
cin >> number;
 An important characteristic of the while loop is that the
loop will never iterate if the test expression is false to
start with.
Terminating a Loop
 Loops must contain a way to terminate.
 This means that something inside the loop must eventually make the
test expression false..
 If a loop does not have a way of stopping, it is called an infinite
loop. Here is an example:
int test = 0;
while (test < 10)
cout << "Hello\n";
 The following loop will stop after it has executed 10 times:
int test = 0;
while (test < 10)
{
cout << "Hello\n";
test++;
}
Counters
 A counter is a variable that is regularly incremented or
decremented each time a loop iterates.
 For example, the following code displays numbers 1
through 10 and their squares, so its loop must iterate 10
times. Output
int num = 1; // Initialize counter Number Number
Squared
while (num <= 10) ---------- ---------------
{ 1 1
2 4
cout << num << "\t\t" << (num * num) << endl; 3 9
num++; // Increment counter 4 16
5 25
} 6 36
7 49
8 64
9 81
10 100
…counters
 num is used as a counter variable,
 num counts the number of iterations the loop has
performed.
 Since num is controlling when to stay in the loop and
when to exit from the loop, it is called the loop control
variable.
 Another approach is to combine the increment
operation with the relational test, as shown in the
code below.
int num = 0;
while (num++ < 10)
cout << num << "\t\t" << (num * num) << endl;
The do-while Loop
 The do-while loop looks similar to a while
loop turned upside down.
 The following figure shows its format:
… the do-while Loop
 The do-while loop must be terminated with
a semicolon after the closing parenthesis
of the test expression.
 do-while is a posttest loop.
 It tests its expression after each iteration is
complete.
 do-while always performs at least one
iteration, even if the test expression is false
from the start.
… the do-while Loop
 For example, in the following while loop the cout
statement will not execute at all:
int x = 1;
while (x < 0)
cout << x << endl;
 But the cout statement in the following do-while loop will
execute once.
int x = 1;
do
cout << x << endl;
while (x < 0);
 You should use do-while when you want to make sure
the loop executes at least once.
Example -1(… the do-while Loop)
// the following program echoes any number you enter until you enter 0.
#include <iostream.h>
int main () Output:
{ Enter number (0 to end):
unsigned long n; 12345
do You entered: 12345
{ Enter number (0 to end):
cout << "Enter number (0 to end): "; 160277
cin >> n; You entered: 160277
Enter number (0 to end): 0
cout << "You entered: " << n << "\n";
You entered: 0
} while (n != 0);
return 0;
}
The for Loop
 It is ideal for situations that require a
counter.
 Here is the format of the for loop.
for (initialization; test; update)
{
statement;
statement;
// Place as many statements here as necessary.
}
… the for Loop
 The for loop has three expressions inside the
parentheses, separated by semicolons.
 The first expression is the initialization
expression.
 It is used to initialize a counter or other variable that
must have a starting value.
 The second expression is the test expression.
 it controls the execution of the loop.
 The third expression is the update expression.
 It executes at the end of each iteration.
 It increments a counter or other variable that must be
modified in each iteration.
… the for Loop
 Example: The mechanics of the for loop
Example -1(… the for Loop)
// countdown using a for loop
#include <iostream.h>
int main ()
{
for (int n=10; n>0; n--)
{
cout << n << ", ";
}
cout << "FIRE!";
return 0;
}
Output:
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
… the for Loop
 The initialization expression may be omitted from the for
loop’s parentheses if it has already been performed.
int num = 1;
for ( ; num <= 10; num++)
cout << num << "\t\t" << (num * num) << endl;
 You may also omit the update expression if it is being
performed elsewhere in the loop.
 The following for loop works just like a while loop:
int num = 1;
for ( ; num <= 10; )
{
cout << num << "\t\t" << (num * num) << endl;
num++;
}
… the for Loop
 You can even go so far as to omit all three
expressions from the for loop’s parentheses.
 Be warned, that if you leave out the test
expression, the loop has no built-in way of
terminating.
 Here is an example:
for ( ; ; )
cout << "Hello World\n";
 Because this loop has no way of stopping, it will
display "Hello World\n" forever (or until
something interrupts the program).
Other Forms of the Update
Expression
 Here is a loop that displays all the even numbers
from 2 through 100 by adding 2 to its counter:
for (number = 2; number <= 100; number += 2)
cout << number << endl;
 And here is a loop that counts backward from 10
down to 0:
for (number = 10; number >= 0; number--)
cout << number << endl;
 The following loop has no formal body. The
combined increment operation and cout
statement in the update expression perform all
the work of each iteration:
for (number = 1; number <= 10; cout << number++);
… the for Loop
 Connecting multiple statements with commas works well in
the initialization and update expressions.
 If you wish to perform more than one conditional test, build
an expression with the && or || operators, like this:

for (count = 1, total = 0; count <= 10 && total < 500; count++)
{
double amount;
cout << "Enter the amount of purchase #" << count << ": ";
cin >> amount;
total += amount;
}
Nested Loops
 A loop that is inside another loop is called a nested loop.
 The first loop is called the outer loop.
 The one nested inside it is called the inner loop.
 Here is the format:
while (some condition) // Beginning of the outer loop
{
// ---
while (some condition) // Beginning of the inner loop
{
//------
} // End of the inner loop
} // End of the outer loop
 Nested loops are used when, for each iteration of the outer loop,
something must be repeated a number of times.
 Whatever the task, the inner loop will go through all its iterations
each time the outer loop is done.
 Any kind of loop can be nested within any other kind of loop.
… Nested Loops
 An inner loop goes through all of its iterations for
each iteration of an outer loop.
 Inner loops complete their iterations faster than
outer loops.
 To get the total number of iterations of an inner
loop, multiply the number of iterations of the
outer loop by the number of iterations done by
the inner loop each time the outer loop is done.
 For example, if the outer loop is done twice, and the
inner loop is done three times for each iteration of the
outer loop, the inner loop would be done a total of six
times in all.
Breaking Out of a Loop
 The break statement causes a loop to terminate early.
 When it is encountered, the loop stops and the program jumps to
the statement immediately following the loop.
 It can be used to end an infinite loop, or to force it to end before its
natural end.
 The following program segment appears to execute 10 times, but
the break statement causes it to stop after the fifth iteration.
int count = 0;
while (count++ < 10)
{
cout << count << endl;
if (count == 5)
break;
}
Example -1
//Stop the count down before it naturally finishes using break statement:
#include <iostream.h>
int main ()
{
int n;
for (n=10; n>0; n--)
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
return 0;
}
Output:
10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!
Using break in a Nested Loop
 In a nested loop, the break statement only interrupts the loop it is
placed in.
 For example:
for (int row = 0; row < 2; row++)
{
for (int star = 0; star < 6; star++)
{ The output of this program segment is
cout << '*';
if (int star == 3)
break;
****
****
}
cout << endl;
}
The continue Statement
 The continue statement causes a loop to stop its
current iteration and begin the next one.
 The continue statement causes the current
iteration of a loop to end immediately.
 When continue is encountered, all the
statements in the body of the loop that appear
after it are ignored, and the loop prepares for the
next iteration.
 In a while loop, this means the program jumps to the
test expression at the top of the loop.
 In a do-while loop, the program jumps to the test
expression at the bottom of the loop
 In a for loop, continue causes the update expression
to be executed.
Example(…the continue statement)
 The following program segment demonstrates the use of continue in
a while loop:
int testVal = 0;
while (testVal++ < 10)
{
if (testVal == 4)
continue;
cout << testVal << " ";
}
 This loop looks like it displays the integers 1 through 10. When
testVal is equal to 4, however, the continue statement causes the
loop to skip the cout statement and begin the next iteration.
The output of the loop is
1 2 3 5 6 7 8 9 10
… the continue Statement
 The continue instruction causes the program to skip the rest of the
loop in the present iteration, causing it to jump to the following
iteration.
 For example, we are going to skip the number 5 in our countdown:
#include <iostream.h>
int main ()
{
for (int n=10; n>0; n--)
{
if (n==5) Output:
continue; 10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!
cout << n << ", ";
}
cout << "FIRE!";
return 0;
}
The goto statement
 It allows making an absolute jump to
another point in the program.
 The destination point is identified by a
label, which is then used as an argument
for the goto statement.
 A label is made of a valid identifier
followed by a colon (:).
… the goto statement
// goto loop example
#include <iostream.h>
int main ()
{
int n=10;
loop: Output:
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
cout << n << ", ";
n--;
if (n>0)
goto loop;
cout << "FIRE!";
return 0;
}
Using Loops for Data Validation
 Loops can be used to create input routines that repeat until
acceptable data is entered.
 Loops are especially useful for validating input.
 They can test whether an invalid value has been entered and, if so,
require the user to continue entering inputs until a valid one is received.
 Using if statements to validate user inputs cannot provide an ideal
solution.
 Using while loop to validate input data, Every input will be checked.
cout << "Enter a number in the range 1 - 100: ";
cin >> number;
while (number < 1 || number > 100)
{
cout << "ERROR: The value must be in the range 1 - 100: ";
cin >> number;
}
………………………………………
………………………………………
………………………………..END
………………………………….OF
………………………...CHPTER 4
-*-*-*-*-*-*-*-*-*-*-*-*-*-*- THANK U!
THANK U!
*-*-*
END OF CHAPTER-3
*-*-*

You might also like