LOGIC DEVELOPMENT
LOGIC DEVELOPMENT
LOGIC DEVELOPMENT
Unit 3: Logic Development
 Problem Analysis
 Variables
 Declaration of variables
 Assigning Values to variables
 Expressions and Manipulation
 Arithmetic Expression
 Evaluation of Expression
 Data types
 Integer
 Floating Point
 Void
 Character
 Operators
 Arithmetic Operator
 Relational Operator
 Logical Operator
 Assignment Operator
 Increment & decrement Operator
 Conditional Operator
 Bitwise Operator
 Special Operator
 I/O Statements (printf & scanf)
 Assignment Statement
 Problem Analysis in LOGIC DEVELOPMENT
 It consists of both review and detailed task analysis in order to minimize wastage of time, money, manpower and material. It also consists of processing the input and output data, the general structure of input and output data, representation of various mathematical equations etc.
 In problem analysis, given problem is divided into sub programs. This process is repeated until, you get a set of sub problems that are small, easy to understand and manage. After developing solution to each small problem, integrating their solution properly gives total solution
 BASIC STRUCTURE OF C
Documentation Section 
Link Section 
Definition Section 
Global Declaration Section 
Main() Function Section
{ Declaration Part
Executable Part } 
Subprogram section
Function 1 Function 2 (User defined functions) ———— Function n 
Character set: The characters that can be used to form words, numbers and expressions depend upon the computer on which the program is run is known as character set.
The characters in c are grouped into following categories:
Letters [A…. Z, a…z]
Digits [0…. 9]
Special characters [@, #, $,! Etc.]
White spaces [Blank space, tab, new line]
Trigraph characters:
They are the sequences to provide a way to enter certain characters that are not available on some keyboard. Each trigraph sequences consist of three characters (two question marks followed by another character).
Example:
?? = #Number sign
?? ( [Left bracket
??) ] Right bracket
In a c program the smallest individual units are known as c tokens LOGIC DEVELOPMENT
Six types of tokens:
 Keywords: if, while, int
 Constants: 15.5,12,100
 Identifiers: main, amount
 Strings: “abc”, “year”
 Special symbols: [], {}
 Operators: +, , *, /
 All keywords have fixed meaning and these meanings can’t be changed. They serve as basic building blocks for program statements.
 Identifiers refer to the name of variables, functions and arrays. These are userdefined names and consist of a sequence of letters and digits, with a letter as a first character.
Rules for Identifiers:
 First character must be an alphabet.
 Must consist of only letters, digits or underscore.
 Only first 31 characters are significant.
 Cannot use a keyword.
 Must not contain white space.
Constants in c refer to fixed values that do not change during the execution of a program LOGIC DEVELOPMENT

Constants
 Numeric
 Integer
 Real
Character
 Single character
 String constant
Backslash character constants
Constants 
Meaning 
\a  Audible alert 
\b  Backspace 
\n  New line 
\t  Horizontal tab 
\v  Vertical tab 
\0  Null 
 Variables:
A variable is a data name that may be used to store data value.
A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program.
Rules for Variables:
 First character must be an alphabet.
 Must consist of only letters, digits or underscore.
 Only first 31 characters are significant.
 Cannot use a keyword.
 Must not contain white space.
Syntax to Declare Variable:
<DATA TYPE> <Variable name>;
Example
 int I;
 float j;
 chat c;
Assigning Value to Variable:
 i = 20;
 j = 20.36;
 c = ‘a’;
Data types:
 Primary data type
 Derived data type
 User defined data type
Basic data types
Data type  Range 
char  128 to 127 
int  32768 to 32767 
float  3.4e38 to 3.4e+38 
double  1.7e308 to 1.7e+308 
int:
This int keyword is used to declare integers, whole numbers either positive or negative. Most of the compilers treat this with a size of 2 bytes. i.e., integer of 16 bits length. The following statement shows how the variables of int type are declared.
 int var1;
 int var11 = 10; //initialization
float:
This keyword float is used to declare floatingpoint decimal numbers. A sample declaration would be,
 float var2;
double:
This data type is used to represent double precision floating point number.
char:
This keyword is used to declare characters. The size of each character is 8 bits. i.e., 1 byte. The characters that can be used with this data type are ASCII characters.
 Expression and Manipulation:
 Arithmetic Expression
 An arithmetic expression is a combination of variables, constants & operators arranged as per the syntax of the language.
 Evaluation of Expression using Arithmetic Operators
 Expression are evaluated using an assignment statement of the form :
Variable = expression
Variable is any valid C variable name. When the statement is encountered the expression is evaluated first and the result will be replaced to variable.
X = a * b + c;
Y = a / b * c;
 Operators:
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation. Operators are used in C language program to operate on data and variables. C has a rich set of operators, which can be classified as LOGIC DEVELOPMENT
 Arithmetic operators
 Relational Operators
 Logical Operators
 Assignment Operators
 Increment and Decrement Operators
 Conditional Operators
Arithmetic Operators
All the basic arithmetic operations can be carried out in C. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language. Unary operations operate on a single operand, therefore the number 5 when operated by unary – will have the value –5.
Operator  Meaning 
+  Addition or Unary Plus 
–  Subtraction or Unary Minus 
*  Multiplication 
/  Division 
%  Modulus Operator 
Examples of arithmetic operators are
x+ y
x – y
x + y
a * b + c
a * b
etc.,
here a, b, c, x, y are known as operands. The modulus operator is aj special operator in C language, which evaluates the remainder of the operands after division
Relational Operators
Using relational operators we can direct the computer to compare two variables. The C relational operators are summarized below, with their meanings. Pay particular attention to the equality operator; it consists of two equal signs, not just one.
Operator  Meaning 
<  less than 
<=  less than or equal to 
>  greater than 
>=  greater than or equal to 
==  equal to 
! =  not equal to 
A simple relational expression contains only one relational operator and takes the following form.
Exp1 relational operator exp2
Where exp1 and exp2 are expressions, which may be simple constants, variables or combination of them. Given below is a list of examples of relational expressions and evaluated values.
6.5 <= 25 TRUE
65 > 0 FALSE
10 < 7 + 5 TRUE
Relational expressions are used in decision making statements of C language such as if, while and for statements to decide the course of action of a running program
Logical Operators
Logical operators in C, as with other computer languages, are used to evaluate expressions, which may be true or false. Expressions that involve logical operations are evaluated and found to be one of two values: true or false.
Logical Operators  
Operator  Meaning  Example of Use  Truth Value 
&&  AND  (exp 1) && (exp 2)  True if exp 1 and exp 2 are BOTH true. 
  OR  (exp 1)  (exp 2)  True if EITHER (or BOTH) exp 1 or exp 2 are true. 
!  NOT  ! (exp 1)  Returns the opposite truthvalue of exp 1; if exp 1 is true,! (exp 1) is false; if exp 1 is false,! (exp 1) is true. 
Logical AND (&&)
This operator is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true.
Example
a > b && x = = 10
the expression to the left is a > b and that on the right is
x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10.
Logical OR ()
The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true.
Example
a < m  a < n
The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m and n.
Logical NOT (!)
The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression.
Example
! (x >= y) the NOT expression evaluates to true only if the value of x is neither greater than or equal to y
Assignment Operators
The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.
Example
x = a + b
Here the value of a + b is evaluated and substituted to the variable x.
In addition C has a set of shorthand assignment operators of the form.
var oper = exp;
Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as shorthand assignment operator
Example
x + = 1 is same as x = x + 1
Shorthand assignment operators
Statement with simple assignment operator 
Statement with shorthand operator 
a = a + 1  a += 1 
a = a – 1  a = 1 
a = a * (n+1)  a *= (n+1) 
a = a / (n+1)  a /= (n+1) 
a = a % b  a %= b 
The advantages of using shorthand assignment operators:
 What appears on the left hand side need not be repeated and therefore it becomes easier to write.
 The statement is more concise and easier to read.
 The statement is more efficient.
Increment and Decrement Operators
The increment and decrement operators are one of the unary operators, which are very useful in C language. They are extensively used in for and while loops. The syntax of the operators is given below
 ++ Variable name
 variable name++
 Variable name
 Variable name —
The increment operator ++ adds the value 1 to the current value of operand and the decrement operator – – subtracts the value 1 from the current value of operand. ++Variable name and variable name++ mean the same thing when they form statements independently; they behave differently when they are used in expression on the right hand side of an assignment statement.
Consider the following
m = 5;
y = ++m; (prefix)
in this case the value of y and m would be 6
Suppose if we rewrite the above statement as
m = 5;
y = m++; (post fix)
Then the value of y will be 5 and that of m will be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left. On the other hand, a postfix operator first assigns the value to the variable on the left and then increments the operand.
you are reading LOGIC DEVELOPMENT article from c programming language. You can also check few more article on c programming language.
Conditional Operator
The conditional operator consists of 2 symbols the question mark (?) and the colon (:)
the syntax for a ternary operator is as follows.
exp1? exp2: exp3
the ternary operator works as follows
exp1 is evaluated first. If the expression is true then exp2 is evaluated & its value becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Note that only one of the expressions is evaluated.
For example
a = 10;
b = 15;
x = (a > b)? a: b
Here x will be assigned to the value of b. The condition follows that the expression is false therefore b is assigned to x.
 I/O Statements:
 Printf
Printf function is used to display data on the screen.
Data can be number, character, string, etc.
Syntax:
printf (“ control string “ , variable1, variable2, …);
Example :
printf(“Hello World”);
printf(“Value of sum is : %d”, sum);
1^{st} example prints Hello World on output screen.
2^{nd} example prints value of sum is : <value of sum variable>
 Scanf
Scanf function is used to get data from the user.
Data can be number, character, string, etc.
Syntax :
scanf (“ control string “ , &variable1, &variable2, …);
Example :
Printf(“Enter value of no1”);
scanf(“%d”, &no1);
 Assignment Statements :
Value can be assigned to variable using assignment operator ( = ).
Syntax :
<Variable name> = constant;
Example :
i = 5;
We can write multiple assignment statement in one line using semicolon( ; ).
e.g. i = 0; j = 5; k = 10;
hope you like my LOGIC DEVELOPMENT article on C programming language. Click on next link to download in pdf format.
Download Tutorial in PDF Format
Check C Programming Tutorials and Sample Code