Conditional Statement - If & For Loop
Conditional Statement - If & For Loop
}
if else
We can additionally specify what we want to happen if the
condition is not fulfilled by using the keyword else. Its form used
in conjunction with if is: #include <iostream.h>
if (condition) int main()
{
statementA1;
{ int x ;
statementA2; cout << "enter value for x \n";
… cin>> x ;
} if (x > -1)
else cout << "x is positive\n";
{
statementB1;
else
statementB2; cout<< "x is negative";
… return 0;
} }
Because the condition is either satisfied or not, one of the blocks
in an if-else must execute.
#include <iostream.h> #include <iostream.h>
int main() int main()
{ int x,y ; { int x,y ;
cout << "enter value for x & y\n"; cout << "enter value for x & y\n";
cin>> x >>y; cin>> x >>y;
if (x > y) if (x > y)
{ cout<<"result of x-y is\t"; { cout<<"result of x-y is\t";
cout << (x-y) <<endl; cout << (x-y) <<endl;
} }
if(x<y) else
{ cout<<"result of y-x is\t"; { cout<<"result of y-x is\t";
cout<<(y-x) ; cout<<(y-x) ;
} }
return 0; return 0;
} }
else if
The else if is used to decide between two or more blocks based on
multiple conditions:
#include <iostream.h> #include <iostream.h>
int main()
int main()
{ {
int x,y; int x,y;
cin>>x>>y; cin>>x>>y;
if (x > y) if (x > y)
cout << " x is greater than y\n"; cout << " x is greater than y\n";
else if (y > x) if (y > x)
cout << "y is greater than x\n“; cout << "y is greater than x\n";
else
cout << "x and y are equal\n"; else
return 0; cout << "x and y are equal\n";
return 0;
} }
#include <iostream.h>
int main()
{
int x,y;
cin>>x>>y;
if (x > y)
cout << " x is greater than y\n";
else if (y > x)
cout << "y is greater than x\n";
else if (y == x)
cout << “x and y are equal\n";
return 0;
}
Nested if else
#include <iostream.h>
if (expression) int main()
{
{ if(expression) int x,y,z;
statement; cin>>x>>y>>z;
else If (x >y)
{ if (x>z)
statement; {cout << " x is the largest\t"<<x;}
} else
else {cout << "z is the largest\t"<<z; }
}
{ if(expression)
else
statement; { if (y>z)
else { cout << "y is the largest\t"<<y; }
else
statement
{ cout << "z is the largest\t"<<z; }
} }
return 0;
}
Class work
switch (expression)
{
case constant1:
statementA1
statementA2 curly braces are not necessary for cases
... where there is more than one statement
break;
case constant2:
statementB1 The switch evaluates expression and, if expression is equal
statementB2
to constant1, then the statements beneath case constant 1:
are executed until a break is encountered. If expression is not
...
equal to constant1, then it is compared to constant2. If these
break;
are equal, then the statements beneath case constant 2: are
...
executed until a break is encountered. If not, then the same
default: process repeats for each of the constants, in turn. If none of
statementZ1 the constants match, then the statements beneath default: are
statementZ2 executed.
...
}
#include <iostream.h>
int main()
{
int x,y;
cin>>x;
y=x%2;
switch(y)
{
case 0:
cout<<"x is even number";
break;
default :
cout <<"x is odd";
}
return 0;
}
Several case can use same statement
#include <iostream.h>
int main()
{ int score,grade;
cin>>score;
grade=score/10;
switch(grade)
{
case 10:
case 9:
case 8:
cout<<"you got A+";
break;
default :
cout <<"you got less than 80% mark”;
}
return 0;
}
Char type data can be used as case constant
#include <iostream.h>
int main()
{
char x;
cin>>x;
switch(x)
{ case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout<<"your choice is a vowel";
break;
default :
cout <<"your choice is a consonent“;
}
return 0;
}
LOOP
Loops allow you to perform statements/block repeatedly
for loop
Its syntax is:
for(initialization; condition; increase/decrease) // no semicolon
{
statement1;
statement2;
…
}
Initializations: declare and/or initialize control variable. it is evaluated first,
before any iteration occurs.
int main()
{
int x = 0;
for(; x < 10;)
{cout << x << “\n”;
x ++;
}
return 0;
}
Any type variable can be used as control variable in for
loop
#include <iostream.h>
int main()
{
for(char x = ‘A’; x < =‘Z’; x ++)
cout << x << endl;
return 0;
}
#include <iostream.h>
int main()
{
cout<<"first loop\n";
for(int x = 1; x < 10; x ++)
{cout << x << "\n";}
cout<<"second loop\t";
for(int y = 5; y >0; y --)
{cout << y << "\t"; }
cout<<"\n\nthird loop\a\n";
for(int z = 15; z < 10; z ++)
{cout << z << "\n"; }
return 0;
}
For loop to sum a series
s=1+2+3+4+5+………………………….n
#include<iostream.h>
void main()
{
int i ,n,sum=0;
cout <<"what is your n value ";
cin>>n ;
for(i=1;i<=n;i++)
{sum =sum+i;}
cout<<"summation is "<<sum;
}
Nested for loop
When you place another loop within a loop, the loops are
nested loops.
#include<iostream.h>
void main ()
{
int i, j;
for(i=1;i<=3;i++) //outer loop start
{ cout<<"\ni is now\t"<<i;
for(j=1;j<=4;++j) //inner loop start
{
cout<<" j= "<<j;
i is now 1 j=1 j=2 j=3 j=4
i is now 2 j=1 j=2 j=3 j=4
} // inner loop end i is now 3 j=1 j=2 j=3 j=4
} // outer loop end
}
#include<iostream.h> #include<iostream.h>
void main () void main ()
{ {
int i, j; int i, j;
for(i=5;i>=1;i--)
for(i=1;i<=5;i++)
{
{ for(j=1;j<=i;j++)
for(j=1;j<=i;j++) {cout<< j<< "\t";
{cout<< j<< "\t"; }
} cout<<"\n";
cout<<"\n"; }
}
} }
HW
//pyramid of star
#include<iostream.h>
void main()
{
int b,c,n,m;
cout<<"Enter your line no\t";
cin>>n;
for(c=0;c<=n;c++) // for row
{ for(b=1;b<=n-c;b++)
*
{cout<<" "; //printing spaces
* * * }
* * * * * for(m=1;m<=2*c-1;m++)
{cout<<"*";} //printing stars
* * * * * * * cout<<"\n ";
* * * * * * * * * }
}
while loop
The while loop has a form similar to the if conditional:
while(condition) // no semicolon
{ #include <iostream.h>
int main ()
{
int n;
do {
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
}
while (n != 0);
cout<< “terminated”;
return 0;
}