Transfer of controls
In C++ programming language, all statements are executed sequentially
from the top to the bottom. With the help of control structure, we can
alter the normal flow of execution of statement of the programs.
Following statements are used to change the normal flow of execution
of statement of the program.
• If statement
• If-else statement
• Switch statement
• Loop statement
C++ IF-else Statement
The C++ if-else statement also tests the condition. It executes if block, if
condition is true otherwise else block is executed.
Syntax
if(condition)
//code if condition is true
}
else
//code if condition is false
C++ If-else Example
#include <iostream>
using namespace std;
int main () {
int num;
cout<<”\n Enter value for Num….”;
cin>>num;
if (num == 500)
{
cout<<"Wel come";
}
else
{
cout<<"Bye bye";
}
return 0;
}
Activity program
➢ Program for Odd/ Even Number
➢ Program for Pass/ Fail on the basis of Average of marks of three subjects
// program for Odd / Even number
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"\n enter value for n....";
cin>>n;
if(n % 2==0)
{
cout<<"\n Even No";
}
else
{
cout<<"\n Odd No";
}
return 0;
}
// program to calculate and display total marks and average of three
//subjects
#include<iostream>
using namespace std;
int main()
{
int s1,s2,s3,total,avg;
cout<<"\n Enter marks of Subject1...";
cin>>s1;
cout<<"\n Enter marks of Subject2...";
cin>>s2;
cout<<"\n Enter marks of Subject3...";
cin>>s3;
total=s1+s2+s3;
avg=total/3;
cout<<"\n Obtained marks="<<total;
cout<<"\n Average="<<avg;
if(avg>=60)
{
cout<<"\n Remarks=Pass";
}
else
{
cout<<"\n Remarks=Fail";
}
return 0;
}
References / links
https://www.javatpoint.com/cpp-if-else
https://www.geeksforgeeks.org/c-sharp-decision-making-else-else-
ladder-nested-switch-nested-switch/
https://www.javatpoint.com/cpp-switch
Channel name
Mjaved Learning
Video links
If-Else Statement in C/C++ --Programming Basics
https://www.youtube.com/watch?v=F1Tvm-
vgSbM&list=PLtgDiTatpOXAKHv9xXzchEanNWYSYZoAv&index=20
Channel link
For more video lectures click on the following
https://www.youtube.com/channel/UCZ3bJokA8Jmj6QJ11h7bGQg
For any query or suggestion, students can send E-mail at:
mjavedpk2000@yahoo.com
Thanks