Professional Documents
Culture Documents
Computing Fundamentals: Dr. Muhammad Yousaf Hamza
Computing Fundamentals: Dr. Muhammad Yousaf Hamza
if(num%2==0)
printf("\nThe number %d is an even number",num);
if(num%2!=0)
printf("\nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
To decide even/odd
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number:\n");
scanf("%d",&num);
else
printf("\nThe number %d is an odd number\n",num);
printf(“Bye”);
getchar(); getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
Pre-Class Quiz (MS)
Number of
11 74 9 0 2
Students
Chemistry 1.63
Process 1.65
Physics 1.38
MRE 2.00
Materials 1.33
Mechanical 3.46
Electrical 2.76
System 3.33
Computer / Cyber 5.14
Dr. Muhammad Yousaf Hamza
Flow Chart of If-Else Statements
else
printf("\nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Be careful about Equality (==) and
Assignment (=) Operators
• Dangerous error
– Does not ordinarily cause syntax errors
if (x == 4 )
printf( “You are happy\n" );
• Checks value of x, if it is 4 then it prints You are happy
– Example, replacing == with =:
if ( x = 4 )
printf( “You are happy\n" );
• This always prints You are happy
• 4 is nonzero, so expression is always true.
• Logic error, not a syntax error
if ( x = 0 )
printf( “You are happy\n" );
What’s output?
if(age<=12)
printf("Please go to Pediatrics in Room 10\n\n");
if(age>12)
printf("Please go to Medical Specialist in Room 15\n");
printf(“Allah Hafiz");
getchar();
return 0;
}
if (age<=12)
printf("Please go to Child Specialist in Room 10\n\n");
else
printf("Please go to Medical Specialist in Room 15\n");
printf(“Allah Hafiz");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
The if Statement
• Form 1:
if (expression)
statement1;
next statement;
• Form 2:
if (expression)
statement1;
else
statement2;
next statement;
• && AND
• || OR
• ! NOT
z = !(x || y);
printf ("%d NOR %d = %d\n", x, y, z) ; Try yourself for
z = (x && !y) || (!x && y) ;
x = 0, y = 1
printf ("%d XOR %d = %d\n", x, y, z) ; x = 1, y = 0
x = 1, y = 1
z = !( (x && !y) || (!x && y) ) ;
printf ("%d XNOR %d = %d\n", x, y, z) ;
getchar(); return 0;
} Dr. Muhammad Yousaf Hamza
Logical Operators
More Examples
Statement Expression
• Arithmetic: +, -, *, /, %
– prefix ++i or --i ; increment/decrement before value is used
– postfix i++, i--; increment/decrement after value is used
• Relational and logical: <, >, <=, >=, ==, !=, &&, ||
In the following a C program is written. It contains
BUGS. ENCIRCLE the bugs. You don’t need to write
them in correct form.
$ include (stdio.h) x=2+
int main () 3;
a+1;
int a, b, x, time_in_minutes, 7distance, b = c;
y; printf(“If I solve wisely, this paper is
int weight in kilogram, sum; easy\n’)
j = 7; x = 9 + (j = 10+11);
scanf(“%d”,a); sum = sum + x;
scanf(“%f”,b); y + = 7;
ptintf(“%d:, y);
printf{“What are you doing?\n”}; g = (a)(a)(a); // it calculates cube of a
time_in_minutes = 53
7distance = 23; getchre();
printf(“The result is %d, ” add_result); Return0;
printf(“ENTER Time in Minutes”);
scanf(“%d”, time_in_minutes)
length = 100;
In the following a C program is written. It contains
BUGS. ENCIRCLE the bugs. You don’t need to write
them in correct form.