Lecture3
Lecture3
Condition
Conditionals operators use two kinds of special operators:
relational and logical. These are used to determine whether
some condition is true or false. The relational operators are
used to test a relation between two expressions:
The logical operators are often used to combine
relational expressions into more complicated Boolean
expressions:
Examples using logical operators
(assume x = 6 and y = 2):
return 0;
}
#include <iostream> if (y>x)
{
using namespace std; if (y>z)
int main(){ {
int x,y,z,l; l=y;
}
cout<< "Enter THREE
Numbers at random"; }
cout<< "Enter X"; else if (z>x)
cin>> x; {
l=z;
cout<< "Enter Y"; }
cin>> y;
cout<< "Enter Z "; cout<< "The largest is "<< l;
return 0;
cin>> z;
}
l=x;
This is a multiple-branching statement
where, based on a condition, the control is
transferred to one of the many possible
points;
The switch-case is another conditional structure
that may or may not execute certain statements.
The switch evaluates expression and, if expression
is equal 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 are equal, then the
statements beneath case constant 2: are executed
until a break is encountered.
If not, then the same process repeats for each of the
constants, in turn. If none of the constants match,
then the statements beneath default: are executed.
switch(expression)
{
case constant1:
statementA1
statementA2
...
break;
case contant2:
statementB1
statementB2
...
break;
...
default:
statementZ1
statementZ2
...
}
#include <iostream>
using namespace std;
int main()
{
int x = 6;
switch(x)
{
case 1:
cout << “x is 1\n”;
break;
case 2:
case 3:
cout << "x is 2 or 3";
break;
default:
cout << "x is not 1, 2, or 3";
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
char gender;
cout<< "Enter Gender m/f :";
cin>> gender;
switch(gender)
{
case 'm': cout<<"He is a MAN";
break;
case 'f': cout<<"She is a WOMAN";
break;
default: cout<<" Invalid Gender";
}
return 0;
}
Used where statements in a program
be require to be executed several
times
C++ has three kinds of loops:
While
do-while
for
The while statement:
Syn:
While(condition)
{
Statements;
}
As long as condition holds, the block of
statements will be repeatedly executed.
#include <iostream>
using namespace std;
int main()
{
int x = 0;
while(x < 10)
{
x = x + 1;
cout << “x is “ << x << “\n”;
}
return 0;
}
The do-while statement:
The do-while loop is a variation that
guarantees the block of statements will be
executed at least once:
Syn:
Do
{
Statements ;
}
while(condition);
The block of statements is executed and then,
if the condition holds, the program returns to
the top of the block.
Curly braces are always required.
Also note the semicolon after the while
condition.
The for loop:
Syn:
for(expression1;expression2;expression3)
{
Statements;
Statements;
}
The for loop is designed to allow a counter
variable that is initialized at the beginning of
the loop and incremented (or decremented) on
each iteration of the loop.
#include <iostream>
using namespace std;
int main()
{
for(int x = 0; x < 10; x = x + 1)
{
cout << x << “\n”;
}
return 0;
}
This program will print out the values 0 through 9,
each on its own line.
If the counter variable is already defined, there is no
need to define a new one in the initialization portion
of the for loop.
#include <iostream>
using namespace std;
int main()
{
int n,c=1;
cout<< "Enter number of times to display the Message :";
cin>>n;
while (c<=n)
{
cout<< "HELLO" << "\n";
c++;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n,c=1;
cout<< "Enter number of times to display the Message :";
cin>>n;
do
{
cout<< "HELLO" << "\n";
c++;
}while (c<=n);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n,c;
cout<< "Enter number of times to display the Message :";
cin>>n;
for (c=1;c<=n;c++)
{
cout<< "HELLO" << "\n";
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n,fact=1,c;
cout<< "Enter number N to compute Factorial :";
cin>>n;
for (c=1;c<=n;c++)
{
fact = fact*c;
}
cout<< "The Factorial of "<<n<< "is "<<fact;
return 0;
}
It is possible to place ifs inside of ifs and
loops inside of loops by simply placing these
structures inside the statement blocks. This
allows for more complicated program
behavior.
The inner loop is executed first, after which
control is taken to the outer loop until
program terminates
#include <iostream>
using namespace std;
int main()
{
for(int x = 0; x < 4; x = x + 1)
{
for(int y = 0; y < 4; y = y + 1)
{
cout << y;
}
cout << “\n”;
}
return 0;
}