Ch2 If Switch Statement

  • Uploaded by: akirank1
  • 0
  • 0
  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ch2 If Switch Statement as PDF for free.

More details

  • Words: 1,703
  • Pages: 30
CMPE 150: Introduction to Computing

If and Switch statements

If statement • The "if statement" is used to break the sequential flow of execution. • Enforces branching in execution according to the result of an expression. – There are two possible paths to take. – The expression determines which path should be taken. Read age T

F

age<=25 ?

Message: "You are young"

Message: "You are mature"

Rest of the program Spring 2008

CMPE 150 – Introduction to Computing

2

If statement • Syntax: if (int_expr) stat_block1

Text in green is optional

else stat_block2

where stat_block is one of the following: stat; • a single statement ; • the null statement • a group of statements enclosed in braces {

stat1; ... statn;

} Spring 2008

CMPE 150 – Introduction to Computing

3

If statement stat0;

stat0

if (expr) { stat1;

T

expr ?

stat1

stat2;

F

stat3

stat2

} else stat3;

stat4

stat4; Notice the indentation.

Spring 2008

CMPE 150 – Introduction to Computing

4

If statement stat0;

stat0

if (expr) { stat1;

T

F

stat1

stat2;

stat2

} stat4;

stat4

How would you move stat1 and stat2 to the "then" branch?

Spring 2008

expr ?

CMPE 150 – Introduction to Computing

5

If statement • Read one character as input; check if it is a digit; if so, convert it to an integer and display twice that number; o/w display an error message. char ch;

int num;

scanf("%c", &ch); if ((‘0’<=ch) && (ch<=‘9’)) { num=ch-’0’; printf("Twice input is %d \n", 2*num); } else printf("Input is not a digit! \n"); Spring 2008

CMPE 150 – Introduction to Computing

6

If statement • Read a number and state whether it is odd or even. int num; scanf("%d", &num); printf("%d is an ", num); if (num%2!=0) printf("odd "); else printf("even "); printf("number.\n"); Spring 2008

CMPE 150 – Introduction to Computing

7

Nested-if statements • Remember the syntax: if (int_expr) stat_block1 else stat_block2

• Statement block contains statements. – "if" is also a statement. – So, you can "nest" one if statement in another. • This structure is called nested-if statements. • You can nest as much as you can. Spring 2008

CMPE 150 – Introduction to Computing

8

Nested-if statements stat0; if (expr1) if (expr2) { stat1; stat2; } else if (expr3) { stat4; stat5; } else stat6; else stat7; stat8;

Spring 2008

stat0 T T

expr1 ? F

expr2 ? T

stat1 stat2

F

expr3 ?

stat4

F

stat7

stat6

stat5

stat8

CMPE 150 – Introduction to Computing

9

Nested-if statements • Remember that the "else" part is optional. – Thus, some of the if statements may not have an "else" part. – Then, it is a little bit tricky to find to which "if" the "else" belongs. • Note that indentation is completely ignored by the compiler, so it does not help.

• The trick is the following: – "else" belongs to the nearest incomplete "if" Spring 2008

CMPE 150 – Introduction to Computing

10

Nested-if statements stat0; if (expr1) if (expr2) { stat1; stat2; } else if (expr3) { stat4; stat5; } else stat6; else stat7; stat8;

stat0 T

expr1 ?

F

IF INDENTATION IS NOT CORRECT, IT IS MISLEADING

Spring 2008

T

F

expr2 ?

T

stat1 stat2

expr3 ?

stat4

F

stat7

stat5

stat8

CMPE 150 – Introduction to Computing

11

Else-if statements • Else-if is a variation of nested-if. • An inner if statement is executed iff all previous if statements have failed.

– Thus, executing an inner if statement implies that all previous expressions were false.

• Syntax:

if (int_expr1) stat_block1 else if (int_expr2) stat_block2 ... else stat_blockn Spring 2008

CMPE 150 – Introduction to Computing

12

Else-if statements if (age<=1) printf("infant"); else if (age<=3) printf("toddler"); else if (age<=10) printf("child"); else if (age<=18) printf("adolescent"); else if (age<=25) printf("young"); else if (age<=39) printf("adult"); else if (age<=65) printf("middle-aged"); else printf("elderly"); Spring 2008

CMPE 150 – Introduction to Computing

13

