Sunday, 19 July 2015

if Statement :


In C++ if statement is used to check the truthness of the expression. Condition written inside If statement consists of a boolean expression. If the condition written inside if is true then if block gets executed.

Syntax

The syntax of if statement is as follows :

             if (condition){
                  statement;
                                   }

The above syntax is used for single statement. A set of statements can also be made conditional. In this case, these statements are written in curly brackets {  } . The set of statements is also called compound statements.

                The syntax for compound statements in if statement is as follows :

            if (condition){
                statement 1;
                statement 2;
                .
                .
                statement n;
                                 }

Example :  

void main(){
int marks;
cout<<"Enter Marks : ";
cin>>marks;
if (marks>40)                        //////////////////////////Condition/////////////
cout<<"Congratulations.....You Have Passed.";               ////////////////Statement////////////
getch();
}

If user enter more than 40 marks then statement is excuted otherwise display will be empty.
Firstly check if condition, condition is fulfil then enter into a body otherwise program will be terminated.

Output :

Enter Marks : 80
Congratulations.....You Have Passed.

Again Run the program,

Enter Marks : 30
///program will be terminated and display screen will be empty.

No comments:

Post a Comment