Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda  Basic Logic - Continued...  if else statements  switch Statements  Compound Conditions.

Similar presentations


Presentation on theme: "Agenda  Basic Logic - Continued...  if else statements  switch Statements  Compound Conditions."— Presentation transcript:

1 Agenda  Basic Logic - Continued...  if else statements  switch Statements  Compound Conditions

2 Nested if statements  As you recall, nested if statements are simply if statements within if statements.  If you use nested if statements, you may want to include the else statement to take another action. If so, you must make certain to match all if statements with corresponding else statements

3 Nested if statement  if (age > 5)  {  printf (“Please enter your height: “);  scanf (“%d”, &height);  if (height >= 150)  printf (“OK, you can go\n”); else  printf (“Sorry, you are short\n”);  }  else printf (“Sorry, you can’t go\n); General rule with nested if statements: you should match if with corresponding else

4 Nested if Statements (else if)  The else if statement can be used to simplify nested if statements.  else if statements are particularly useful when specifying different actions based on different ranges of values.  Click here to see system flowchart for an example of an else-if statement Click here

5 Nested if Statements (else if)  if (grade >=80)  printf (“You got an A\n”);  else if (grade >=70)  printf (“You got an B\n”);  else if (grade >= 60)  printf (“You got a C\n”); else if (grade >= 50)  printf (“You got a D\n”);  else printf (“You got an F\n”); Notice that ranges of values are categorized in various statements

6 Nested if Statements (else if)  if (grade >=80)  printf (“You got an A\n”);  else if (grade >=70)  printf (“You got an B\n”);  else if (grade >= 60)  printf (“You got a C\n”); else if (grade >= 50)  printf (“You got a D\n”);  else printf (“You got an F\n”); else statement “catches” rest of range of values.

7 switch Statements  If you are not interested in performing different actions for different ranges of values, but for constant values, then a switch statement can be an efficient alternative to else if statements.  Click here for a system flowchart for an example involving a switch statement. Click here

8 switch Statements  Main () {  int door;  printf (“Enter a door number: ”);  scanf (“%d”, &door);  switch (door) {  case 1:  printf (“You win a car!\n”);  break;  case 2:  printf (“You win a trip!\n”);  break;  default:  printf (“You win a donkey!\n”);  { Braces surround the switch statement, but individual cases or default sections do NOT contain braces

9 switch Statements  Main () {  int door;  printf (“Enter a door number: ”);  scanf (“%d”, &door);  switch (door) {  case 1:  printf (“You win a car!\n”);  break;  case 2:  printf (“You win a trip!\n”);  break;  default:  printf (“You win a donkey!\n”);  { switch statement specifies the variable door to be used for comparison with case constants

10 switch Statements  Main () {  int door;  printf (“Enter a door number: ”);  scanf (“%d”, &door);  switch (door) {  case 1:  printf (“You win a car!\n”);  break;  case 2:  printf (“You win a trip!\n”);  break;  default:  printf (“You win a donkey!\n”);  { Values after case must contain constants (cannot contain other variables, calculations or ranges) Default “catches” all other values than specified in cases above

11 switch Statements  Main () {  int door;  printf (“Enter a door number: ”);  scanf (“%d”, &door);  switch (door) {  case 1:  printf (“You win a car!\n”);  break;  case 2:  printf (“You win a trip!\n”);  break;  default:  printf (“You win a donkey!\n”);  { case sections end with a break statement to get out of the switch statement Notice no break statement with default

12 In Class Exercise Modify Lab #3 question (refer to lab #3 handout) to include a “graduated” charge based on the following ranges of kilometers travelled: Kilometers TravelledPer Kilometer Charge 0 – 50 Kilometers $.20 51 – 100 Kilometers$.25 101 – 500 Kilometers$.27 Otherwise$.30 Have the rest of the input and output structure apply.

13 Compound Conditions  If you need to test for a couple of conditions that exist at the same time, you may need to use compound criteria.  Examples:  Testing if age >= 18 and height == 200  Testing if age > 25 or height > 150

14 Compound Conditions  &&  Test will result in TRUE if all of the sets of conditions are satisfied:  eg. if ( age >= 18 && height ==200 )  ||  Test will result in TRUE if only one of the sets of conditions are satisfied:  eg. if ( age >25 || height > 150 ) Do not use & or | by mistake - these are other operators


Download ppt "Agenda  Basic Logic - Continued...  if else statements  switch Statements  Compound Conditions."

Similar presentations


Ads by Google