Else-if statements • Alternative would be: if (age<=1) printf("infant"); if ((1
CMPE 150 – Introduction to Computing

14

Ternary operator • Ternary operator is similar to the "if" statement. But it is an operator, not a statement. • Syntax: int_expr ? value1 : value2

• Eg: a = (b rel="nofollow">c) ? b : c; k = (n!=0) ? m/n : 0;

Spring 2008

CMPE 150 – Introduction to Computing

15

Ternary operator • Note that it is not possible to know at compile time whether value1 or value2 will be used. – Therefore, the type of the expression is the type of the larger value.

• Eg: In the expression below, if the value of b is 9 a = b / (b%2)?2:3.0; the value of a is 4.5 (not 4), because we perform a float division (not integer division) Spring 2008

CMPE 150 – Introduction to Computing

16

Switch statement • If you have multiple cases depending on different values of the same integer expression, switch is easier to use. • Syntax:

switch (int_expr) { case constant_int_value1: stat(s); case constant_int_value2: stat(s); ... default: stat(s); }

• You may have zero or more statements in each case. Spring 2008

CMPE 150 – Introduction to Computing

17

Break statement • Switch statement actually gathers many statements of several cases.

– The case labels denote the specific statement from which the execution of this group of statements begins. – All statements till the end of the group are executed sequentially.

• To separate the cases, break statement is used.

– break breaks the sequential execution of the statements and immediately jumps to the end of the switch statement.

Spring 2008

CMPE 150 – Introduction to Computing

18

Break statement stat0; switch (expr) { case value1: stat1; stat2; case value2: stat3; stat4; stat5; case value3: stat6; break; case value4: stat7; stat8; } stat9; Spring 2008

stat0 stat1 stat2 stat3 stat4 stat5 stat6 stat7 stat8 stat9

If expr happens to be value2

CMPE 150 – Introduction to Computing

19

Switch statement • Define the days of the week as an enumerated type and display them as strings. enum day_type {MON=1,TUE,WED,THU,FRI,SAT,SUN} day; scanf("%d", &day); switch (day) { case SUN: printf("Sunday\n"); break; case WED: printf("Wednesday\n"); break; case TUE: printf("Tuesday\n"); break; case THU: printf("Thursday\n"); break; case FRI: printf("Friday\n"); break; case SAT: printf("Saturday\n"); break; case MON: printf("Monday\n"); break; default: printf("Incorrect day\n"); break; } Spring 2008

CMPE 150 – Introduction to Computing

20

Switch statement • Note that without the "break" statement, execution traverses all cases until the end of the switch statement. • This allows implementation of OR (if you use it properly. • Eg: switch (number) { case 1: case 3: case 5: printf("Odd number \n"); break; case 0: case 2: case 4: printf("Even number \n"); break; } Spring 2008

CMPE 150 – Introduction to Computing

21

Switch statement • As long as the cases are separated with "break"s, their order is not relevant. • "default" is optional. If the default case is not specified and none of the cases holds, no statement is executed; this is not an error. • It is a good practice to put a break even after the last case.

Spring 2008

CMPE 150 – Introduction to Computing

22

Example 1 • Write a code segment that detects whether a number is divisible by 6. if ((num%2==0) && (num%3==0)) printf("%d is divisible by 6 \n",num);

Spring 2008

CMPE 150 – Introduction to Computing

23

Example 2 • Write a code segment that detects whether a number is divisible by 3 or 6. if (num%3==0) if (num%2==0) printf("%d is divisible by 6 \n",num); else printf("%d is divisible by 3 \n",num);

Spring 2008

CMPE 150 – Introduction to Computing

24

Example 3 • Write a program that reads two real numbers and checks if they are equal. (Assume first number is smaller.) #include <stdio.h> #define EPSILON 0.000000001 float r1, r2; int main() { scanf("%f %f", &r1, &r2); if ((r2-r1)<=EPSILON) printf("The numbers are (almost) equivalent"); return 0; } Spring 2008

CMPE 150 – Introduction to Computing

25

Example 4 • Write a program that reads a 3-digit number from the input char-by-char, and displays its square. #include <stdio.h> char c1, c2, c3; int num; int main() { scanf("%c%c%c",&c1,&c2,&c3); num = (c1-'0')*100; num += (c2-'0')*10; num += c3-'0'; num *= num; printf("%d", num); return 0; } Spring 2008

CMPE 150 – Introduction to Computing

26

Example 5.1 • Write a program that defines a type for letter grades (AA,BB,CC,DD,F as 4,3,2,1,0), reads a grade and displays corresponding letter grade. #include <stdio.h> enum grade_type {F,DD,CC,BB,AA}; int main() { enum grade_type g; scanf("%d", &g); printf("%d", g); return 0; } Spring 2008

CMPE 150 – Introduction to Computing

27

Example 5.2 • But, we don't want to display a number. We want to see the letter grade. #include <stdio.h> enum grade_type {F,DD,CC,BB,AA}; int main() { enum grade_type g; scanf("%d", switch (g) { case AA: case BB: case CC: case DD: case F: default: } return 0;

&g); printf("AA \n"); break; printf("BB \n"); break; printf("CC \n"); break; printf("DD \n"); break; printf("F \n"); break; printf("Invalid \n"); break;

} Spring 2008

CMPE 150 – Introduction to Computing

28

Example 5.3 • Could we do it without defining an enumerated type? #include <stdio.h> int main() { int g; scanf("%d", switch (g) { case 4: case 3: case 2: case 1: case 0: default: } return 0;

&g); printf("AA \n"); break; printf("BB \n"); break; printf("CC \n"); break; printf("DD \n"); break; printf("F \n"); break; printf("Invalid \n"); break;

} Spring 2008

CMPE 150 – Introduction to Computing

29

Example 6 • Write a program that reads a character ('a', 'p', or 'v') and radius R. It displays the area or perimeter of a circle with radius R, or the volume of a sphere. #include <stdio.h> #define PI 3.14 3.141592654 char ch; float R; int main() { scanf("%c%f",&ch,&R); switch(ch) { case 'a': printf("Area of circle = %f\n", PI*R*R); break; case 'p': printf("Perimeter of circle = %f\n", 2*PI*R); break; case 'v': printf("Volume of sphere = %f\n", (4/3)*(PI*R*R*R)); (4.0/3) break; default: printf("Invalid input\n"); break; } return 0; }

Spring 2008

CMPE 150 – Introduction to Computing

30

Related Documents

Lo Statement If
June 2020 19
Ch2
April 2020 37
Ch2
May 2020 39
Ch2
November 2019 47

More Documents from ""

Javascript
May 2020 19
Ch8 Structures
April 2020 24
Ch4 Functions
April 2020 24
Cold Fusion Ii
May 2020 21