1. C Programming
Language
By:
Yogendra Pal
yogendra@learnbywatch.com
Dedicated to My mother and Father
2. t
y
Keep your notebook with you.
Write important point and questions that comes in your mind
Solve Mind band exercise.
C
Rewind when not clear
Ask Questions by call or SMS or by mail
Keep Watching Keep Learning
THIS IS DECISION CONTROL
2
3. Need
• Consider a problem:
– Write a program which take a number as a input
and print it if it is an even number.
– This problem can not solved by calculation or
arithmetic expression.
– We need to take decision here.
3
4. Decision statements
• To change the control flow of a program we
use:
– Branching statements : cause one section of code
to be executed or not executed, depending on a
conditional clause.
– Looping statements : used to repeat a section of
code a number of times or until some condition
occurs.
4
5. Branching Statements : 1
• if Statements
– General form:
if(condition)
statement;
• if Condition is true (non-zero), the statement
will be executed.
• if the condition is false (0), the statement will
not be executed.
5
6. An Example
• Write a program which take a number as a
input and print it if it is an even number.
• == is a relational operator that represents
“equal to”.
– Eg. if (number % 2 == 0)
printf(“It is an even number”);
• This statement reads
– “if the number%2 is equal to zero print ‘it is an even
number’ ”
6
7. Relational operator
Operator Meaning
<= Less than OR equal to
< Less than
>= Greater than or equal to
== Equal
!= Not equal
7
9. Multiple Statements
• Put all statements inside the curly { } braces.
if(condition)
{
statement 1;
statement 2;
statement 3;
}
9
10. Problems
• Input cost price and selling price of an item
through the keyboard and write a program to
determine the profit or loss.
• Write a program to determine whether the
year (input from keyboard) is a leap or not.
• Write a program that take a character as a
input and determine whether the character
entered is a capital letter or small letter or a
number.
10
11. Branching Statement : 2
• else statement:
• An alternate form of the if statement:-
if(condition) If condition is true
statement; execute this statement
else otherwise
statement; execute this statement
• Write a program which take a number as a
input and print it if it is an even number
otherwise print “you entered ODD number”.
11
13. Nested if - else
if(condition) if(condition){
statement; statement 1;
else if (condition) statement 2;
statement; }
else if (condition) else if (condition){
statement; statement 1;
else statement 2;
statement; }
else {
statement 1;
statement 2;
}
13
14. A problem
• Write a program which take an operator (+, -, *
or /) as an input and perform the operation
according to the input.
• Like:
– a = 10;
– b = 2;
– Input = ‘+’ output = 12;
– Input = ‘-’ output = 8;
14
15. Branching Statement : 3
• switch statement
– An alternative to the chain of if / else statements.
switch (expression)
• General form: {
case constant_1:
statement;
break;
Case case constant_2:
label statement;
default:
statement;
break;
}
15
16. Rules to use switch
• Duplicate labels are not allowed.
• Expression must evaluate an integer, character
or enumeration.
• Case labels can be in any order and must be
constant.
• Default label can be put anywhere in the
switch.
• No two case have the same value.
16
17. If-else / switch
• Break statement causes the program to go to
the end of the switch.
switch (operator)
if(operator == „+‟){
{
result += value;
case „+‟:
}else if (operator == „-‟){
result += value;
resultbreak;
-= value;
case „-‟:
}else { result -= value;
printf(“Unknown operator %c n”,operator);
break;
} default:
printf(“Unknown error %c n”, operator);
}
17
18. Conditional Operator
• Works as if-else statement.
• Condition ? Statement 1 : Statement 2;
• If condition is true statement 1 will execute
other wise statement 2.
– ( a <= 5) ? 0 : 100;
18
19. Operator Precedence
Operator Operation Precendence
() Parentheses Evaluate First,
Innermost first,
Left to right.
-, +, --, ++, sizeof, (type) Unary operators Right to left
*, / or % Multiplication, Division or Left to right.
modulus
+ or - Addition and subtraction Left to right.
<, <=, >, >= Relational operators Left to right.
== != Equality operators Left to right.
&& Logical and Left to right.
|| Logical or Left to right.
?: Conditional operator Right to left
= Assignment (Multiple) Right to left.
19
20. Mind Bend
• Write a program that accept a character from
keyboard and convert as follows:
– If it is uppercase then convert in lowercase letter.
– If it is lowercase then convert in uppercase letter.
– If it is other then alphabet print error message.
• Write a program which take two sides of a
rectangle and determine whether the given
sides are of a rectangle or a square.
20
21. To get complete benefit of this tutorial solve all the quiz on
www.learnbywatch.com
For any problem in this tutorial mail me at
yogendra@learnbywatch.com
with the subject “C”
For Other information mail at
info@learnbywatch.com
Keep Watching Keep Learning
NEXT IS LOOPS
21