> Online tutorial : If and Else Program
Showing posts with label If and Else Program. Show all posts
Showing posts with label If and Else Program. Show all posts

if else statement in c

If else statement

syntax

If(test expression)

{

True block statement(s)

}

Else

{

False block statement(s)

}

Statement –x

If the test epression can be executed first if it is true then true block can be executed

immediately following if statement are executed otherwise false statement are executed if neither case they sattement x can be executed

Eg:

If(code==1)

Boy=boy+1;

Else

Girl=girl+1;

If the code==1 can be executed first if it is true then boy is one incremented can be executed
otherwise false statement are executed that is girl will be one incremented

Program:

Main()

{

Int n,boy,girl;

Printf(“enter the value”);

Scanf(“%d”,&n);

If(n==1)

{

Boy=boy+1;

printf("boy=%d",&boy);

Else

printf(girl%d",girl);

Girl=girl+1;

}

Getch();

}


output:

enter the value 1

boy=1



If and Else Program

/***********************************************/
/* */
/* If demo #3 */
/* */
/***********************************************/

#include <stdio.h>

/***********************************************/

main ()

{ int persnum,usernum,balance;

persnum = 7462;
balance = -12;

printf ("The Plastic Bank Corporation\n");
printf ("Please enter your personal number :");

usernum = getnumber();

if (usernum == 7462)
{
printf ("\nThe current state of your account\n");
printf ("is %d\n",balance);

if (balance < 0) { printf ("The account is overdrawn!\n"); } } else { printf ("This is not your account\n"); } printf ("Have a splendid day! Thank you.\n"); } /**************************************************/ getnumber () /* get a number from the user */ { int num = 0; scanf ("%d",&num); if ((num > 9999) || (num <= 0)) { printf ("That is not a valid number\n"); } return (num); }


if statement in c



The If Statement




Syntax

If(test expression)

{

Statement block;

}


Flow of data


The statement block may be singlesatement or group of statement .if the test expression is true

,the statement block is executed ,otherwise the statement block will be skipped and the execution will jump to the statement x

Eg:

If(category==sports)

{

Marks=marks+bonus;

}

Printf(“%f”,marks);

Program:

Main()

{

Int a,b,c,d;

Float ratio;

Printf(“enter the integer no”);

Scanf(“%d%d%d%d”,&a, &b, &c, &d);

If(c-d!=0)

{

Ratio=(float)(a+b)/float(c-d);

Printf(“%f”,ratio);

}

}

Output:

Enter the integer no

12

23

34

45

Ratio=-3.18