Sunday, November 5, 2017

Day 4 - Top Down Programming, Logical and Conditional statements

CLASSWORK:
If the denominator is close to zero
if (fabs(denominator) < 0.0001) 
printf("Denominator close to zero"); 
else { 
x = numerator/denominator; 
printf("x = %f \n",x); 
}

Example of an if/else statement
if (code == 10) 
  printf("Too hot - turn equipment off \n"); 
    else 
    { 
      if (code == 11) 
        printf("Caution - recheck in 5 minutes \n"); 
          else { 
            if (code == 13) 
              printf("Turn on circulating fan \n"); 
                else 
                  printf("Normal mode of operation \n"); 
            } 


An equivalent statement is the following switch statement:

switch (code) 

case 10: 
printf("Too hot - turn equipment off \n"); 
break; 
case 11: 
printf("Caution - recheck in 5 minutes \n"); 
break; 
case 13: printf("Turn on circulating fan \n"); 
break; 
default: 
printf("Normal temperature range \n"); 
break; 


HOMEWORK:
Give the value of a after the following set of statements is executed.
int a = 750; 
... 
if (a>0) //if a is greater than zero
if (a >= 1000) //check if a is greater than or equal to 1000
a = 0; //if the previous statements are true, set a equal to zero
else if (a < 500) //if not, check if a is less than 500
a *= 2; //if the previous statement is true, let a equal 2a
else 
a *= 10; //if a is greater than or equal to 500, let a equal 10a 
else 
a += 3; //if a is less than or equal to zero, let a equal 3+a

Since a = 750, a = a*10 => a = 7500



No comments:

Post a Comment