C++ Control Statement.





















C++ Programming Tutorial


If Statements.


When do we want to execute which code in our program? We can decide this. And this is also called the decision-making statement.


If the condition is one of the decision-making statements. They are 4 types.








  1. If Statement.

  2. If else statement.

  3. Else if-else statement.

  4. Nested If statement.


1.If Statement.

If a statement is used to decide whether a certain statement or a block of the statement will be executed or if the given condition is true, then a block of the statement is executed and if the given state is not true Then the block is not executed.

Syntax:-

if(condition) { //statement; }

Flowchart.

If statement


Example:-

#include <iostream> using namespace std; int main() { int number; cout<<"\n Enter any number:"; cin>>number; if(number%2==0) { cout<<"It is even number :"<<number; } return 0; }
Output:
Enter any number:36 It is even number :36 Enter any number:31 //If the condition is false the result not displayed.

2.If else statement.

If a statement is used to decide whether a certain statement or a block of the statement will be executed or if the given condition is true, then 'if' a block of the statement is executed and if the given state is not true Then 'else' the block is executed.

Syntax:-

if(condition) { //statement-1; ( 'If' condition is true statement-1 Executed.) } else { //statement-2; ( 'If' condition is false statement-2 Executed.) }

Flowchart.

If else statement

Example:-

#include <iostream> using namespace std; int main() { int number; cout<<"\n Enter any number:"; cin>>number; if(number%2==0) { cout<<"It is even number :"<<number; } else { cout<<"It is odd number :"<<number; } return 0; }
Output:
Enter any number:31 It is odd number :31 Enter any number:36 It is even number :36



3.Else if-else statement.

If you want to apply another condition with the condition, then you can use else if statement. If one condition is false in the else if statement, another condition is checked. For eg condition-1 true the statement-1 executed and stop if statement and condition-1 false the then condition-2 check-up to end of if-else if statement.

Syntax:-

if (condition -1) { //statement-1; } else if ( condition-2) { //statement-2; } else if ( condition-3) { //statement-3; } else if ( condition-4) { //statement-4; } . . . . . else { //statement-4; }

Flowchart.

If else if ladder statement

Example:-

#include using namespace std; int main() { char ch; int number1,number2; double res; cout<<"\n Enter first number:"; cin>>number1; cout<<"\n Enter second number:"; cin>>number2; cout<<"\n Enter choice(+,-,*,/ and %): "; cin>>ch; if(ch=='+') { res=number1+number2; cout<<"Sum of two numbers :"<<res; } else if(ch=='-') { res=number1-number2; cout<<"Subtraction of two numbers :"<<res; } else if(ch=='*') { res=number1*number2; cout<<"Multiplication of two numbers :"<<res; } else if(ch=='/') { res=number1/number2; cout<<"Division of two numbers :"<<res; } else if(ch=='%') { res=number1%number2; cout<<"Remainder of two numbers :"<<res; } else { cout<<"There is no choice "; } return 0; }
Output:
Enter first number:10 Enter second number:20 Enter choice(+,-,*,/ and %): + Sum of two numbers :30 Enter first number:20 Enter second number:15 Enter choice(+,-,*,/ and %): - Subtraction of two numbers :5 Enter first number:10 Enter second number:5 Enter choice(+,-,*,/ and %): * Multiplication of two numbers :50 Enter first number:10 Enter second number:2 Enter choice(+,-,*,/ and %): / Division of two numbers :5 Enter first number:10 Enter second number:3 Enter choice(+,-,*,/ and %): % Remainder of two numbers :1 Enter first number:10 Enter second number:20 Enter choice(+,-,*,/ and %): $ There is no choice

4.Nested If Else Statement.

The nested if-else statement is used when one or more decisions/conditions have to be checked one if condition inside one if condition are used.

Syntax:-

if (condition -1) { if ( condition-2) { //condition-1 and condition-2 are true. //True Statement block for if condition-2. } Else { //condition-2 is false. //False statement block for if condition-2. } } else { // if condition-1 is false. }

Flowchart.

Nested if statement

 

Example:-

/** * Check whether a triangle is valid or not * using sides and nested if statement. */ #include <iostream> using namespace std; int main() { int side1, side2, side3; cout<<" Enter side1 of triangle: "; cin>>side1; cout<<"\n Enter side2 of triangle: "; cin>>side2; cout<<"\n Enter side3 of triangle: "; cin>>side3; if((side1 + side2) > side3) // if condition one { if((side2 + side3) > side1) // if condition two { if((side1 + side3) > side2) // if condition three { cout<<"Triangle is valid."; } else { cout<<"Triangle is not valid."; // if condition three false } } else { cout<<"Triangle is not valid."; // if condition two false } } else { cout<<"Triangle is not valid."; // if condition one false } return 0; }
Output:
Enter side1 of triangle: 1 Enter side2 of triangle: 2 Enter side3 of triangle: 3 Triangle is not valid. Enter side1 of triangle: 8 Enter side2 of triangle: 2 Enter side3 of triangle: 5 Triangle is not valid.