Friday 30 August 2013

Simple “if” statement | C Programming Language Tutorial pdf



(1) Simple “if” statement:

The 'if' statement is a powerful decision making statement and is used to control the flow of execution of statements.
Syntax:
                                                   if (Condition or test expression)
if (Condition or test expression)      {
Statement;                                   OR  Statement;
Rest of the program                       }
                                                  Rest of the program;

=> It is basically a “Two-way” decision statement (one for TRUE and other for FALSE)
=> It has only one option.
=> The statement as executed only when the condition is true.
=> In case the condition is false the compiler skips the lines within the “if Block”.
=> The condition is always enclosed within a pair of parenthesis ie ( ) .
=> The conditional statement should not the terminated with Semi-colons (ie ;)
=> The Statements following the “if”-statement are normally enclosed in Curly Braces ie { }.
=> The Curly Braces indicates the scope of “if” statement.
=> The default scope is one statement. But it is good practice to use curly braces even with a single statement.
=> The statement block may be a single statement or a group of statements.
=> If the Test Expression / Conditions is TRUE, the Statement Block will be executed and executes rest of the program.
=> If the Test Expression / Condition is FALSE, the Statement Block will be skipped and rest of the program executes next..

=> Flow chart for “ if ” statement:
Write a program to check equivalence of two numbers. Use “if” statement:
# include<stdio.h>
# include<conio.h>
void main( )
{
int m,n;
clrscr( );
printf(“\n Enter two numbers:”);
scanf(“%d %d”, &m, &n);
if((m-n)= =0)
printf(“\n two numbers are equal”);
Cond
ition
if block
Rest of the program
getch();
}

Output:
Enter two numbers: 5 5

Two numbers are equal.
? The two numbers are entered.
? They are checked by “if”-Statement whether their difference is „0? or not.
? If „true? the numbers are equal and the message is displayed as shown in the output.

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved