C++ If-else statement

C++ If-else statement


This statement has a single section, but it has two blocks.
One is true block and the other is false block.

In this condition is checked first. If it is true, then the program control flow goes inside
the braces and executes the block of statements associated with it. If it returns false,
then it executes the else part of a program.

Syntax :

if(condition)
{
true statements;
}
else
{
false statements
}

Example :


#include <iostream.h>
#include <conio.h>

void main()
{
int no;
clrscr();
cout << "\n Enter Number :";

cin>>no;

if(no%2==0)
cout << "\n\n Number is even !";

else
cout << "\n\n Number is odd !";

getch();
}

Output :








When user enter 11 the if condition becomes false and hence the else statement is executed.

Conditional statements/Decision making statements

Conditional Statements

In c langauge there are different types of conditional/decision making statements.

C program executes program sequentially. Sometimes, a program requires checking of certain conditions in program execution. C provides various key condition statements to check condition and execute statements according to conditional criteria.

These statements are called as 'Decision Making Statements' or 'Conditional Statements.'

Followings are the different conditional statements used in C.
(Click on the links given below to go to the different statements)

  1. If statement
  2. Nested if statement
  3. If-else statement
  4. Nested if-else statement
  5. If-else if statement
  6. Switch statement

To go to next topic click next



     Prev                                                   NEXT