Free Download Characteristics of Computer Languages in the form
of power point slides for step by step understanding . Download Now….
IF ELSE STATEMENT
If statement execute single statement and else statement executes set of statements. When the test expressions or condition is true is does nothing otherwise it executes else statement.
Syntax
if (condition)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
Example Program Code:
main ()
{
Int charactercount=0;
Int wordcount=1; ch;
Clrscr()
Printf(“\n\n This program will count number of characters and words you entered”);
Printf(“Type whatever you like .and press enter to see result\n\n”);
While((ch=getche())=!’\r’) {
If(ch==” ’’)
Wordcout++;
Else
Charactercout++;
}
Printf(“entered characters are :”,charactercount);
Printf(“entered words are :”,wordcount);
Getch();
}
WHERE
-“int” it declares integer
-“charactercount” you can give any name you like and it should must explicable. =0 means its value is zero. Here it will add all that characters you enter.
-“printf” what ever you write in it that appears on screen
-“clrscr()” use to clear all code above it and show result only code below it.
-“\r” use for enter
-“ch=getche()” ch is declared as character storage and =getche()means user will give input to program while it will run.
-“=!” not equal
-“ch==” <space>”” the space between inverted commas it will count words compiler will take it as word for example “Science and technology ” we can differentiate these three words due to space between them that’s why ch==”<space>”.
-“getch()” at the end it is important if use want prompt to stay till you close it.
OutPut
It depends what you entered.
Example Program Code:
if-else-if statement
void main(void)
{
int numb;
printf(“Type any Number : “);
scanf(“%d”, &numb);
if(numb > 0) {
printf(“%d is the positive number”, numb);
}
else if(numb < 0)
printf(“%d is the Negative number”, numb);
else printf(“%d is zero”,numb);
}
Out Put
It depends what you entered.
NESTED IF-ELSE STATEMENT
Use of If else inside if else statement
Syntax
{
int x,y;
For (y=1; y<12; y++)
{
For (x=1;x<10;x++)
If(y==x)
Printf(“\xDB”);
Else
If(x==12-y)
Printf(“\xB0”);
Else
}
}