Saturday 19 November 2016

The IF selection Statement

Selection statements are used to choose among alternative courses of action.
 For example, suppose the passing grade on an exam is 60. The pseudocode statement
If student’s grade is greater than or equal to 60 
     Print “Passed”
determines whether the condition “student’s grade is greater than or equal to 60” is true or false. If the condition is true, then “Passed” is printed, and the next pseudocode statement in order is “performed” (remember that pseudocode isn’t a real programming language). If the condition is false, the printing is ignored, and the next pseudocode statement in order is performed. The second line of this selection structure is indented. Such indentation is optional, but it’s highly recommended, as it helps emphasize the inherent structure of structured programs. The C compiler ignores white-space characters such as blanks, tabs and newlines used for indentation and vertical spacing. The preceding pseudocode If statement may be written in C as
Syntax:
If(condition)
{
    statements;
}

Example:
if ( grade >= 60 )
 {   
printf( "Passed\n" );
 }
Notice that the C code corresponds closely to the pseudocode (of course you’ll also need to declare the int variable grade). This is one of the properties of pseudo code that makes it such a useful program development tool. 



3 comments: