C Programming Lecture III
Instructor zgr ZEYDAN
Logical Opeartors
You use C logical operators to connect expressions and /
or variables to form compound conditions.
The C logical expression return an integer (int).
The result has value 1 if the expression is evaluated to
true otherwise it return 0.
C uses the following symbols for the boolean operations
AND, OR ,and NOT.
Operator
&&
||
!
Meaning
logical AND
logical OR
logical NOT
Logical Opeartors
Example:
if ((final>=50) && (average>=60))
printf("Success");
<math.h> functions
x,y
double
The Essentials of Repetition
Loop
Group of instructions computer executes repeatedly while some
condition remains true
Counter-controlled repetition
Definite repetition - know how many times loop will execute
Control variable used to count repetitions
Sentinel-controlled repetition
Indefinite repetition
Used when number of repetitions not known
Sentinel value indicates "end of data"
The Essentials of Repetition
Counter-controlled repetition requires
name of a control variable (or loop counter).
initial value of the control variable.
condition that tests for the final value of the control
variable (i.e., whether looping should continue).
increment (or decrement) by which the control variable
is modified each time through the loop.
The for Loop
For loop statement to
execute a block of
code repeatedly with
various options.
The for Loop
for ( initialization; loopContinuationTest; increment )
statement;
Or
for ( initialization; loopContinuationTest; increment )
{
statement 1;
statement 2;
statement n;
}
The for Loop
There are three parts that are separated by semi-colons in
control block of the C for loop.
initialization expression is executed before execution of the
loop starts. The initialization expression is typically used to
initialize a counter for the number of loop iterations. You can
initialize a counter for the loop in this part.
The execution of the loop continues until the loop condition
is false. This expression is checked at the beginning of each
loop iteration.
The increment expression, is usually used to increase (or
decrease) the loop counter. This part is executed at the end
of each loop iteration.
Increment and Decrement Operators
C provides two operators for incrementing and
decrementing the value of variables.
The increment operator ++ add 1 to its operand.
The decrement operator -- subtracts 1 from its operand
The C increment and decrement operators can be used
either as prefix operator or postfix operators as follows:
variable++;
variable--;
++variable;
--variable;
Increment and Decrement Operators
The C increment operator in both prefix or postfix
contexts is to add 1 to a variable.
But,
the expression ++variable increments variable before its
value is used,
whereas variable++ increments variable after its value has
been used.
Example:
If x = 10, then
printf(
Prints
printf(
Prints
"%d", ++x);
11
"%d", x++);
10
Assignment Operators
Assignment operators abbreviate assignment expressions
i = i + 3; can be abbreviated as i += 3; using the
addition assignment operator
Statements of the form
variable = variable operator expression;
can be rewritten as
variable operator=
Example:
a -= 1
(a =
a *= 2
(a =
a /= 3
(a =
a %= 4
(a =
expression;
a
a
a
a
*
/
%
1)
2)
3)
4)
The for Loop Example 1
Write a C program that calculates the sum of numbers
from 1 to 50.
Then displays the sum as an output.
Use for loop instead of Gauss formula.
Pseudocode:
Start
Initialize sum = 0
For (counter = 1; counter <= 50; counter++)
sum=sum+counter
Display sum
Stop
Example 1
#include <stdio.h>
#include <conio.h>
main()
{
int sum=0,counter;
for (counter=1;counter<=50;counter++)
sum+=counter;
printf("Sum = %d",sum);
getch();
return(0);
}
Example 1 same solution
#include <stdio.h>
#include <conio.h>
main()
{
int sum,counter;
sum=0;
for (counter=1;counter<=50;counter=counter+1)
sum=sum+counter;
printf("Sum = %d",sum);
getch();
return(0);
}
Example 2
Write an algorithm which calculates the average exam
grade for a class of n students (n is given by user).
What are the program inputs?
the exam grades
Processing:
Find the sum of the grades;
count the number of students;
calculate average grade = sum of grades / number of
students.
What is the program output?
the average exam grade
Example 2
#include <stdio.h> #include <conio.h>
main()
{
int sum=0,counter,n,g;
/*n: # of students, g:grade */
float ave;
printf("Enter number of students: ");
scanf("%d",&n);
for (counter=1;counter<=n;counter++)
{
printf("Enter %d student grade: ",counter);
scanf("%d",&g);
sum+=g;
}
ave=sum/n;
printf("Average grade of class = %4.1f",ave);
getch();
return(0);
}
Example 3
Write a C program that calculates the geometrical
mean of numbers given by user.
Then program displays geometrical mean as an
output.
Use pow()function to calculate geometrical mean.
Example 3
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int counter,n,x;
/*n: total numbers, x: each number */
float geo,sum=1; /*geo: geometrical mean */
printf("How many numbers do you have: ");
scanf("%d",&n);
Example 3
for (counter=1;counter<=n;counter++)
{
printf("Enter %d number: ",counter);
scanf("%d",&x);
sum*=x;
}
geo=pow(sum,(1.0/n));
printf("Geometrical mean = %4.2f",geo);
getch();
return(0);
}
Example 4
Write a C program that calculates the factorial of
a number n given by user.
Then program displays the factorial as an output.
Be careful that n>=0
If n<0 then use abort(); function
#include <stdlib.h>
abort();
Example 4
#include <stdio.h> #include <conio.h>
#include <stdlib.h>
main()
{
int n,f=1,c;
/*n: number, f: factorial, c:counter */
printf("Enter positive number to calculate its factorial: ");
scanf("%d",&n);
if (n<0) abort();
else if (n==0);
else for (c=1;c<=n;c++) f*=c;
printf("Factorial of %d is %d",n,f);
getch();
return(0);
}
Homework for next week
Try to write programs that calculate,
C to F conversion for each degrees
1! + 2! + 3! + + n!
Combination
m!
m
=
n
n ! (m n )!