Lecture #3: Click To Edit Master Subtitle Style

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Lecture #3

5/5/12

Programming Master subtitle style Click to edit Fundamentals

OUTLINE FOR TODAY

Using Selection Statements.


If and if-else statements. Nested if-else statement. Case switch statement. Conditional Operators.

Arithmetic Operators. Repetition


5/5/12

Decision making if and if else statements.

#include "stdio.h" int main() { int n; printf("Enter


5/5/12

Write a program If Basic salary is <1500 HRA=10%of BS DA=90%of BS If Basic salary is <1500 HRA=500 DA=98%of BS Employees salary is entered through 5/5/12 key board. Find Gross Salary.

Nested if else statement.


#include "stdio.h" int main() { int n; printf("Enter integer>\n"); scanf("%d",&n); if(n>0)


5/5/12

else if(n<0) { if (n%2) { printf("number enterd is -ve even\n"); } else


5/5/12

A Simple Calculator

int main() { int a,b.choice,c; scanf("%d %d %d ",&a,&b,&choice); if(choice===0) { c=a+b; } else { if(choice==1)

5/5/12

Simple Calculator Contd..


else { if(choice==2) { c=a*b; } else


5/5/12

5/5/12

5/5/12

Case switch statement.

#include "stdio.h" int main() { int n1.n2,result,choice; scanf(%d %d %d ",&n1.,$&n2,&choice); switch(choice) { case 1: result=n1+n2; break; case 2: result=n1-n2;

5/5/12

Case switch statement Contd


case 4: if(n2==0) {
5/5/12

Conditional Operators.

5/5/12

1st Precedence : 2nd Precedence: 3rd Precedence: In case of tie, Evaluate from left to right E.g M=a+b+c/5 In C M= (a+b+c)/5 Z=10*3%1+5/2-3
5/5/12

Evaluate it: i=2*3/4+4/4+8-2+5/8 i=6/4+4/4+8-2+5/8 i=1+4/4+8-2+5/8 i=1+1+8-2+5/8 i=1+1+8-2+0 i=2+8-2+0 i=10-2+0


5/5/12

Repetition Statements.

For Loop. Nested For loops. While Loop. Do while Loop. Break and Continue Statements

5/5/12

For Loop

5/5/12

For Loop.

#include "stdio.h" int main() { int x; for(x=0;x<10;x++) {

printf("%d\n",x); }
5/5/12

For Loop : Example


#include "stdio.h" int main() { int sum=0; int number; for(number=2;number<=100;number+=2) { sum+=number; } printf("sum is %d\n",sum); 5/5/12

Write a program to convert decimal to binary number (0-15).

5/5/12

Nested For loops.

#include "stdio.h" int main() { int x=0; int y=0; for(x=0;x<4;x++) { for(y=0;y<=x;y++) { printf("*"); }

5/5/12

Nested For Loop: Example

#include "stdio.h" int main() { int x,y; for(x=0;x<6;x++) { for(y=0;y<6;y++) { if(x==0||y==0||x==5||y==5) printf(*); else printf(" ");

5/5/12

Nested For Loop: Example

#include "stdio.h" int main() { int x,y; for(x=0;x<4;x++) { for(y=0;y<4;y++) { printf("%d",x+y); } printf(\n");

5/5/12

While Loop

int main() { int i=1; while(i<=10) { printf (%d\n",i); i++;


5/5/12

Sentinel Controlled Loops.


void main (void) { int count; clrscr( ); printf ( Type in a phrase: \n ); while ( (ch = getch ( ) ) ! = \r ) { count + + ;
5/5/12

printf ( \n Character Count is = %d ,

Do While Loop:

int main() { int i=1; do { printf (%d\n",i); i++;

Enter Loop

Body of the Loop

Increment

F
Test 5/5/12 Exit Loop

Comparison of Loop Choices.


Kind
Counting loop

When to Use

C Structure

We know how many loop for, while repetitions will be needed in advance. Input of a list of data ended by a special value while while

Sentinel-controlled loop Loop

There is no loop variable to initialize or increment.

Input validation loop

Repeated interactive input do-while of a value until a desired value is entered. Repeated processing of datawhile, for until a desired condition is 5/5/12 met.

General conditional loop

Break Statements

#include "stdio.h" int main() { int x; for(x=1;x<=10;x++) { if(x==5)


5/5/12

Continue Statement

#include "stdio.h" int main() { int x; clrscr(); for(x=1;x<=10;x++) {


5/5/12

Surprise

5/5/12

You might also like