Conditional Statements
Conditional Statements
Conditional Statements:
C++ conditional statements allow you to make a decision, based upon the
resultof a condition. These statements are called Decision Making Statements
or Conditional Statements.
1. If statement:
This is the most simple form of decision control statement. In
this form, a set of statements are executed only if the condition
given with if evaluates to true. Its general syntax and flow chart
is as under: -
if(condition)
{
Statements;
}
2. If else:
if (condition1) {
} else if (condition2) {
} else {
Example:-
cout<<”Good Morning”<<endl;
else {
cout<<”Good Evening”<<endl;
3. Nested if-else:-
If we have if-else statement within either the body of an if statement or the body
of else statement or in the body of both if and else, then this is known as nesting
of if else statement. The general form of nested if-else statement is as follows:-
if(condition1)
{
if(condition2)
statements;
else
statements;
}
else
{
if(condition3)
Statements;
else
statements;
}
4. Else if ladder:-
5. Switch:
Use the switch statement to select one of many code blocks to be executed.
The break Keyword:
● When C++ reaches a break keyword, it breaks out of the switch block.
● This will stop the execution of more code and case testing inside the
block.
● When a match is found, and the job is done, it's time for a break.
The default Keyword:
The default keyword specifies some code to run if there is no case match:
Example:-
Int day = 4;
switch(day){
case 6:
cout<<”Today is Saturday”<<endl;
break;
case 7:
cout<<”Today is Sunday”<<endl;
break;
default:
cout<<”Looking forward to the Weekend”<<endl;
}
Ternary Operator / Conditional Operator
The conditional operator takes less space and helps to write the if-else
statements in the shortest way possible.
Syntax:
Example:
Loops
1. For loop
Syntax:
for(initialization; condition;
increment/decrement)
{
statement-block;
}
In for loop we have exactly two semicolons, one after initialization and second
after the condition. In this loop we can have more than one initialization or
increment/decrement, separated using comma operator. But it can have only one
condition.
2. While loop:
while loop can be addressed as an entry
control loop. It is completed in 3 steps.
Variable initialization.(e.g int x = 0;)
condition(e.g while(x <= 10))
Variable increment or decrement (
x++ or x-- or x = x + 2 )
Syntax :
variable initialization;
while(condition)
{
statements;
variable increment or decrement;
}
Example:
void main( )
{
int x;
x = 1;
while(x<=10)
{
cout<<x<<” “;
// below statement means, do x = x+1, increment x by 1 //
x++;
}
}
Output:
1 2 3 4 5 6 7 8 9 10
3. do while loop
In some situations it is necessary to execute the body of the loop before
testing the condition. Such situations can be handled with the help of a do-
while loop. do statement evaluates the body of the loop first and at the end,
the condition is checked using while statement. It means that the body of the
loop will be executed at least once, even though the starting condition inside
while is initialized to be false.
Syntax:
do
{
Statements;
.....
}
while(condition);
Example:
#include<iostream>
void main()
{
int a, i;
a = 5;
i = 1;
do
{
cout<<a*i<<” “;
i++;
}
while(i <= 10);
OutPut:
5 10 15 20 25 30 35 40 45 50
Jump statements
Jump statements are used to transfer the control from one part of the program to
another part.
The various jump statements in C++ are as under:-
1. break statement
2. continue statement
break statement :-
break statement is used inside the loops or
switch statement. This statement causes an
immediate exit from the loop or the switch case
block in which it appears.
It can be written as
break;
The difference between break and continue is that when a break statement is
encountered the loop terminates and the control is transferred to the next
statement following the loop, but when a continue statement is encountered the
loop is not terminated and the control is transferred to the beginning of the loop.