More Examples For Control Statements

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

More examples for Control statements

C language possesses such decision making capabilities and supports the following statements known as control or decision-making statements.
1. 2. 3.

if statement switch statement Conditional operator statement

if Statement The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically a two-way decision statement and is used in conjunction with an expression.

Syntax if (conditional) { block of statements executed if conditional is true; } else { block of statements if condition false; }
Example

main() { int x=5 if (x > 1) { x=x+10; } printf("%d", x); } ifelse statement The if....else statement is an extension of the simple if statement. The general form is

if (condition) { True-block statement(s); } else { False-block statement(s); }


If the condition is true, then the true-block statement(s), immediately following the if statement are executed; otherwise the false-block statement(s) are executed. void main(void) { int a, b; char ch; printf("Choice:\n"); printf("(A) Add, (S) Subtract, (M) Multiply, or (D) Divide?\n"); ch = getchar( ); printf("\n"); printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); 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); } 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);

} Switch Statement: The switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body. Syntax:

switch (expression) { case item: statements; break; case item: statements; break; case item: statements; break; default: statement; break; }
Example: #include <stdio.h> main( ) { int numb; printf(Type any Number); scanf(%d,&numb); switch(numb % 2) { case 0 : printf("the number %d is even \n", numb); break; case 1 : printf("the number %d is odd \n", numb); break; }

Ternary condition The ? (ternary condition) operator is a more efficient form for expressing simple if statements. It has the following form:

expression1 ? expression2 : expression3


Example:

res = (a>b) ? a : b;
if a is greater than break statement break statement is used to exit from a loop or a switch, control passing to the first statement beyond the loop or a switch. With loops, break can be used to force an early exit from the loop, or to implement a loop with a test to exit in the middle of the loop body. A break within a loop should always be protected within an if statement which provides the test to control the exit condition.) Example: printf("%s\n", grade>60 ? "passed" : "failed"); Example For(i=0;i<=10;i++) { if(i==5){ break; } printf(\n%d,i); } Output:
0 1 2

then

res has the value: a else the: res has value b.

3 4

Continue statement ---------------------------Continue is similar to the break statement but it only works within loops where its effect is to force an immediate jump to the loop control statement. Like a break, continue should be protected by an if statement. Example For(i=0;i<10;i++) { /* start of loop */ if(i==5){ continue; /* when i=5 continue to end of loop and continue loop from to 6*/ } printf(\n%d,i); } Output: 0 1 2 3 4 6 7 8 9 /* end of loop */

Multiway selection means switch statements in C: The general format of a switch statement is as follows:
switch (expression) { case value : statements; break; case value : statements; break; //as many more such cases as needed default : statements; //the open-end option }

An example of program with switch statements in C: Now write another program in C language, which demonstrates the use of switch statement. For this type, using the build-in text editor, a program in C language, named char_id.c. Here is the listing of this C program, after it is typed in and ready for running:
// This C program named char_id.c is to recognise a char using switch stmt // Author ... (type here your name) // Date ... (type here the current date) // For ... (institution, course, purpose)

//---------------------------------------------------------------------------

#include <stdio.h> void main( ) { char input_ch;

//Handles the keyboard input and screen output

printf("\nThis program recognizes a character being typed\n"); printf ("\nType a digit, a vowel (a, e, o, u), or +, -, *, / operator "); printf ("(e.g. *),\nand then press ENTER: ");

scanf ("%c", &input_ch); printf ("\n");

switch (input_ch) { case '+': printf("This is the add operator.\n"); break;

case '-': printf("This is the minus operator.\n"); break; case '*': printf("This is the multiply operator.\n");break; case '/': printf("This is the divide operator.\n"); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': printf ("The character typed is a digit.\n"); break; case 'A': case 'E': case 'I': case 'O': case 'U': case 'a': case 'e': case 'i': case 'o': case 'u':

; case '\n':case '\t': case ' ': case '\v': case '\f': printf ("The character typed is a control one.\n"); break; default : printf("This is an unidentified char.\n"); } printf("\nThank you for using this program. BYE!\n");

Run this program as shown above and try to do it by if else statement. Enter different characters for different run, then modify your program to be used by you to enter different inputs used loops ( for, while, do..while). If you do this, this mean that you understood all the material we discussed in the class. This is your homework before exam 1.

You might also like