esc101: introduction to computing · esc101, programming general form of switch-case switch...

36
Esc101, Programming ESC101: Introduction to Computing Conditional Statements Aug-15 1

Upload: others

Post on 29-May-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

ESC101: Introduction to Computing

Conditional Statements

Aug-15 1

Page 2: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Conditional statements in CThree types of conditional statements in C■ if (condition) action else some-other-action■ if (condition) action■ switch-case

Each action is a sequence of one or more statements

Aug-15 2

Page 3: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

if statement

Aug-15 3

if (expression) statement S1

else statement S2

statement S3

S2S1

S3

true false

if (expression) statement S1

statement S2 S1

S2

Page 4: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Nested if, if-else

if and if-else are also statements, they can be used anywhere a statement or block can be used.

Aug-15 4

if (a <= b) { if (a <= c) { … } else {…}} else { if (b <= c) { … } else { … }}

Page 5: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Else ifA special kind of nesting is the chain of if-

else-if-else-… statements

Aug-15 5

if (cond1) {stmt-block1}

else if (cond2) {stmt-block2}else if (cond3) {stmt-block3}else if (cond4) {stmt-block4}else if …else last-block-of-stmt

if (cond1) {stmt1

} else { if (cond2) { stmt2 } else { if (cond3) { …. } }}

Gen

eral

form

of i

f-els

e-if-

else

Page 6: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

if-else

Aug-15 6

if ((a != 0) && (b != 0)) if (a * b >= 0) printf (“positive”); else printf(“negative”);

if ((a != 0) && (b != 0)) if (a * b >= 0) printf (“positive”);else printf(“zero”);

OUTPUT for a = 5, b = 0 NO OUTPUT!!OUTPUT for a = 5, b = -5

zero

OUTPUT for a = 5, b = 0 NO OUTPUT!!OUTPUT for a = 5, b = -5

negative

Page 7: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Unmatched if and elseAn else always matches closest unmatched if■ Unless forced otherwise using { … }

Aug-15 7

if (cond1) if (cond2) … else …

if (cond1) { if (cond2) … else …}

Page 8: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Unmatched if and elseAn else always matches closest unmatched if■ Unless forced otherwise using { … }

Aug-15 8

if (cond1) if (cond2) … else …

if (cond1) { if (cond2) …}else …

IS N

OT SAME A

S

Page 9: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

# include <stdio.h> int main(){ float a, b; scanf("%f %f",&a, &b); if( _______________ ) if( _________ ) printf("positive\n"); else printf("negative\n"); else printf("zero\n"); return 0; }

Esc101, Programming

Write a program that checks if product of 2 numbers is positive, negative or 0

8/3/2015 9

Page 10: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

# include <stdio.h> int main(){ float a, b; scanf("%f %f",&a, &b); if( _______________ ) if( a*b >=0 ) printf("positive\n"); else printf("negative\n"); else printf("zero\n"); return 0; }

Esc101, Programming

Write a program that checks if product of 2 numbers is positive, negative or 0

8/3/2015 10

Page 11: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

# include <stdio.h> int main(){ float a, b; scanf("%f %f",&a, &b); if( a != 0.0 && b != 0.0 ) if( a*b >=0 ) printf("positive\n"); else printf("negative\n"); else printf("zero\n"); return 0; }

Esc101, Programming

Write a program that checks if product of 2 numbers is positive, negative or 0

8/3/2015 11

Page 12: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

# include <stdio.h> int main(){ float a, b; scanf(“%f %f”,&a, &b); if( _____________ )

printf( “zero\n”); else if (___________) printf( “positive\n”); else printf( “negative\n”); return 0; }

Esc101, Programming

Write a program that checks if product of 2 numbers is positive, negative or 0

8/3/2015 12

Page 13: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

# include <stdio.h> int main(){ float a, b; scanf(“%f %f”,&a, &b); if( a == 0.0 || b == 0.0 )

