Control flows
These are statements which are used to check the condition of a Boolean expressions which is true or false. These statements are used in a broad way to decide whether the statement is returning a true or false value.
** Selection statements: if and else **
The if keyword is used to execute a statement or block, if, and only if, a condition is fulfilled. Its syntax is:
if (condition) statement
Here, condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is not executed (it is simply ignored), and the program continues right after the entire selection statement.
For example, the following code fragment prints the message (x is 100), only if the value stored in the x variable is indeed 100:
if (x == 100)
cout << "x is 100";
If x is not exactly 100, this statement is ignored, and nothing is printed. If you want to include more than a single statement to be executed when the condition is fulfilled, these statements shall be enclosed in braces ({}), forming a block:
if (x== 100)
{
cout << "x is ";
cout << x;
}
Selection statements with if can also specify what happens when the condition is not fulfilled, by using the else keyword to introduce an alternative statement. Its syntax is:
if (condition) statement1 else statement2:
where statement1 is executed in case condition is true, and in case it is not, statement2 is executed.
For example:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
This prints x is 100, if indeed x has a value of 100, but if it does not, and only if it does not, it prints x is not 100 instead.
Several if + else structures can be concatenated with the intention of checking a range of values. For example:
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
This prints whether x is positive, negative, or zero by concatenating two if-else structures. Again, it would have also been possible to execute more than a single statement per case by grouping them into blocks enclosed in braces: {}.
Example for these If condition is:
//ConditionFlowStatements.cpp
#include<iostream>
using namespace std;
int main()
{
int a,b
cout<<"\n Enter the value of an and b"<<endl;
cin>>a>>b;
if(a>b){
cout<<"\n The greatest number among these is "<<a<<endl;
}
else{
cout<<"\n The greatest number among these is "<<b<<endl;
}
return 0;
}
Another Example of Conditional Statements is:
Program to find the average of the marks stored by a student is:
//ConditionFlowStatements1.cpp
#include<iostream>
using namespace std;
int main()
{
int m1,m2,m3,m4,m5,m6;
cout<<"\n Enter your marks for your grade"<<endl;
cin>>m1>>m2>>m3>>m4>>m5>>m6;
if(m1>70 && m2>70 && m3>70 && m4>70 && m5>70 && m6>70 ){
cout<<"\n Your grade is : A"<<endl;
}
else if(m1>70 && m2>70 && m3>70 && m4>70 && m5>70 && m6>70 ){
cout<<"\n Your grade is : B"<<endl;
}
else if(m1>60 && m2>60 && m3>60 && m4>60 && m5>60 && m6>60 ){
cout<<"\n Your grade is : C"<<endl;
}
else if(m1>50 && m2>50 && m3>50 && m4>50 && m5>50 && m6>50 ){
cout<<"\n Your grade is : D"<<endl;
}
else(m1>40 && m2>40 && m3>40 && m4>40 && m5>40 && m6>40 ){
cout<<"\n Your grade is : Fail(E)"<<endl;
}
return 0;
}
Please Subscribe and follow Programming languages join now or Subscribe!!
Please follow the notes given here and Share this Blog. For any comments or any mistakes report or comment in comment section of this blog.
ALL THE BEST!!!
2 Comments
Valuable information shared in an easily understandable way. Looking forward to more.
ReplyDelete👍👍👍👍
ReplyDelete