CSC 183 Chap-4
CSC 183 Chap-4
Programming C
Chapter - 4
#include<stdio.h>
Int main()
{
int num1,num2;
num1 = 25;
num2 = 20;
if(num1 > num2)
{
printf(“num1 is large”);
}
else
{
printf(“num2 is large”);
}
return 0;
}
March 8, 2025 CSC-183 8
C Operators – Logical Operators
• Logical refers to the ways relationships can be connected.
• Used when need more than one condition to take decision.
• Expressions that use logical operators return 0 for false & 1 for true.
• The truth table for logical operators is shown below:
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if((year%4 == 0) && (year%100 != 0) || (year%400 == 0))
{
printf("\n%d is a leap year",year);
}
else
{
printf("\n%d is not a leap year",year);
}
return 0;
}
March 8, 2025 CSC-183 10
C Operators – Assignment Operator
• ‘=‘ used to assign the result of an expression to a variable.
General form: variable_name = expression; c = a+b
• Multiple assignments: x = y = z = 0;
• Compound assignments: The statement of the form var = var
operator expression can be written as var operator =
expression
e.g. x = x + 1 can written as x += 1
• It also called shorthand assignment operator.
Operator Operator Example
+= X=X+1 X += 1
-= X=X-1 X -= 1
*= X=X*Y X *= Y
/= X=X/Y X /= Y
%= X=X%Y X %= Y
a = 10; if (a > b)
x = a;
b = 15;
else
x = ( a > b) ? a : b; x = b;
x = 10
y=7
z=4