You are on page 1of 33

TA ZC142 Computer Programming Lecture 4 Date: 18/01/2013

Announcement

Link for the lab server is active on the course web page on taxila. Some lab sheets are also uploaded on the course web page in news forum.
Some Assignments are also uploaded on the course web page in news forum.

Last Lecture

Flow Charts and Algorithms Programming Examples


If statement If-else statement Nested if statement

Todays Agenda

Switch case statement


Repetition Control Structure for statement while statement do-while statement

Control Structure

Sequential Control Structure


Its 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.

if (condition) true block statement(s); else false block statement(s);

If - else Statement
T condition F

True Block statement

False Block statement

else allows choice between two mutually exclusive actions without re-testing condition.

Chaining ifs and elses if(condition 1) statement-1; else if(condition 2) statement-2; else if(condition 3) statement-3; else if(condition 4) statement-4; else default-statement; next_statement;

Example if (month == 4 || month == 6 || month == 9 || month == 11) printf(Month has 30 days.\n); else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) printf(Month has 31 days.\n); else if (month == 2) printf(Month has 28 or 29 days.\n); else printf(Dont know that month.\n);

Program using if-else ladder


/* Checking a character whether it is capital letter or small letter or special symbol*/ #include<stdio.h> int main() { char ch; printf("Enter a char:"); scanf("%c",&ch); if(ch>='A' && ch<='Z') printf("\n Capital letter"); else if(ch>='a' && ch<='z') printf("\n Small letter"); else if(ch>='0' && ch<='9') printf("\n Digit"); else printf("\n Special Symbol"); return 0;

if (condition 1) { if (condition 2) { statement(s) 1; } else { statement(s) 2; } } else { statement(s) 3; } next_statement;

Nesting of if else statements

Switch

evaluate expression

switch (expression) { case const1: block-1; break; case const2: block-2; break; default: block-3; } next_statement

= const1?

block-1

F
= const2?

block-2

block-3

/* 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(Dont know that month.\n); }

Switch Example

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: If a is 1, prints ABC. printf(A); If a is 2, prints BC. case 2: Otherwise, prints C. printf(B); default: printf(C); }

switch Summary
1. 2.

3.

4.

5.

6.

7.

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

What if you have to calculate Gross Salary of 50 employees.

Control Structure

Sequential Control Structure Its 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
1. 2.

3.

4.

5.

6.

7.
8.

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 while statement

for Statement
init

Structure:
for (init; test-condn; incr/decr)

test

{ loop_body; } Loop body can have a single statement or a group of statements.

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.

Exercise

Compute xn/n!. Write a program to print whether a number is prime or not.

Exercise

Generate and print the first n terms of a fibonacci series. 0, 1, 1, 2, 3, 5, 8, 13,


Compute the series given below 0! + 1! + 2! + 3! + 4! + n terms

You might also like