Sei sulla pagina 1di 2

C++ sample problem (conditional and loop structure }

combine) getch();
return 0;
• Write a c++ program that prompts the user to }
enter an integer between 5 to 10. The program
will then output the number type in by the user • The inner loop is executed many times compared
except that the user is limited to three trials only, with the outer loop. However, it is the outer loop
if the user exceeds the limit, the program that dictates whether the inner loop will execute
terminates. or not.

#include <iostream.h> #include <iostream.h>


#include <conio.h> #include <conio.h>
#include <process.h> main()
main() {
{
int num, try=1; int i,j;
clrscr(); clrscr();
cout<<"\n Enter number from 5-10: "; for(i=1;i<5;++i)
cin>>num; {
while(num <5 || num>10 ) cout<<"\n"<<i;
{ for(j=i+1;j<5;++j)
++try; cout<<“ "<<j;
if(try >3) }
{ getch();
cout<<“You Exceed the number of trials”; return 0;
cout<<"\n Program Exiting"; }
getch();
exit(0); Using a break statement with Nested loops

} #include <iostream.h>
clrscr(); #include <conio.h>
cout<<"\n\nINvalid number!!ty again\n\n "; #include <iomanip.h>
cout<<"Enter an integer between 5 and 10 :"; main()
cin>>num; {
}
cout<<"The number you entered is: "<<num; int i,j,m;
getch(); clrscr();
return 0; for(i=1;i<6;++i)
} {
for(j=1;j<6;++j)
Nested Loop if(j>i)
break;
• When there’s a need to repeat a loop several else
times, we simply put the loop inside another loop. cout<<setw(4)<<i*j;
• Nested loop can be a bit confusing but if you cout<<"\n";
know the basic concepts well it shouldn’t be a }
problem. getch();
• As an analogy, we can think of a nested loop like return 0;
the hands of a clock. Where the hands represent }
each loop, the hour hand is the outer loop,
followed by the minute hand, then the second The switch statement
hand.
 The switch statement can be used instead of the
Sample: else-if construct to implement a sequence of
parallel alternatives.
• A c++ program that prints a multiplication table.  Its syntax is:
#include <iostream.h> switch(expression)
#include <conio.h> {
main() case constant1: statement1;break;
{ case constant2: statement2;break;
case constant3: statement3;break;
int x,y; :
clrscr(); :
for(x=1;x<=5;++x) case constantN: statementN;break;
{ default: statement 0;
for(y=1;y<5;++y) }
{
cout<<" "<<x*y;
}  This evaluates the expression and then looks for
cout<<"\n"; its value among the case constants. If the value is
found among the constants listed, then the
statements in the corresponding statement list are
executed.
 Otherwise if there is a default(which is optional),
then the program branches to its statement list.
 The expression must evaluate to an integral type
and the constants must be integral constants.

Example: using a switch statement to select a range of


scores

main()
{
int score;
cout<<“Enter your test score:”;
cin>>score;
switch(score/10)
{
case 10:
case 9:cout<<“Your grade is A.”; break;
case 8:cout<<“Your grade is B.”; break;
case 7:cout<<“Your grade is C.”; break;
case 6:cout<<“Your grade is D.”; break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:cout<<“Your grade is an F.”;break;
default: cout<<“Error: Score is out of range\n”;
}
}

Potrebbero piacerti anche