printf( “zero\n”); else if (a*b > 0.0) printf( “positive\n”); else printf( “negative\n”); return 0; }

Esc101, Programming

Write a program that checks if product of 2 numbers is positive, negative or 0

8/3/2015 13

Page 14: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

A problemWrite a program to check if a year is a leap

year

A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.

14

Page 15: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

#include <stdio.h> int main() { int year; scanf("%d",&year); if(year % 4 == 0) printf("%d is a leap year\n",year); else if(year % 100 == 0) printf("%d is not a leap year\n",year); else if(year % 400 == 0) printf("%d is a leap year\n",year); else printf("%d is not a leap year\n",year); return 0; }

Esc101, Programming

Write a program that checks if a year is a leap year

8/3/2015 15

Input: 2012 Output: 2012 is a leap year

Page 16: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

#include <stdio.h> int main() { int year; scanf("%d",&year); if(year % 4 == 0) printf("%d is a leap year\n",year); else if(year % 100 == 0) printf("%d is not a leap year\n",year); else if(year % 400 == 0) printf("%d is a leap year\n",year); else printf("%d is not a leap year\n",year); return 0; }

Esc101, Programming

Write a program that checks if a year is a leap year

8/3/2015 16

Input: 1900 Output: 1900 is a leap year INCORRECT

Page 17: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

#include <stdio.h> int main() { int year; scanf("%d",&year); if(year % 4 == 0) if(year % 100 == 0) if(year % 400 == 0) printf("%d is a leap year\n",year); else printf("%d is not a leap year\n",year); else printf("%d is a leap year\n",year); else printf("%d is not a leap year\n",year); return 0; }

Esc101, Programming

Write a program that checks if a year is a leap year

8/3/2015 17

Page 18: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

#include <stdio.h> int main() { int year; scanf("%d",&year); if(year % 400 == 0) printf("%d is a leap year\n",year); else if(year % 100 == 0) printf("%d is not a leap year\n",year); else if(year % 4 == 0) printf("%d is a leap year\n",year); else printf("%d is not a leap year\n",year); return 0; }

Esc101, Programming

Write a program that checks if a year is a leap year

8/3/2015 18

Page 19: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

#include <stdio.h> int main() { int year; scanf("%d",&year); if( ( (year % 400 == 0) || (year%4 == 0) ) && (year%100 !=0) ) printf("%d is a leap year\n",year); else printf("%d is not a leap year\n",year); return 0; }

Esc101, Programming

Write a program that checks if a year is a leap year

8/3/2015 19

Page 20: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Printing the day

Aug-15 20

int day; scanf (“%d”, &day); if (day == 1) { printf(“Sunday”); } else if (day == 2) { printf (“Monday”); } else if (day == 3) { printf (“Tuesday”); } else if (day == 4) { printf (“Wednesday”); } else if (day == 5) { printf (“Thursday”); } else if (day == 6) { printf (“Friday”); } else if (day == 7) { printf (“Saturday”); } else { printf (“ Illegal day %d”, day); }

Page 21: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Switch-case statementMulti-way decision

Checks whether an expression matches one out of a number of constant integer (or char) values

Execution branches based on the match found

Aug-15 21

Page 22: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Printing the day, version 2

Aug-15 22

switch (day) { case 1: printf(“Sunday”); break;case 2: printf (“Monday”); break;case 3: printf (“Tuesday”); break;case 4: printf (“Wednesday”); break;case 5: printf (“Thursday”); break;case 6: printf (“Friday”); break;case 7: printf (“Saturday”); break;default: printf (“ Illegal day %d”, day); }

Page 23: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Weekday, version 4

Aug-15 23

switch (day) { case 1: case 7: printf (“Weekend”); break;case 2:case 3: case 4:case 5:case 6: printf (“Weekday”); break;default: printf (“ Illegal day %d”, day); }

Page 24: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

