Members BookmarksPolls Fresher Jobs Funny Photos B.Tech Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

Advertisements


website counter



Conditional Execution programs


Posted Date: 14 Jun 2008    Resource Type: Articles/Knowledge Sharing    Category: Computer & Technology

Posted By: Lenin       Member Level: Diamond
Rating:     Points: 1



Conditional Execution programs

This section contains example programs demonstrating if else and select
statements (also break and continue statements).

This program demonstrates the if else statement.
#include

main()
{
int a , b;

printf("Enter two numbers: ");
scanf("%d%d" , &a , &b);

if (b) printf("%d\n" , a/b);
else printf("... cannot divide by zero\n");
}

Another example of if else statement.
#include
#include

main()

{
int a,b;
char ch;
printf("Do you want to: \n");
printf"Add, subtract, Multiply, or Divide?\n");

/* force user to enter valid response */
do {
printf("Enter first letter: ");
ch=getchar();
printf("\n");
} while (ch!='A' && ch!='S' && ch!='M'
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);

if (ch=='A') printf("%d", a+b);
else if (ch=='S') printf("%d", a-b);
else if (ch=='M') printf("%d", a*b);
else if (ch=='D' && b!=0) printf("%d", a/b);
&& ch!='D');
}

The switch statement is often used to process menu commands.
#include
#include
main()
{
int a,b;
char ch;

printf("Do you want to:\n");
printf("Add, Subtract, Multipy, or Divide\n");
/* force user to enter valid response */
do (
printf("Enter first letter: ");
ch =getchar();
printf("\n");
} while (ch!='A' && ch!='S' && ch!='M'
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);

switch (ch) {
case 'A' : printf("%d", a+b);
break;
case 'S' : printf("%d", a-b);
break;
case 'M' : printf("%d", a*b);
break;
case 'D' : if (b!=0) printf("%d", a/b);
break;
}
}
&& ch!='D');
The statement sequence associated with a case may be empty allowing two
or more cases to share a common statement sequence.
#include
#include

main()
{
char ch;
printf("Enter the letter: ");
ch=getchar();

switch(ch) {
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
printf(" is a vowel \n");
break;
default:
printf(" is a consonant");
}
}

A simple program to show the use of the continue statement.
#include

main()
{
int x ;
for ( x=0 ; x<=100 ; x++) {
if (x%2) continue; /* using modulus operation */
printf("%d\n" , x);

}

This program jumps out of an infinite loop using the break statement.
#include

main()
{
int t ;

for ( ; ; ) {
scanf("%d" , &t) ;
if ( t==10 ) break ;
}
printf("End of an infinite loop...\n");

}






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Control Loop programs
Previous Resource: Structure and Nesting programs
Return to Discussion Resource Index
Post New Resource
Category: Computer & Technology


Post resources and earn money!
 
Related Resources


Contact Us    Privacy Policy    Terms Of Use   

SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.