C             Programming
                       Language
               By:
    Yogendra Pal
yogendra@learnbywatch.com
  ...
t
                                                               y
         Keep your notebook with you.

Write important ...
Need
• Consider a problem:
  – Write a program which take a number as a input
    and print it if it is an even number.
  ...
Decision statements
• To change the control flow of a program we
  use:
  – Branching statements : cause one section of co...
Branching Statements : 1
• if Statements
  – General form:
        if(condition)
             statement;
• if Condition is...
An Example
• Write a program which take a number as a
  input and print it if it is an even number.
• == is a relational o...
Relational operator
Operator    Meaning
<=          Less than OR equal to
<           Less than
>=          Greater than o...
Logical Operator
Operator         Meaning
||               or
&&               and




                   8
Multiple Statements
• Put all statements inside the curly { } braces.
               if(condition)
               {
      ...
Problems
• Input cost price and selling price of an item
  through the keyboard and write a program to
  determine the pro...
Branching Statement : 2
• else statement:
• An alternate form of the if statement:-
if(condition)    If condition is true
...
Nested if - else
if(condition)                 if(condition)
      if(condition)           {
            statement;       ...
Nested if - else
if(condition)         if(condition){
      statement;             statement 1;
else if (condition)       ...
A problem
• Write a program which take an operator (+, -, *
  or /) as an input and perform the operation
  according to t...
Branching Statement : 3
• switch statement
  – An alternative to the chain of if / else statements.
                   swi...
Rules to use switch
• Duplicate labels are not allowed.
• Expression must evaluate an integer, character
  or enumeration....
If-else / switch
  • Break statement causes the program to go to
    the end of the switch.
switch (operator)
if(operator ...
Conditional Operator
• Works as if-else statement.
• Condition ? Statement 1 : Statement 2;
• If condition is true stateme...
Operator Precedence
Operator                       Operation                     Precendence
()                           ...
Mind Bend
• Write a program that accept a character from
  keyboard and convert as follows:
  – If it is uppercase then co...
To get complete benefit of this tutorial solve all the quiz on
                       www.learnbywatch.com

              ...
Upcoming SlideShare
Loading in …5
×

Decision control

2,126 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,126
On SlideShare
0
From Embeds
0
Number of Embeds
892
Actions
Shares
0
Downloads
55
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Decision control

  1. 1. C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
  2. 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. 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. 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. 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. 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. 7. Relational operator Operator Meaning <= Less than OR equal to < Less than >= Greater than or equal to == Equal != Not equal 7
  8. 8. Logical Operator Operator Meaning || or && and 8
  9. 9. Multiple Statements • Put all statements inside the curly { } braces. if(condition) { statement 1; statement 2; statement 3; } 9
  10. 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. 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
  12. 12. Nested if - else if(condition) if(condition) if(condition) { statement; if(condition) else statement; statement; } if(condition) else { statement; if(condition) statement; else statement; } 12
  13. 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. 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. 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. 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. 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. 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. 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. 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. 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

×