Thursday, September 5, 2013

HOW TO USE IF ELSE STATEMENT

Posted by Unknown On 6:30 AM No comments

Flow Of Control

Statements

Statements are the instructions given to the computer to perform any kind of action. Action may be in the form of data movement, decision making etc. Statements form the smallest executable unit within a C++ program. Statements are always terminated by semicolon.

Compound Statement

A compound statement is a grouping of statements in which each individual statement ends with a semi-colon. The group of statements is called block. Compound statements are enclosed between the pair of braces ({}.). The opening brace ({) signifies the beginning and closing brace (}) signifies the end of the block.

Null Statement

Writing only a semicolon indicates a null statement. Thus ';' is a null or empty statement. This is quite useful when the syntax of the language needs to specify a statement but the logic of the program does not need any statement. This statement is generally used in for and while looping statements.

Conditional Statements

Sometimes the program needs to be executed depending upon a particular condition. C++ provides the following statements for implementing the selection control structure.
  • if statement
  • if else statement
  • nested if statement
  • switch statement

if statement

syntax of the if statement
if (condition)
{
  statement(s);
}
From the flowchart it is clear that if the if condition is true, statement is executed; otherwise it is skipped. The statement may either be a single or compound statement.

THE IF STATEMENT IS USED TO TEST AND COMPARE VALUES OF THE VARIABLES AND DO SOMETHING DEPENDING ON THE RESULT
FOR eg YOU CAN CHECK THE VALUE A PERSON HAS ENTERED IS EQUAL TO A  CERTAIN VALUE

int age;
cout<<" enter an age";
cin>>age;
if(age==16)
cout<<" your age is sixteen"; // if the statement is correct it will execute this statement
else
cout<<"your age is not sixteen "; // if the statement is incorrect it will execute this statement
}

THE FOLLOWING ARE THE DIFFERENT CONDITIONS

 ==       EQUAL  ,
  !=       NOT EQUAL, 
  >         GREATER THAN, 
 <          LESS THAN,
 >=        GREATER THAN OR EQUAL TO,     
<=         LESS THAN OR EQUAL TO,
THE IF STATEMENT ACTUALLY CHECKS THE CONDITIONS AND CONVERT IT TO A TRUE OR FALSE.IF THE AGE THAT WAS ENTERED IS EQUAL TO 18 IN OUR EXAMPLE THEN IT CONVERTS IT TO TRUE AND IF IT IS NOT 18 THEN IT GOES TO THE ELSE STATEMENT

THE PROGRAM WHICH I CREATED TO TEST WHETHER AN ANGLE IS ACUTE ,OBTUSE,RIGHT TRIANGLE














IN THIS CASE I HAVE USED A NESTED IF STATEMENT - IT  MEANS USING IF STATEMENT 
INSIDE ANOTHER IF STATEMENT
If you we an “if statement” in an “if statement” it is called nesting. Nesting ”if statements” can make a program very complex, but sometimes there is no other way. So use it wisely. Take a look at a nested “if statement”
THAT IS ALL ABOUT NESTING IF STATEMENT

Multiple condition testing

It is possible to test two or more conditions at once in an “if statement” with the use of the AND (&&) operator. Example:
if(a>10&&b>20&&c<10)
If a is greater then ten and b is greater then twenty and c is smaller then ten, do something. So all three conditions must be true, before something happens. With the OR ( || ) operator you can test if one of two conditions are true.
example:
if(a==10||b<20)
If a equals ten or b is smaller then twenty then do something. So if a or b is true, something happens.

0 comments:

Post a Comment