Introduction to C (3)
Introduction to C (3)
3
Structured Program
Development in C
OBJECTIVES
In this chapter you will learn:
▪ To develop algorithms through the process of
top-down, stepwise refinement.
▪ To use the if selection statement and if...else
selection statement to select actions.
▪ To use the while repetition statement to execute
statements in a program repeatedly.
▪ Counter-controlled repetition and sentinel-controlled
repetition.
▪ Structured programming.
▪ The increment, decrement and assignment operators.
Pseudocode:
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 60
Print “FAIL”
else
Print “PASS”
7
– Pseudocode:
If student’s grade is greater than or equal to 60
Print “Passed
▪ If condition true
– Print statement executed and program goes on to next
statement
– If false, print statement is ignored and the program
goes onto the next statement
if (condition) Example
{
statement 1; if (a < 50)
statement 2; {
… count=count +1;
statement n; sum =sum + a;
} }
Example:
if (a < 50)
{
count=count +1;
sum = sum + a;
if (b > a)
b = 0;
}
if (x > y)
if (y < z)
k++;
else
m++;
else
j++;
(same) (same,correct
indentation)
int main()
{
int x=1, y=2, z=3, k=0, j=0;
if (x > y){
if (y < z)
k++;}
else
j=j+1;
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x1, x2, x3, x4;
int sum=0;
OR
OR
if ( grade >= 60 )
printf( "Passed\n");
else
printf( "Failed\n");
a) 10 b) 9 c) 11 d) 12 e) 13
Write a program that reads in one integer from the keyboard and then Q
prints out a message whether integer is greater than zero or not (by
using ternary operatör) . The screen dialogue should appear as follows:
Input one integer: 14
Greater than zero
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x1;
– Pseudocode:
While there are more items on my shopping list
Purchase next item and cross it off my list
Counter-Controlled Repetition
▪ Counter-controlled repetition
– Loop repeated until counter reaches a certain value
– Definite repetition: number of repetitions is known
if number of repetitions is known a counter is used to
control repetition
Performance Tip
Assignment Operators
c = c + 3;
Assignment operator
The form
variable = variable operator expression;
can be rewritten as
variable operator= expression;
can be abbreviated as
c += 3;
using the addition assignment operator
d -= 4; is equal to (d = d - 4)
e *= 5; is equal to (e = e * 5)
f /= 3; is equal to (f = f / 3)
g %= 9; is equal to (g = g % 9)
▪ Preincrement
– Operator is used before the variable (++c or --c)
– Variable value is changed before the expression it is in is evaluated
▪ Postincrement
– Operator is used after the variable (c++ or c--)
– Expression executes before the variable value is changed
5
6
6
© 2007 Pearson Education,
Inc. All rights reserved.
What will be the value of the variable length Q
after the following code is executed?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x1, x2, x3, x4;
x1=x2=x3=x4=1;
x1=x1+1; %1st
x2+=1; %2nd
x3++; %3th
++x4; %4th
printf("The results are: %d, %d, %d, %d\n",x1,x2,x3,x4);
system("Pause");
return 0; /* successful termination */
}
What is the expected output of each of the following? Q
int i=3;
printf( "%d ", (--i + 3) );
printf( "%d", (i++ + 10) );
Answer:
5 12
What will be the output of the following code segment? Q
int a=1, b=2, c=10;
while (b<c) {
a+=--c-b++;
printf ("%d ", a); }
a) 7 12 15 16 b) 8 13 16 17 c) 8 12 15 17
d) 6 11 14 15 e) 7 11 14 16