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. if statement 2. switch statement 3. 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 Example main() int x=5; if (x > 1) x=x+10; printf("%d", x); if (conditional) block of statements executed if conditional is true; else block of statements if condition false; if else 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( ); /* getchar() is function which read only one character and assign it to ch */ 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 : (Example) 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_1: case item_2: case item_n: default: statements; statements; statements; statement; /* next statement in sequence will be executed after any break */ item_1, item_2,.item_n are fixed integer or character or any enumerated values, and not variable names.
Example: #include <stdio.h> main( ) int numb; printf( Type any Number: ); scanf( %d,&numb); switch(numb % 2) /* selector can be an expression or just one variable*/ case 0 : printf("the number %d is even \n", numb); case 1 : printf("the number %d is odd \n", numb); /* note that default is optional */ Ternary condition The? (ternary condition) operator is a more efficient form for: expressing simple if statements. It has the following form (expression and not a complete statement): expression1? expression2 : expression3 Example: (Assignment statement) int a=5; int b=13; int res; res = (a>b)? a : b; if a is greater than b (is true) then res has the value of: a else the: res has value of b. Example: printf("%s\n", grade>60? "passed" : "failed"); It means: if grad > 60 then print "passed" else print "failed"
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 (Program segment) for(i=0;i<=10;i++) if(i==5) printf( \n%d,i); /* next statement in sequence */ Output: 0 1 2 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: ( program segment ) 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); /* if i==5 branch to this point and continue the loop until end loop Output: 0 1 2 3 4 6 7 8 9 terminated successfully */
Homework-2 ( Prof. Dr. Yahia Halabi) You should submit it before exam-1 (Maximum: 12 PM 5 PM)- one week after the first exam scheduled to you ( exactly next Thursday)). Multiway selection means switch statements in C: The general format of a switch statement is as follows: switch (expression) case value : statements; case value : statements; //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> //Handles the keyboard input and screen output void main( ) char input_ch; printf("\nthis program recognizes a character being typed\n"); scanf ("%c", &input_ch);
printf ("\n"); switch (input_ch) case '+': printf("this is the add operator.\n"); case '-': printf("this is the minus operator.\n"); case '*': printf("this is the multiply operator.\n"); case '/': printf("this is the divide operator.\n"); 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"); 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"); 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 runs, then modify your program to be used by you to enter different inputs and use also 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 second homework before exam. Extra grades if your program contains the following: Enter 1 for variable_1 if you want to use switch or enter 2 to use( if or if else or if else if) and : Also: enter for another variable_2 if you want to use (for), 2 if you want to use (while) and 3 if you want to use (do.. while).