Programming in C++
Generally a program executes its statement from beginning to
end. But sometimes program needs repetition of statement and making decisions.
C++ provides such tools by providing statements to attain so. Such statements
are called program control statements.
Flow Of Control in C++
· Flow of control
1.
Sequence
2.
Selection
3.
Iteration
1. Sequence
Sequence
construct means the statements are being executed sequentially. In C++, program
starts from main () and ends at the last statement of main ().
2.
Selection
Selection
is based upon the test condition .if condition evaluates to true, one
course-of-action is executed otherwise other course-of-action is executed.
3.
Iteration
Iteration
construct means repetition of a set of statement on the basis of test
condition. As soon as the condition false repetition will stop.
Conditional control statements
1.
If
2.
If-else
3.
If-else if-else
4.
While
->looping statement (entry control)
5.
Do while ->looping
statement (exit control)
6.
Switch
->multiple branch selection
7.
For
->looping statement
1. If
If the statement evaluates to true
statement 1 (S1) is executed and then statement2 (S2).
if(condition)
S1;
S2;
If we want to execute more than one
statement when condition is true then write all those statement in braces {}
after if.
if (condition)
{
S1;
S2;
}
S3;
2.
If-else
Also known as either -or. This used to
select one statement and ignore the other statement.
Ex.
main ()
{
int a, b;
cin>>a>>b;
if (a>b)
{
cout<<”a is greater”;
}
else
{
cout<<”b is greater”;
}
}
3. For
It is repeated structure which can repeat a
statement as long as the given condition is satisfied.
for (initialization; condition; update)
{
S1;
}
Eg.
main ()
{
int i;
for (i=0; i<10; i++)
{
cout<<i;
}
}
Output
0
1 2 3
4 5 6 7 8 9
4. While
It is also a repeated structure statement.
It is also known as entry-controlled loop.
Syntax
while (expression)
loop-body
in while loop, a loop control variable
should be initialized before the loop begins and loop variable should be
updated inside the body of loop.
Q. Write a program to find the factorial of a number.
#include<iostream.h>
#include<conio.h>
int main()
{
int i, num, fact=1;
cout<<”\n Enter
integer:”;
cin>>num;
i=num;
while(num)
{
fact*= num;
--num;
}
cout<<”\n The factorial of”<<i<<”is”
<<fact;
return 0;
}
Output: Enter integer:5
The factorial of 5 is 120
5. Do- while
Like while statement it repeats a statement
given as long as the condition is satisfied unlike in while statement the
condition is checked at the end of the structure.
Syntax
do
{
statement 1;
}
while (expression);
statement 1 is executed till the condition
is true.
Ex.
char ch=’a’;
do
{
cout<<”\n”<<ch;
ch++;
} while (ch<=’z’);
6. Switch
It is a multi branch statement which can be
used to select and execute one of the available statements.
A switch can have upto 257 case statement.
When there is no break statement in the switch case, it is called fall through.
Syntax
switch (value)
{case 1:
statement 1;
break;
case 2:
statement 2;
break;
.
.
.
.
case n:
statement n;
break;
default:
statement d;
}
Note:
here value can be numeric or character.
Jump Statements
The jump statement unconditionally
transfers program control within the function. C++ has four statements that
perform an unconditional branch-
1.
return
2.
goto
3.
Break
4. Continue
In
addition, C++ provides a standard library function exit() that helps you break
out of a program.
1.
Goto statement
A
goto statement can transfer the program control anywhere in the program. The
target destination of a goto statement
is marked by a label.
Syntax
goto
label;
.
.
.
.
.
label;
Ex.
a=0;
start:
cout<<”\n”<<++a;
if
(a<50) goto start;
2.
Break statement
Break
statement is used to skip over a part of code. Break statement terminates the
smallest enclosing while, do-while, for or switch statement.
Ex while (test expression) for
(int;expression;update)
{ {
statement 1; statement 1;
if (a==5)
if(a==5)
break; break;
. statement 2;
. }
statement 2; statement
3;
statement
3;
Break statement leaves the program control to statement 3 in above example.
Break statement leaves the program control to statement 3 in above example.
3.
Continue statement
The
continue statement is another jump statement like the break as both statement
skip over a part of code. But it is different from break because it forces the next iteration of the loop to take
place, skipping any code in between.
while (expression) for (int;
exp; update)
{ {
S1; s1;
if(condition) if(condition)
continue; continue;
s2; s2;
} }
s3; s3;
In while loop continue statement leave the program control to expression.
In for loop continue statement leave the program control to update.
Ex. main ()
{
int
a; b; i;
for (i=0; i<10; i++)
{
cout<<”\n Enter 2 numbers”;
cin>>a>>b;
if(b==0)
{
cout<<”\n the denominator can’t be zero”<< “Enter again!”;
continue;
}
else
c=a/b;
cout<<”\n Quotient=”<<c”\n”;
}
Q. Write a short program to input a digit and print it in words.
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<”Enter a
number from(0-9)”;
cin>>num;
switch(num)
{
case 1:
cout<<”One”;
break;
case 2:
cout<<”Two”;
break;
case 3:
cout<<”Three”;
break;
case 4:
cout<<”Four”;
break;
case 5:
cout<<”Five”;
break;
case 6:
cout<<”Six”;
break;
case 7:
cout<<”Seven”;
break;
case 8:
cout<<”Eight”;
break;
case 9:
cout<<”Nine”;
break;
default:
cout<<”Wrong
Entry”;
}
Q. Write a program to check whether the number is prime or not.
#include<iostream.h>
#include<conio.h>
void main()
{
int num, i, flag=0;
cout<<”Enter a
number”;
cin>>num;
for(i=2 ; i< num ;
i++)
{
if ( ( num % i)==0 )
{
flag=1;
break;
}
}
if(flag=0)
cout<<num<<”is
prime no”;
else
cout<<num<<”is not prime no”;
}
Q.Write a program to print
A
A B
A B C
A B C D
A B C D E
A B C D E F
#include<iostream.h>
#include<conio.h>
void main()
{
char ch=’A’;
int i, j;
for(i=1;i<7;i++)
{
cout<<”\n”;
for(j=1;j<=i;j++)
{
cout<<ch++;
}
}
}
This post is about Flow of Control in C++. In the next post we will discuss the topic Function.
0 Comments