C, C++ Programming जरूरी है सॉफ्टवेयर बनाने के liye | C++ Functions & Sy...
Forms Of If Statement
(a) if (condition) (c) if (condition)
do
this; do
this;
else
(b) if (condition) do
this;
do
this;
and
this; (d) if (condition)
{
(e) if
(condition) do
this.;
do
this and
this;
else else
{ {
if (condition) do this;
do
this; and
this;
else
{ }
do
this;
and
this;
}
}
(f) if (condition)
{
if
(condition)
do
this;
else
{
do
this;
and
this;
}
}
else
do
this;
Logical Operators
! - NOT
// - OR
&& - AND
P72 ! (y<10) means “ not Y less than 10”. In other words, if y is less than 10, the
expression will be false, since (y<10) is ture. We can express the same condition as
(y>=10).
NOT
operator is often used to reverse the logical value of a single variable for
example :-
If (1flag)
is the same as writing if (flag = =0)
P.76 CONDITIONAL OPERATOR
General form :-
Expression 1 ? expression 2 : expression 3
If expression 1 is true (that
is its value in non-zero), then value returned will be expression2, otherwise
the value returned will be expression3.
Example :-
Int x, y;
Cout<<”Enter a number :”;
Cin>> x;
Y = (x<5 ? 3 : 4);
This statement will store 3
in y if x is less than 5, otherwise it will store 4 in y.
The equivalent if statement
will be,
If
(x<5)
y =
3;
Else
y =
4;
There are three methods by way of which we can repeat a
part of a program. They are :-
(i)
Using a for
statement
(ii)
Using a while
statement
(iii)
Using a do …
while statement.
(i) For Loop
P.108 General
Form
For (initialize counter; test counter; increment counter)
{
do this;
and this;
and this;
}
P.112 Examples
1. int x;
for (x = 1; x <=10; x++)
cout <<x;
2.
for (int x = 1;
x<=10; x++)
cout<<x;
3.
int x;
for (x = 1; x<=10;)
{ cout<<x;
x++,
}
4.
int x=1
for (;x<=10; x++)
cout<<x;
5.
int x=1
for (;x<=10;)
{
cout<<x;
x++;
}
6.
int x;
for (x=0;
x++<10;)
cout<<x;
7.
int x;
for (x=0;
++x<=10;)
cout<<x;
P.114 Nesting of Loops
Int x,y,sum;
For (x=1; x<=3; x++) /* outer loop*/
{
For (y=1; y<=2; y++) /* inner loop*/
{
sum = x+y;
cout<<” x = “ << x<<”y=
“<<y;
cout<<” sum = “ << sum;
}
}
(ii) While
Loop
P 101 General Form :-
Initialize loop counter;
While (test loop counter using a condition)
{
do this;
and this;
increment loop counter;
}
Examples :-
1. int x = 1;
while
(x<=10)
{
cout<<x;
x++; // or x= x+1; or x+1;
}
2. int
x = 0;
while
(x++<10)
cout<<x;
3. int x = 0;
while
(++x<=10)
cout<<x;
(iii) do while Loop
General form :-
do
{
this;
and
this;
and
this;
} while ( this condition is true);
The break statement :
When ‘break’ is encountered inside any loop, control
automatically passes to the first statement after the loop. A ‘break’ is usually associated with an ‘if’.
Example :- Testing a number for prime :-
Void main ()
{
int
num, I = 2;
cout<<”Ener
a number:”;
cin>>num;
while(i<=num/2)
if(num% i= = 0)
{
cout<<” Not a prime number”;
break;
}
i++
}
if
(i>num/2)
cout<<” \the number is prime”;
}
Continue Statement :-
When ‘continue’ is encountered inside any loop, control
automatically passes to the beginning of the loop. A ‘continue’ is usually associated with an
‘if’.
Example :-
Void main ()
{
int i,j;
for
(i=1; i<=2; i++)
{
for
(j=1; j<=2; j++)
{
if
(i=j)
continue;
cout<<
i<<j;
}
cout<<
endl;
}
}
The
output of the above program will be :- 12
21
The Switch statement :-
This is a multiple – permiting statement. It allows us to make a decision from a number
of choices. Its general form is :
Switch (integer expression)
{
case
constant 1:
do this;
case
constant2:
do this;
case
constant3:
do this;
default:
do this;
Some tips about usage of
‘Switch’:
(a) It is not necessary to arrange the cases
in ascending order. They may be
scrambled.
(b) Char type
data are allowed in switch and case. For
example :-
void main()
{
char c =’x’;
switch©
{
case’v’
cout<<-
- - -;
break;
case’a’
cout<<- - - -;
break;
case’x’
cout<<- - - -;
break;
default :
cout<<- - - -;
}
}
The letters ‘v’,’a’, ‘x’ are actually replaced by the
ASCII values (118, 97, 120) of these character constants.
(c) Common
set of statements can be used
for Example :
case’a’:
case ‘A’:
cout<< - - - - -;
break;
case’b’:
case ‘B’:
cout<< - - - - -;
break;
default;
cout<< - - - - -;
(d) Even if there are multiple statements to
be executed in each case, there is no need to enclose them with in a pair of
braces.
(e) Even statement in a switch must belong
to some case or the other. Statements
before the case will not be executed.
(f) The disadvantage of switch is that we
cannot have a case in a switch which looks like :
case
j<=20;
(g) After that case we can have an int
constant or a char const. Float value is
not allowed.
(h) ‘break’ statement when used in a switch,
takes control outside the switch, and ‘continue’ will not take the control to
the beginning of switch.
(j) It is
very useful while writing menu driven programs.
The ‘goto’ keyword
Use of ‘goto’ takes the control where you want. Though it is easy to use, it should be
avoided. The big problem with ‘goto’ is
that we can never be sure how we got to a certain point in our code.
Comments
Post a Comment