SlideShare a Scribd company logo
1 of 21
Download to read offline
C             Programming
                       Language
               By:
    Yogendra Pal
yogendra@learnbywatch.com
  Dedicated to My mother and Father
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
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
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
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
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
Relational operator
Operator    Meaning
<=          Less than OR equal to
<           Less than
>=          Greater than or equal to
==          Equal
!=          Not equal


                       7
Logical Operator
Operator         Meaning
||               or
&&               and




                   8
Multiple Statements
• Put all statements inside the curly { } braces.
               if(condition)
               {
                    statement 1;
                    statement 2;
                    statement 3;
               }



                          9
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
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
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
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
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
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
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
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
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
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
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
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

More Related Content

What's hot (20)

Control structure in c
Control structure in cControl structure in c
Control structure in c
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Control structure
Control structureControl structure
Control structure
 
Lecture03(c expressions & operators)
Lecture03(c expressions & operators)Lecture03(c expressions & operators)
Lecture03(c expressions & operators)
 
Aae oop xp_04
Aae oop xp_04Aae oop xp_04
Aae oop xp_04
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Input output
Input outputInput output
Input output
 
C statements
C statementsC statements
C statements
 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Na50 enus devi_14
Na50 enus devi_14Na50 enus devi_14
Na50 enus devi_14
 

Viewers also liked (8)

Sol7
Sol7Sol7
Sol7
 
Sol5
Sol5Sol5
Sol5
 
Conditional structure problems
Conditional structure problemsConditional structure problems
Conditional structure problems
 
Sol8
Sol8Sol8
Sol8
 
Sol9
Sol9Sol9
Sol9
 
Control structures(class 02)
Control structures(class 02)Control structures(class 02)
Control structures(class 02)
 
Sol 1
Sol 1Sol 1
Sol 1
 
Assignment # 2
Assignment # 2Assignment # 2
Assignment # 2
 

Similar to C Programming Language Decision Control

Programming in Java: Control Flow
Programming in Java: Control FlowProgramming in Java: Control Flow
Programming in Java: Control FlowMartin Chapman
 
De so 1
De so 1De so 1
De so 1kiennt
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.comGreen Ecosystem
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection StatementsSzeChingChen
 
Fundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptxFundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptxEyasu46
 
10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptxAqeelAbbas94
 
CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selectionHamad Odhabi
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language CourseVivek chan
 
Arithmetic instructions
Arithmetic instructionsArithmetic instructions
Arithmetic instructionsLearn By Watch
 
IF Else logic in c#
IF Else logic in c#IF Else logic in c#
IF Else logic in c#Ehsan Ehrari
 
java basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arraysjava basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arraysmellosuji
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 

Similar to C Programming Language Decision Control (20)

Loops
LoopsLoops
Loops
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Programming in Java: Control Flow
Programming in Java: Control FlowProgramming in Java: Control Flow
Programming in Java: Control Flow
 
De so 1
De so 1De so 1
De so 1
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Fundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptxFundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptx
 
10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx
 
C++ Tokens
C++ TokensC++ Tokens
C++ Tokens
 
Week2 dq4
Week2 dq4Week2 dq4
Week2 dq4
 
CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selection
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
How to Program
How to ProgramHow to Program
How to Program
 
Arithmetic instructions
Arithmetic instructionsArithmetic instructions
Arithmetic instructions
 
IF Else logic in c#
IF Else logic in c#IF Else logic in c#
IF Else logic in c#
 
java basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arraysjava basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arrays
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 

More from Learn By Watch

Demodulation of fm pll detector
Demodulation of fm pll detectorDemodulation of fm pll detector
Demodulation of fm pll detectorLearn By Watch
 
Demodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorDemodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorLearn By Watch
 
In direct method of fm generation armstrong method
In direct method of fm generation armstrong methodIn direct method of fm generation armstrong method
In direct method of fm generation armstrong methodLearn By Watch
 
Direct method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodDirect method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodLearn By Watch
 
Spectrum and power of wbfm
Spectrum and power of wbfmSpectrum and power of wbfm
Spectrum and power of wbfmLearn By Watch
 
Narrow band frequency modulation nbfm
Narrow band frequency modulation nbfmNarrow band frequency modulation nbfm
Narrow band frequency modulation nbfmLearn By Watch
 
General expression of fm signal
General expression of fm signalGeneral expression of fm signal
General expression of fm signalLearn By Watch
 
Frequency division multiplexing
Frequency division multiplexingFrequency division multiplexing
Frequency division multiplexingLearn By Watch
 
Demodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorDemodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorLearn By Watch
 
Generarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodGenerarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodLearn By Watch
 
Generarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodGenerarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodLearn By Watch
 
Demodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverDemodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverLearn By Watch
 
Quadrature carrier multiplexing qam
Quadrature carrier multiplexing qamQuadrature carrier multiplexing qam
Quadrature carrier multiplexing qamLearn By Watch
 
Demodulation of am synchronous detector
Demodulation of am synchronous detectorDemodulation of am synchronous detector
Demodulation of am synchronous detectorLearn By Watch
 

More from Learn By Watch (20)

Tutorial 9 fm
Tutorial 9 fmTutorial 9 fm
Tutorial 9 fm
 
Phase modulation
Phase modulationPhase modulation
Phase modulation
 
Demodulation of fm pll detector
Demodulation of fm pll detectorDemodulation of fm pll detector
Demodulation of fm pll detector
 
Demodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorDemodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detector
 
In direct method of fm generation armstrong method
In direct method of fm generation armstrong methodIn direct method of fm generation armstrong method
In direct method of fm generation armstrong method
 
Direct method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodDirect method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator method
 
Carson's rule
Carson's ruleCarson's rule
Carson's rule
 
Spectrum and power of wbfm
Spectrum and power of wbfmSpectrum and power of wbfm
Spectrum and power of wbfm
 
Narrow band frequency modulation nbfm
Narrow band frequency modulation nbfmNarrow band frequency modulation nbfm
Narrow band frequency modulation nbfm
 
General expression of fm signal
General expression of fm signalGeneral expression of fm signal
General expression of fm signal
 
Angle modulation
Angle modulationAngle modulation
Angle modulation
 
Frequency division multiplexing
Frequency division multiplexingFrequency division multiplexing
Frequency division multiplexing
 
Vsb modulation
Vsb modulationVsb modulation
Vsb modulation
 
Demodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorDemodulation of ssb synchronous detector
Demodulation of ssb synchronous detector
 
Generarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodGenerarion of ssb phase discrimination method
Generarion of ssb phase discrimination method
 
Generarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodGenerarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination method
 
Ssb modulation
Ssb modulationSsb modulation
Ssb modulation
 
Demodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverDemodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiver
 
Quadrature carrier multiplexing qam
Quadrature carrier multiplexing qamQuadrature carrier multiplexing qam
Quadrature carrier multiplexing qam
 
Demodulation of am synchronous detector
Demodulation of am synchronous detectorDemodulation of am synchronous detector
Demodulation of am synchronous detector
 

C Programming Language Decision Control

  • 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
  • 8. Logical Operator Operator Meaning || or && and 8
  • 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
  • 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. 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