220 likes | 338 Views
Today’s Agenda. Switch case statement Repetition Control Structure for statement while statement do-while statement. Switch. evaluate expression. = const1?. switch (expression) { case const1: block-1; break; case const2: block-2; break; default: block-3; } next_statement.
E N D
Today’s Agenda • Switch case statement • Repetition Control Structure • for statement • while statement • do-while statement
Switch evaluateexpression = const1? • switch (expression) • {case const1: block-1; break;case const2: block-2; break;default: block-3; • } • next_statement block-1 T F = const2? block-2 T F block-3
Switch Example • /* same as month example for if-else */ • switch (month) { case 4: case 6: case 9: case 11: printf(“Month has 30 days.\n”); break; case 1: case 3: case 5: • case 7: • case 8: • case 10: • case 12: • printf(“Month has 31 days.\n”);break; • case 2: printf(“Month has 28 or 29 days.\n”);break; default: printf(“Don’t know that month.\n”); • }
More About Switch • Case expressions must be constant. • case i: /* illegal if i is a variable */ • If no break, then next case is also executed. • switch (a) { case 1: printf(“A”); case 2: printf(“B”); default: printf(“C”); } If a is 1, prints “ABC”. If a is 2, prints “BC”.Otherwise, prints “C”.
switch Summary • It is a multi way decision statement, so it is an alternative to long if-else chain. • If break is not used, then case "falls through" to the next. • const-1 and const-2 are constants or constant expressions also known as case labels. • Case labels must be unique and must end with colon. • block-1 and block-2 are statement lists; may contain zero or more statements. • The default label is optional. It can be used anywhere within switch. • The break statement transfers the control out of the switch.
Program using switch • #include<stdio.h>/* program for arithmetic operation */ • int main() • { float num1,num2; • char choice; • printf(“Enter two numbers”); • scanf(“%f %f”,&num1,&num2); • printf(“\nEnter your choice”); • printf(“\nEnter A for Addition”); • printf(“\nEnter B for Subtraction”); • printf(“\nEnter C for Division”); • printf(“\nEnter D for Multiplication”); • choice = getchar(); • /* continue to next slide */
switch(choice){ • case ‘A’: • res = num1+num2; • printf(“\n Addition of %f and %f” is %f”,num1,num2,res); • break; • case ‘B’: • res = num1-num2; • printf(“\n Sub of %f and %f” is %f”,num1,num2,res); • break; • case ‘C’: • res = num1/num2; • printf(“\n Division of %f and %f” is %f”,num1,num2,res); • break; • case ‘D’: • res = num1*num2; • printf(“\n Mul of %f and %f” is %f”,num1,num2,res); • break;
default: • printf(“Not a valid Choice”); • } • return 0; • } • Note: • Remove all break statements from the program and observe the output if • Choice = ‘A’ ?? • Choice = ‘B’ ?? • Choice = ‘C’ ?? • Choice = ‘D’ ?? • Assume num1 = 125.0 and num2 = 10.0
Revisit Example to calculate gross salary Draw a flow chart for calculating the gross salary of a employee if his basic salary is input through the keyboard. if basic salary is less than Rs.1500 then HRA = Rs. 500 and DA = 90% of basic. If salary is equal or greater than 1500 then HRA = 10% and DA = 95% of basic salary.
START INPUT bs YES is bs < 1500 NO Hra = 500 Da = bs * 90/100 Hra = bs * 10/100 Da = bs * 95/100 Gs = bs + hra + da PRINT GS STOP
Control Structure • Sequential Control Structure • It’s a sequential flow of execution of instructions. • Selection Control Structure • This control structure chooses among alternative program statements. • Repetition Control Structure • This control structure repeats a group of program statements till it satisfies some condition.
Basics of loops • Initialization of a condition variable. • Execution of the loop statements. • Test for a specified value of the condition variable. • Updating the condition variable. • C provides three constructs for loop operations • The for statement • The while statement • The do –whilestatement
for Statement init • Structure: • for (init; test-condn; incr/decr) { • loop_body; • } • Loop body can have a single statement or a group of statements. test F T loop_body updation
for : Single statement • sum = 0; • for (i = 1; i <= 10; i++) • sum + = i; • i = 1; • sum = 0; • for ( ; i <= 10; i + = 1) • sum + = i; • i = 1; • sum = 0 • for ( ; i < 10 ; ) • sum + = i++;
Introducing while loop • sum = 0; • for (i = 1; i <= 10; i++) • sum + = i; sum = 0; i = 0; while (i <= 10) sum += i++;
More Examples on for loop • i=1; • sum = 0; • for( ; ; ) • sum + = i++; • How many times loop statement will be executed? • for (i = 0,j = 7; i < j; i++, j--) • printf(“%d %d”,i,j); • Output???
for: Block statement Option1: for (i = 0, sum = 0; i < 10; i++) { sum = sum + i; printf(“%d”, sum); } Option2: i = 0; sum = 0; for ( ; i<10;i++) { sum + = i; printf(“%d”, sum); }
More Examples: • /* -- what is the output of this loop? -- */ • for (i = 1, sum = 0; i <= 10; i++) • sum + = i*i; printf("%d ", sum); • /* -- what is the output? -- */ • letter = 'a'; • for (c = 0; c < 26; c++) printf("%c ", letter+c);
/* -- what does this loop do? -- */ • sum = 0; for (i = 0; i < 10 && sum < 50; i++) • { sum = sum + i; printf(“%d %d\n”, i, sum); }
Draw a flow chart and write a C program to calculate the gross salary of 50 employees.
Exercise on loops • Write a program to calculate the factorial of number. • Write a program to reverse a number. • Write a program to add the digits of a number. • Write a program to print even and odd numbers separately ranging from 1 to 100.