Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lesson 3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

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.

Decision-making statement is depending on the condition block need to


be executed or not which is decided by the condition. If the condition is
“True” statement block will be executed, if the condition is “False” then
statement block will not be executed.
In this chapter we are discussing about if-then (if), if –then-
else (if-else), nested if..else, switch statement and ternary
operator. In C++ language there are five types of decision-
making statement.
• if
• if..else
• nested if..else
• Switch
• Ternary operator
The if Statement
The if is the most basic statement of decision-making statement. It tells to program to
execute a certain part of code only if a particular condition is true. It is very frequently used in
allowing the flow of program execution and decision making.

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

Click here for example program


The if..else Statement
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t. but what if we want to do something else if the
condition is false. Here comes the else statement. We can use the else statement with if statement to
execute a block of code when the condition is false.

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;

//condition start here//


if (number>0){
cout<<"\n\n";
cout<<"\t You entered: "<<number<<" A positive integer ";
}
else if (number<0){
cout<<"\n\n";
cout<<"\t You entered "<<number<<" A negative integer";
}
else{
cout<<"\n\n";
cout<<"\t You entered 0";
}
cout<<"\n\n";
cout<<"\t End of the Program";
return 0;

You might also like