COSC 1315 - Fundamentals of Programming
Allan Kochis

  1. Relational operators are used to describe the relationship between operands. The relational operators are :
    1. > greater than
    2. >= greater than or equal.
    3. < less than
    4. <= less than or equal
    5. == equal ( do not confuse this with =).
    6. != not equal

      If the relationship is true the expression returns a 1. If it is false the expression returns a 0.

  2. Logical operators are used to build complex expression. The 3 logical operators are :
    1. && and
      If either operand is zero, and returns a 0. If both operands are non zero and returns a 1.
      ABA && B
      TrueTrueTrue
      TrueFalseFalse
      FalseTrueFalse
      FalseFalseFalse
    2. || or
      If both operands are zero return a zero (0), If either operand is non zero return a 1.
      ABA || B
      TrueTrueTrue
      TrueFalseTrue
      FalseTrueTrue
      FalseFalseFalse
    3. ! not
      Returns 0 if given non zero, returns 1 if given 0.
      A ! A
      TrueFalse
      FalseTrue
    4. Precedence
      The order of precedence is ! && ||
      Fill in the following Truth table
      ABCA || B && C
      TrueTrueTrue     
      TrueTrueFalse     
      TrueFalseTrue     
      TrueFalseFalse     
      FalseTrueTrue     
      FalseTrueFalse     
      FalseFalseTrue     
      FalseFalseFalse     

      AB! (A && B)
      TrueTrue     
      TrueFalse     
      FalseTrue     
      FalseFalse     

  3. Logical Constructs
    1. if
      • Logic
      • Code
        	if(iMonth == 1 )
        	{
        		strMonth="January";
        	}
        
    2. if else
      • Logic
      • Code
        	if(temp < 60)
        	{
        		strText="Turn up the heat!";
        	} else {
        		strText="I am ok";
        	}
        
    3. if else if else
      • Logic
      • Code
        	if(Temp < 60)
        	{
        		strText="Turn up the heat";
        	} else if (Temp > 90)
        		{
        			strText="Turn on the AC";
        		} else {
        			strText="I am ok";
        		}
        
    4. Cascading
      • Logic
      • Code 1.
        	if (day ==0) strName="Sunday";
        	else if (day==1) strName="Monday";
                     else if (day==2) strName="Tuesday";
                          else if (day==3) strName="Wednesday";
                               else if (day==4) strName="Thursday";
                                    else if (day==5) strName="Friday";
                                         else if (day==6) strName="Saturday";
        
      • Code 2.
        	switch (day) {
        		case 0 : strName="Sunday";
        			 break;
        		case 1 : strName="Monday";
        			 break;
        		case 2 : strName="Tuurday";
        			 break;
        		case 3 : strName="Wednesday";
        			 break;
        		case 4 : strName="Thursday":
        			 break;
        		case 5 : strName="Friday";
        			 break;
        		case 6 : strName="Saturday":
        			 break;
                } // end of switch
        


© Allan Kochis .