Lesson 3
Lesson 3
Lesson 3
Conditional
Statements
Every day in our life we make decisions whether our decisions good or
bad it all depends on the conditions that we make. The same is also true
in computer programming we use conditions to make our programs
intelligent enough to give us right results when needed most based on the
values given by the user.
Syntax
if (condition)
{
statement1;
statement2;
}
The commend says that if the condition is true then perform the following statement or if the
condition is false the computer skips the statement and moves on to the next instruction in the
program.
Example Program
Syntax
if (condition)
{
//Execute this block if
//Condition is true
}
Else
{
//Execute this block if
//condition is false
}
Example Program
#include<iostream>
using namespace std;
int main()
{
int age = 17;
cout<<"\n\n";
if (age>=18){
cout<<"\t Your age is "<< age <<" Your are already Adult";
}
else {
cout<<"\t Your age is "<<age<<" Your are still a Minors";
}
cout<<"\n\n";
cout<<"\t End of Program";
cout<<"\n\n";
return 0;
}
C++ Nested if..else statment
a nested if if..else is an if statement that is the target of another if statement. Nested if..else
statements mean an if statement inside another if statement. The nested if..else statement allows you to check for
multiple test expressions and execute different codes for more than two conditions.
Syntax
if (testExpression1)
{
//statement to be executed if test Expression 1 is true.
}
else if (testExpression2)
{
//statement to be executed if testExpression1 is false and
testExpression2 is true
}
else if (testExpression3)
{
//statement to be executed if testExpression1 and
testExpression2 is false and testExpression3 is true.
}
.
.
else{
//statement to be executed if all test expression are false.
}
Example Program
#include<iostream>
using namespace std;
int main()
{
int number=0;
cout<<"\n\n";
cout<<"\t Positive and Negative Number Checker";
cout<<"\n\n";
cout<<"\t Enter an integer: ";
cin>>number;