Statement Of C++

If

Syntax:
if (expression)
statement

Example:
#include
int main()
{
int value=10;
if (value > 0)
cout<<"
Abc("<endl;
return 0;
}

Example: Multiple statement within If
#include
int main()
{
int value=10;
if (value > 0) {
cout<<"The number was positive."<<
endl;
cout<<"
Abc("<endl;
}

return 0;
}

If-Else

Syntax:
if (expression)
statement
else
statement

Example
#include
int main()
{
int value=-10;
if (value > 0) // When if statement has single statement
cout<<"Abc("< else
// When else statement has single statement
cout<<"Abc("<
return 0;
}

Example: Multiple statement within If-Else
#include
int main()
{
int value=-10;
if (value > 0){ // When if statement has multiple statements
cout<<"Abc("<endl;
cout<<"The number was positive."<<
endl;
}
else { // When else statement has multiple statements
cout<<"Abc("<endl;
cout<<"The number was Negative."<<
endl;
}
return 0;
}

Nested If or If-Else

Syntax:
if (expression)
if (expression)
statement
else
statement
else
statement

Example:
#include
int main()
{
int i;
cout<<"Enter either 1 or 2 : ";
cin>>i;
if (i==1)
cout<<"You would go to heaven!";
else {
if (i==2)
cout<<"Hell was created with you in mind";
else
cout<<"How about mother earth!";
}
return 0;
}

Output:
Enter either 1 or 2 : 2
Hell was created with you in mind

If-Else Ladders

Syntax:
if (expression)
statement
else if (expression)
statement

Example:
#include
int main()
{
int day=3;
if (day==1)
cout<<"It\'s Monday";
else if(day==2)
cout<<"It\'s Tuesday";
else if(day==3)
cout<<"It\'s Wednesday";
else if(day==4)
cout<<"It\'s Thursday";
else if(day==5)
cout<<"It\'s Friday";
else if(day==6)
cout<<"It\'s Saturday";
else if(day==7)
cout<<"It\'s Sunday";
return 0;
}

Output:
It\'s Wednesday

For

Syntax:
for ( initialization_statement ; test_expression ; post_expression)
statement

Example:
#include
int main()
{
int number;
cout<<"How many times do you want to see the greeting? ";
cin>>number;
for (int n=0; n cout<<"Hello from C++."<<endl;
return 0;
}

Output:
How many times do you want to see the greeting? 4
Hello from C++.
Hello from C++.
Hello from C++.
Hello from C++.

Example: The above example is written in different way,
#include
int main()
{
int number,n=0;
cout<<"How many times do you want to see the greeting? ";
cin>>number;
for (; n cout<<"Hello from C++."<<endl;
n++;
}
return 0;
}

Nested For

Syntax:
for ( initialization_statement_1 ; test_expression_1 ; post_expression_1)
for ( initialization_statement_2 ; test_expression_2 ; post_expression_2)
statement

Example:
#include
int main()
{
int sum=0;
for (int a=1; a <=3; r++) // outer loop
{
for (int b=1; b<=2; b++) // inner loop
{
sum=a+b;
cout<<"a = "<
While

Syntax:
while (expression)
statement

Example:
#include
int main()
{
int number;
cout<<"Enter a non-zero integer to be checked: ";
cin>>number;
while (number!=0){
if (number > 0)
cout<<"\nThat integer is greater than zero."< else
cout<<"\nThat integer is less than zero."< cout<<"Enter a non-zero integer to be checked: ";
cin>>number;
}
cout<<"\nYou entered zero";
return 0;
}

Do-While

Syntax:
do
statement
while (expression)

Example:
#include
int main()
{
int values[]={1,2,3,4,5}, test, index=0;
do {
test = 5* values[index++];
}while (test < 15);
cout<<"Test = "< return 0;
}

Output:
Test = 15

Case

Syntax:
switch ( expression ) {
case value1:
statement1;
[break;]
case value2:
statement2;
[break;]
case value3:
statement3;
[break;]
................
................
default:
default_statement;
}

Example:
#include
int main()
{
int today=3;
switch (today) {
case 0:
cout<<"It\'s Monday";
break;
case 1:
cout<<"It\'s Tuesday";
break;
case 2:
cout<<"It\'s Wednesday";
break;
case 3:
cout<<"It\'s Thursday";
break;
case 4:
cout<<"It\'s Friday";
break;
case 5:
cout<<"It\'s Saturday";
break;
default:
cout<<"It\'s Sunday";
}
return 0;
}

Output:
It's Thursday

The conditional Operators

Syntax:
expression1 ? expression2 : expression3

Example:
int x, y;
cout<<"Enter two numbers: ";
cin>>x;
y = (x>5 ? 3 : 4); // if x is more than 5, y = 3
cout <<"y = "<

Output:
Enter two numbers: 6
y = 3

Break Statement

When the keyword break is encountered inside any C++ loop, control automatically passes to the first statement after the loop.

Example:
void main()
{
int num, i;
cout<<"Enter a number";
cin>>num;
i =2;
while (i<= num-1)
{
if (num % i == 0)
{
cout<<"Not a prime number";
break;
}
i++;
}
if (i == num)
cout<<"Prime number";
}

Output:
Enter a number : 3
Prime number

Continue Statement

When the keyword continue is encountered inside any C++ loop, control automatically passes to the beginning of the loop.

Example:
void main()
{
for (int i=1; i<=2; i++ )
for (int j=1; j<=2; j++)
{
if (i==j)
continue;
cout<<'\n'< }
}

Using the Exit and Abort Statement

Both the exit and abort statements terminate C++ program.

Example:
#include
#include
int main()
{
for (int n = 0; n > -5; n--) {
if (n == 0)
goto label_1;
cout<<"1 / "< }
exit(0);
label_1 : cerr<<"Ending loop to avoid dividing by 0."< return 0;
}

Goto Statement

We can use the goto statement to jump from location to location in our code.

Syntax:
goto label
label : statement

Example:
#include
#include
int main()
{
for (int n = 0; n > -5; n--) {
if (n == 0)
goto label_1;
cout<<"1 / "< }
exit(0);
label_1 : cerr<<"Ending loop to avoid dividing by 0."<<endl;
return 0;
}