General Form of switch-caseswitch (selector-expr) {case label1: s1; break;case label2: s2; break;...case labelN: sN; break;default : sD;}

Aug-15 24

• default is optional. (= remaining cases)• The location of default does not matter.• The statements following a case label are

executed one after other until a break is encountered (Fall Through)

Expr only of type INTExecution starts at the matching case.

Page 25: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Fall Through…int n = 100;int digit = n%10; // last digitswitch (digit) {default : printf(“Not divisible by 5\n”); break; case 0: printf(“Even\n”);case 5: printf(“Divisible by 5\n”); break;}

Aug-15 25

Answer:EvenDivisible by 5

What is printed by the program fragment?

Page 26: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

A problemWrite a program to check if a number is even

or odd using switch statement

26

Page 27: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

#include <stdio.h> int main() { int n; scanf("%d",&n); switch(____) { case 0: printf("%d is Even\n",n); break; case 1: printf("%d is Odd\n",n); break; default: printf("invalid number\n"); } return 0; }

Esc101, Programming

Write a program that checks if a number is even or odd

8/3/2015 27

Page 28: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

#include <stdio.h> int main() { int n; scanf("%d",&n); switch(n%2) { case 0: printf("%d is Even\n",n); break; case 1: printf("%d is Odd\n",n); break; default: printf("invalid number\n"); } return 0; }

Esc101, Programming

Write a program that checks if a number is even or odd

8/3/2015 28

Page 29: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

#include <stdio.h> int main() { int n; scanf("%d",&n); switch(n%2) { case 0: printf("%d is Even\n",n); break; default: printf("%d is Odd\n",n); } return 0; }

Esc101, Programming

Write a program that checks if a number is even or odd

8/3/2015 29

Page 30: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

#include <stdio.h> int main() { char c; scanf("%c",&c); switch(c) { case (c>= 'A' && c <='Z'): printf("Capital\n"); break; case c>= 'a' && c<='z': printf("small\n"); break; case c>='0' && c<='9': printf("digit\n"); break; default: printf("illegal character\n"); break; } return 0; }

Esc101, Programming

Write a program that checks if a character is capital or not using switch

8/3/2015 30

WRONG One can have only constant case and not logical expressions

Page 31: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

ESC101: Introduction to Computing

Loops

Aug-15 31

Page 32: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Printing Multiplication Table5 X 1 = 55 X 2 = 105 X 3 = 155 X 4 = 205 X 5 = 255 X 6 = 305 X 7 = 355 X 8 = 405 X 9 = 455 X 10 = 50

Aug-15 32

Page 33: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Program…

Aug-15 33

int n;scanf(“%d”, &n);printf(“%d X %d = %d”, n, 1, n*1);printf(“%d X %d = %d”, n, 2, n*2);printf(“%d X %d = %d”, n, 3, n*3);printf(“%d X %d = %d”, n, 4, n*4);….

Too much repetition! Can I avoid it?

Page 34: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Print n x i = ni i = i+1

Input n i = 1

i <=10TRUE FALSE

Printing Multiplication Table

Aug-15 34

Stop

Loop

Loop Entry

Loop Exit

Page 35: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

Printing Multiplication Table

Aug-15 35

scanf(“%d”, &n);int i = 1;

while (i <= 10) { printf(“%d X %d = %d”, n, i, n*i); i = i + 1;}

// loop exited!

Print n x i = ni i = i+1

Input n i = 1

TRUE FALSEi <=10

Stop

Page 36: ESC101: Introduction to Computing · Esc101, Programming General Form of switch-case switch (selector-expr) {case label1: s1; break; case label2: s2; break;... case labelN: sN; break;

Esc101, Programming

While Statement

As long as expression is TRUE execute statement1. When expression becomes FALSE execute

statement 2.

Aug-15 36

while (expression) statement1; statement2;

FALSE

TRUE

statement1

expression

statement2