0% found this document useful (0 votes)
45 views15 pages

Conditional Statements

The document discusses different conditional statements in C++ including if, if-else, nested if, and if-else-if statements. It provides examples of the syntax for each statement and explains how to use them to execute code conditionally based on whether a condition is true or false.

Uploaded by

Usaama Abdilaahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views15 pages

Conditional Statements

The document discusses different conditional statements in C++ including if, if-else, nested if, and if-else-if statements. It provides examples of the syntax for each statement and explains how to use them to execute code conditionally based on whether a condition is true or false.

Uploaded by

Usaama Abdilaahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Conditional Statements

If else Statement in C++


• Sometimes we need to execute a block of statements only when a
particular condition is met or not met. This is called decision making, as
we are executing a certain code after making a decision in the program
logic. For decision making in C++, we have four types of control
statements (or control structures), which are as follows:
Types of Conditional Statements
• a) if statement
• b) nested if statement
• c) if-else statement
• d) if-else-if statement
If statement in C++
• If statement consists a condition, followed by statement or a set of statements as shown below:

• if(condition){
• Statement(s);
• }
• The statements inside if parenthesis (usually referred as if body) gets executed only when the
given condition is true. If the condition is false then the statements inside if body are
completely ignored.
Flow diagram of If statement
• #include <iostream>
Example of if statement
• using namespace std;
• int main(){
• int num=70;
• if( num < 100 ){
• /* This cout statement will only execute,
• * if the above condition is true
• */
• cout<<"number is less than 100";
• }

• if(num > 100){


• /* This cout statement will only execute,
• * if the above condition is true
• */
• cout<<"number is greater than 100";
• }
• return 0;
• }
Nested if statement in C++
• When there is an if statement inside another if statement then it is called the nested if statement.
• The structure of nested if looks like this:
• if(condition_1) {
• Statement1(s);

• if(condition_2) {
• Statement2(s);
• }
• }
……
• Statement1 would execute if the condition_1 is true. Statement2 would
only execute if both the conditions( condition_1 and condition_2) are true.
Example of Nested if statement
• #include <iostream>
• using namespace std;
• int main(){
• int num=90;
• /* Nested if statement. An if statement
• * inside another if body
• */
• if( num < 100 )
• {
• cout<<"number is less than 100"<<endl;
• if(num > 50)
• {
• cout<<"number is greater than 50";
• }
• }
• return 0;
• }
• Output:

• number is less than 100


• number is greater than 50
If else statement in C++

• Sometimes you have a condition and you want to execute a block of code if condition is true and
execute another piece of code if the same condition is false. This can be achieved in C++ using
if-else statement.
• if(condition) {
• Statement(s);
• }
• else {
• Statement(s);
• }
Flow diagram of if-else
Example of if-else statement
• #include <iostream>
• using namespace std;
• int main(){
• int num=66;
• if( num < 50 ){
• //This would run if above condition is true
• cout<<"num is less than 50";
• }
• else {
• //This would run if above condition is false
• cout<<"num is greater than or equal 50";
• }
• return 0;
• }
• Output:

• num is greater than or equal 50


if-else-if Statement in C++
• f-else-if statement is used when we need to check multiple conditions. In this control
structure we have only one “if” and one “else”, however we can have multiple “else if”
blocks. This is how it looks:
if-else-if Statement example
• The most important point to note here is that in if-else-if, as soon as the condition is
met, the corresponding set of statements get executed, rest gets ignored. If none of the
condition is met then the statements inside “else” gets executed.
Example
• #include <iostream>
• using namespace std;
• int main(){
• int num;
• cout<<"Enter an integer number between 1 & 99999: ";
• cin>>num;
• if(num <100 && num>=1) {
• cout<<"Its a two digit number";
• }
• else if(num <1000 && num>=100) {
• cout<<"Its a three digit number";
• }
• else if(num <10000 && num>=1000) {
• cout<<"Its a four digit number";
• }
• else if(num <100000 && num>=10000) {
• cout<<"Its a five digit number";
• }
• else {
• cout<<"number is not between 1 & 99999";
• }
• return 0;
• }
• Output:

• Enter an integer number between 1 & 99999: 8976


• Its a four digit number

You might also like