Computer Science: Control Statements: Condition Statements: If-else Statements

Get top class preparation for UGC right from your home: fully solved questions with step-by-step explanation- practice your way to success.

Download PDF of This Page (Size: 136K)

Image of If else statement

Image of if else Statement

‘If else’ Statement

Syntax of the if- else statement

If (condition)

Statement.

else

Statement.

If Else in Flowchart

If else in Flowchart

Example

Write a program that will print the greatest of two numbers.

# include <iostream.h>

void main ()

{

int x, y.

cout << “Enter two numbers” << “\n”.

cin>> x >> y.

if (x > y)

cout << “The bigger number is” << X;

else

cout << “The bigger number is” <<y.

}

Example

Write a program that will print the greatest of three numbers.

# include <iostream.h>

void main ()

{

int x, y, z, l.

cout << “Enter three numbers” << “\n”.

cin >> x >> y>>z.

if (x > y)

l = x.

else

l = y.

if (l > z)

cout << “The greatest number is “<< l.

else

cout << “The greatest number is “<<z.

}

Developed by: