Slot05-06-07-Basic Logics
Slot05-06-07-Basic Logics
Slots 05-06-07
Basic Logics
Module-C-Logic
Logic Constructs
Programming Style
Walkthroughs
Programming Fundamentals using C
Review
▪A variable is a name referencing to a memory location.
▪A data type defines: How the values are stored and how the operations on
those values are performed.
▪4 primitive data types in C: int (%d), char (%c), float (%f), double (%d)
▪Data stored are in binary format
▪Declare a variable in C: type var [=initialValue];
▪An identifier begin with a letter or ‘_’, the later symbols can be letters,
digits and ‘_’
▪An user-defined identifier must not a C keyword.
▪Expression is a valid association of constants, variables, operators and
functions.
Basic Logics 2
Programming Fundamentals using C
Objectives
Basic Logics 3
Programming Fundamentals using C
Content
1- Logic constructs
2- Programming Styles
3- Walkthroughs
4- Bonus – Redirect a Program ( a technique is used in the ACM
Contest)
Basic Logics 4
Programming Fundamentals using C
1- Logic Constructs
▪Structured Programming
▪Logic constructs:
▪ Sequence constructs
▪ Selection constructs (1/2, 1/n)
▪ Iteration constructs (loop)
Basic Logics 5
Programming Fundamentals using C
Basic Logics 6
Programming Fundamentals using C
Basic Logics 7
Programming Fundamentals using C
Basic Logics 8
Programming Fundamentals using C
Basic Logics 9
Programming Fundamentals using C
Integral
expression
Condition
?
c1 c2 c3 default
TRUE FALSE
Op1 Op2 Op3 Op4
Operation 1 Operation 2
Basic Logics 10
Programming Fundamentals using C
Selection Constructs: if
if (condition)
{
statements
}
Basic Logics 11
Programming Fundamentals using C
if (condition)
{ statements
}
else
{ statements
}
Basic Logics 12
Programming Fundamentals using C
Basic Logics 13
Programming Fundamentals using C
Basic Logics 14
Programming Fundamentals using C
Basic Logics 15
Programming Fundamentals using C
Basic Logics 16
Programming Fundamentals using C
Basic Logics 17
Programming Fundamentals using C
Basic Logics 18
Programming Fundamentals using C
Selection Constructs:
The switch statement
Selection Constructs:
The switch statement
Basic Logics 20
Programming Fundamentals using C
Analysis
Nouns: expression num1 op num2
Selection Constructs: ➔ double num1, num2; char op
result → double result
switch… Verbs: Begin
Accept num1, op, num2 → “%lf%c%lf”
switch (op)
Write a program that allows { case ‘+’ : result = num1 + num2;
user inputting a simple print out result;
expression containing one of break;
case ‘-’ : result = num1 - num2;
four operators +, -, *, / then print out result;
the result is printed out to break;
the monitor. Input format: case ‘*’ : result = num1 * num2;
print out result;
num1 operator num2, break;
Example: 4*5 case ‘/’ : if ( num2==0)
print out “Divide by 0 “
else
{ result = num1 / num2;
print out result;
}
break;
default: print out “Op is not supported”
Implement it. }
Basic Logics 21
End
Programming Fundamentals using C
Basic Logics 22
Programming Fundamentals using C
Iteration…
Identify a loop:
• Calculate S= 1+2+3+4+5+6+7+ … + 100
➔Some addition are performed ➔ Loop
• Sum of some numbers they are inputted until user
enters 0.
➔Accept and add numbers ➔ Loop
Basic Logics 23
Programming Fundamentals using C
Iteration…
Identify a loop for an expression:
Left side → Initial block
Right side → Condition
An operation and preparations for the next iteration: Tasks in each
iteration.
S=1
i=2
Basic Logics 24
Programming Fundamentals using C
Iteration…
S= 0 , n<=0 S= 0 , n<=0
1 + 3 + 5 +… + n, n is odd n + (n-2) + (n-4) + … + 0
2 + 4 + 6 + … + n, n is even
Initial value:
Initial value: S = 0;
S=0 i = n;
i = (n%2==1)? 1: 2; Condition i>0
Condition i <=n Tasks: S += i;
Tasks: S += i; i -=2;
i+=2;
Basic Logics 26
Programming Fundamentals using C
Iteration…
• Write a program that will print out the ASCII
table.
ASCII code : 0 → 255
Initialize: int code =0
Condition: code <256
Task:
Print the code using 4 format: %c, %d, %o, %X
code = code +1
Basic Logics 27
Programming Fundamentals using C
Iteration…
• Write a program that will calculate 1+2+3+…+n.
Basic Logics 28
Programming Fundamentals using C
no
Condition? Task1
yes
Task2
Task1
Task2
yes no
Condition?
/* Initializations */ /* Initializations */
do
while (condition)
{ statements;
{ statements; }
} while (condition) ;
The condition will be tested before
tasks are performed. The condition will be tested after
tasks are performed.
Basic Logics 29
Programming Fundamentals using C
Basic Logics 30
Programming Fundamentals using C
Iteration…
• Write a program that will print out the sum of integers inputted by
user. The input will terminate if user enters the value 0.
Basic Logics 31
Programming Fundamentals using C
Iteration…
• Write a program that permits user entering some characters until the
ENTER key (code 10) is pressed. The program will print out the number of
digits, number of letters, number of other keys were pressed. Accept a
character: c=getchar();
Nouns:
character inputted → char c, Number of digits → int noDigits
Number of letters → noLetters, Number of other keys → noOthers
#define ENTER 10
Algorithm
Begin The while statement is
noDigits = noLetters = noOthers = c= 0 intentionally used. So, Implement
printf(“Enter a string:”); c=0 is assigned and the it by
While (c!=ENTER) condition c!=ENTER is yourself.
{ accept c; evaluated to TRUE
if ( c>=‘0’ && c <=‘9’) noDigits++;
else if ( (c>=‘a’ && c <=‘z’) || (c>=‘A’ && c <=‘Z’) ) noLetters++;
else noOthers++;
} Input form:
Print out noDigits, noLetters, noOthers abc1234fGH+-*/?(ENTER)
End Basic Logics 32
Programming Fundamentals using C
Iteration…
Basic Logics 33
Programming Fundamentals using C
0 1 2 3 4 5
0 1 2 3 4 5
Basic Logics 34
Programming Fundamentals using C
Basic Logics 35
Programming Fundamentals using C
Basic Logics 36
Programming Fundamentals using C
Basic Logics 37
Programming Fundamentals using C
Basic Logics 38
Programming Fundamentals using C
Basic Logics 39
Programming Fundamentals using C
No flag is
used.
A flag is used.
Basic Logics 40
Programming Fundamentals using C
2- Programming Styles
• Habits in programming
• A well-written program is a pleasure to read. Other programmers can
understand it without significant effort. The coding style is consistent
and clear throughout.
• Develop your own style guide or adopt the style outlined here or
elsewhere, but adopt some style.
Recommendations on
• Naming
• Indentation
• Comments
• Magic Values
• General Guidelines
Basic Logics 41
Programming Fundamentals using C
Basic Logics 42
Programming Fundamentals using C
Basic Logics 43
Programming Fundamentals using C
Basic Logics 44
Programming Fundamentals using C
Basic Logics 45
Programming Fundamentals using C
Basic Logics 46
Programming Fundamentals using C
Basic Logics 48
Programming Fundamentals using C
Basic Logics 49
Programming Fundamentals using C
3- Walkthroughs
• Understand code is a skill of programmer.
• To understand code, we should know how the code
execute.
• To know how the code execute, we should perform each
instruction.
• A walkthrough is
• a record of the changes that occur in the values of program variables as a
program executes and
• a listing of the output, if any, produced by the program.
Ways to perform a walkthrough
• Memory Map → You knew that
• Walkthrough Tables → A simpler way
Basic Logics 50
Programming Fundamentals using C
Walkthroughs: Demo.
a b a+b c
5 2 7 1
6 4 10 9
7 6 13 17
8 8 18 25
9 10 19 33
10 12 22 41
Output
41
Basic Logics 51
Programming Fundamentals using C
Walkthroughs: Demo.
int n, i, S=0;
scanf(“%d”, &n);
for (i=1; i<=n; i+=3)
if (i%2!=0 && i%3!=0) S+=i;
printf(“%d”, S);
What is the output if the input is 15?
n 15
i 1 4 7 10 13 16
S=21
Basic Logics 52
Programming Fundamentals using C
Walkthroughs: Demo.
int m,n, i, S=0;
scanf(“%d%d”, &n, &m);
for (i=m; i<=n; i++) S+=i;
printf(“%d”, S);
What is the output if the input are 8 12?
Walkthroughs: Demo.
Study the following code:
int n=15;
int S=0;
i=1;
while (i<2*n)
{ S+= i;
i*=4;
}
Give your opinion.
a) S=21
b) S= 85
c) A syntax error
Basic Logics 54
Programming Fundamentals using C
N=6 i
Basic Logics 55
Programming Fundamentals using C
Basic Logics 56
Programming Fundamentals using C
4- Redirect a Program
• A characteristic is supported by the operating system and it is used in
the ACM International contest.
Summary
Basic Logics 58
Programming Fundamentals using C
Thank You
Basic Logics 59