The 'if-else' Statement
The 'if-else' statement is another form of 'if' statement. It is used for making two way decisions. In this statement/structure, one condition and two blocks of statement are given. It executes one block of statements when the condition is true and the other when condition is false. In any situation, one block of statements is executed and the other is skipped.The general syntax of simple 'if-else' structure is as follows:
if (condition)
statement - 1 ;
else
statement - 2 ;
In the above syntax, "statement-1" will be executed if the condition is true and statement-2 will be ignored. If the condition is false, "statement-2" is executed and "statement-1" will be ignored.
In case of compound statement, the syntax of 'if-else' structure is as follows:
if (condition)
{
Block-1
}
else
{
Block-2
}
The Block-1 indicates a sent of statements on one side and Block-2 indicates set of statements on another side. The 'if-else' structure may contain a single statement (without curly brackets) on one side and compound statement on another side.
Flowchart of 'if-else' Structure
The flowchart of 'if-else' structure is as follows:
'if-else' Statement |
Write a program that input a number and finds whether it is even or odd.
Example in C
#include < stdio.h >
#include < conio.h >
void main ( )
{
int num ;
clrscr ( ) ;
printf ( " Enter a Number : " ) ;
scanf ( " %d " , &num ) ;
if ( n%2 == 0 )
printf ( " %d is Even Number " , num ) ;
else
printf ( " %d is Odd Number " , num ) ;
getch ( ) ;
}
printf ( " %d is Odd Number " , num ) ;
getch ( ) ;
}
Example in C++
#include < iostream >
usimng namespace std ;
int main ( )
{
int num ;
system ( " cls " ) ;
cout << " Enter a Number : " ;
cin >> num ;
if ( n%2 == 0 )
cout << num << " is Even Number " ;
else
return 0 ;
}
cout << num << " is Odd Number " ;
}
Output of the program
Enter a
Number : 31
31 is odd
Number
|
No comments:
Post a Comment
Thanks For Visiting Here...!! I will Reply you as soon as possible.