SlideShare uma empresa Scribd logo
1 de 5
UNIVERSITI TUN HUSSEIN ONN MALAYSIA
           FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING
           BTI 10202: COMPUTER PROGRAMMING


 LAB 5 : Control Statements- Part 1 (Selection)
 NAME : ____ _______________________________________                                         MARKS
 MATRICS NO.: _______________ DATE : _____________


Objective: Introduction to various concepts of selection control structure
           (Be able to choose and use the if, if..else, switch and goto selection
           structures among alternatives action)

Theory:
1.        if single selection structure             Syntax:
     If the condition is TRUE, a set of             if (conditional is true)
     statements are executed.                       {
     If the condition is FALSE, the statement          Statement(s);
     are not executed and the program control       }
     goes to the next statement (after the if          next statement
     block) immediately.
                                                    Statement executes repeatedly as long as the value of
                                                    condition remains TRUE.

2. if double selection structure (if…else)          Syntax:
  If the condition is TRUE, statement A             if (condition is true)
  executes. When FALSE, statement                   {
     executes.                                         statement A;
                                                    }
                                                       else
                                                    {
                                                       statement B;
                                                    }
                                                    Next statement;

3.         if multiple selection structure (if…     Syntax:
     else if)                                       If (condition A)
     The ‘else if’ statement is to check for a      {
     sequence of conditions.                           Statement A;
     If one condition is false, it checks for the   }
     next condition and so on. When all the         else if (condition B)
     conditions are false, the ‘else’ block is      {
     executed.                                         Statement B;
                                                    }
                                                    :
                                                    :
                                                    :else if (condition n)
                                                    {
                                                       Statement set-n;
                                                    }
                                                    else
                                                    {
                                                       Statement set-x;
                                                    }
a. Relational operators                     b. Equality operators


     Symbol     Meaning                         Symbol          Meaning

       >        greater than                       ==           equal to

      >=        greater than or equal to           !=           not equal to


       <        less than

      <=        less than or equal to



                                            Note: If the selection structures have a few statements, then
c. Logical operators                        we need to use the curly bracket { }.
                                            Example:
     Symbol     Meaning                         If (marks>=60)
                                                       printf(“Grade A”);
      &&        AND (Returns true if            else if (marks<60&&marks>=30)
                      both conditions                  printf(“Grade B”);
                      are true)                 else (marks<30)
                                                       {
       ||       OR (Returns true if                    printf(“Failed”);
                       either of its                   printf(“Take a test again!”); }
                       conditions is
                       true

       !        NOT (Reverses the
                      truth/falsity of
                      its condition)



4.    switch is useful when variable or      Syntax:
      condition is tested for multiple
                                             switch (expression) {
      values. It consists of a series of
      case labels and an optional default      case expression 1:
      case.                                           statements1;
      The switch and case statements
                                                             break;
      help control complex conditional
      and branching operations.                  case expression 2:
                                                      statements 2;
                                                             break;
                                                 default:
                                                     expression n;
                                                            break;
                                             }
Note: break is used to terminate loops/ exit from a switch.
                                               Can be used within a for,while, do-while or switch
                                               statement.

5.       goto is a unconditional                         Syntax:
   branching statement used to transfer                       goto name_label;
   control of the program from one                            :
   statement to another. Note: please                         :
   ensure not to use too much goto                        name_label:
   statements in the program because
   its functionality is limited. It is only
   recommended as a last resort if
   structured solutions are much more
   complicated.
Flowchart for selection structures             Flowchart for switch control statements
                                               (Adapted from Deitel & Deitel, C How to Program, 6th
                                               ed., p. 111)




Example:
           start:
           printf(“1. Apple juicen”);
           printf(“2. Mango juicen”);
           printf(“Choose the fruit juice that you like?”);
           scanf(“%d”,&juice);
           switch(juice)
           {
                  case 1: printf(“You like to drink apple juice”);break;
                  case 2: printf(“You like to drink mango juice”);break;
                  default: printf(“None of the fruit juice that you like!”);goto start; break;
                  }

Exercises:

1.   What is the output for the following source code:
                                                   Output:
     #include <stdio.h>
     #include <conio.h>

     main() {
     int n = 0;
     loop:
printf("n%d", n);
          n++;
          if (n<10)
         { goto loop; }
         getch();

     }


2.       Below is a program to print RED when ‘r’or’R’ is pressed and WHITE when ‘w’ or ‘W’ is
         pressed using a switch control statement. The program loops back to enter ‘r’or’R’ or ‘w’ or ‘W’
         if other keys are pressed. Rewrite the coding using if-else control statements.
          SWITCH program
          #include<stdio.h>
          #include<conio.h>
          main()
          {
          char choice;
          choose:
          printf("nPlease enter 'R' for red and 'W' for whiten");
          scanf("%s",&choice);
          switch(choice)
          { case 'r':
          case 'R':printf(" RED "); break;
          case 'w':
          case 'W':printf(" WHITE"); break;
          IF…ELSE program
          default: printf("Error!!"); goto choose; break;
          }
          getch();
          }
Lab 5 2012/2012

Mais conteúdo relacionado

Mais procurados

Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
C language control statements
C language  control statementsC language  control statements
C language control statementssuman Aggarwal
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statementFarshidKhan
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statementRaj Parekh
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and VariablesSyed Afaq Shah MACS CP
 
Presentation of control statement
Presentation of control statement  Presentation of control statement
Presentation of control statement Bharat Rathore
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++amber chaudary
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional StatementDeepak Singh
 
C statements
C statementsC statements
C statementsAhsann111
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrolsteach4uin
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programmingPriyansh Thakar
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 

Mais procurados (20)

Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
Chap 5(decision making-branching)
Chap 5(decision making-branching)Chap 5(decision making-branching)
Chap 5(decision making-branching)
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Presentation of control statement
Presentation of control statement  Presentation of control statement
Presentation of control statement
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Branching in C
Branching in CBranching in C
Branching in C
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
C statements
C statementsC statements
C statements
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 

Destaque

Slide Share
Slide ShareSlide Share
Slide Shareahrong
 
Books to Read
Books to ReadBooks to Read
Books to Readnplisko
 
การนำเสนอ..
การนำเสนอ..การนำเสนอ..
การนำเสนอ..NattAA
 
Ways of looking 2
Ways of looking 2Ways of looking 2
Ways of looking 2amandakane1
 
โครงงานครั้งที่ 2 (Guava Cake)
โครงงานครั้งที่ 2 (Guava Cake)โครงงานครั้งที่ 2 (Guava Cake)
โครงงานครั้งที่ 2 (Guava Cake)NattAA
 
فن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميدفن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميدSunflower Al-mahrouqi
 
Dti2143 lab sheet 7
Dti2143 lab sheet 7Dti2143 lab sheet 7
Dti2143 lab sheet 7alish sha
 
หมั่นโถวเสริมโปรตีนจากใบมะรุม.
หมั่นโถวเสริมโปรตีนจากใบมะรุม.หมั่นโถวเสริมโปรตีนจากใบมะรุม.
หมั่นโถวเสริมโปรตีนจากใบมะรุม.NattAA
 
فن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميدفن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميدSunflower Al-mahrouqi
 
MLP輪読スパース8章 トレースノルム正則化
MLP輪読スパース8章 トレースノルム正則化MLP輪読スパース8章 トレースノルム正則化
MLP輪読スパース8章 トレースノルム正則化Akira Tanimoto
 
Rèdais & IED_Cugusi
Rèdais & IED_CugusiRèdais & IED_Cugusi
Rèdais & IED_CugusiRèdais
 
Profileer jezelf met social media
Profileer jezelf met social mediaProfileer jezelf met social media
Profileer jezelf met social mediaTom van den Berg
 

Destaque (20)

Slide Share
Slide ShareSlide Share
Slide Share
 
WLC AAR
WLC AARWLC AAR
WLC AAR
 
Books to Read
Books to ReadBooks to Read
Books to Read
 
Presentation2
Presentation2Presentation2
Presentation2
 
Advert
AdvertAdvert
Advert
 
Ideas tema 1
Ideas tema 1Ideas tema 1
Ideas tema 1
 
การนำเสนอ..
การนำเสนอ..การนำเสนอ..
การนำเสนอ..
 
Ways of looking 2
Ways of looking 2Ways of looking 2
Ways of looking 2
 
โครงงานครั้งที่ 2 (Guava Cake)
โครงงานครั้งที่ 2 (Guava Cake)โครงงานครั้งที่ 2 (Guava Cake)
โครงงานครั้งที่ 2 (Guava Cake)
 
فن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميدفن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميد
 
Takethetime
TakethetimeTakethetime
Takethetime
 
Dti2143 lab sheet 7
Dti2143 lab sheet 7Dti2143 lab sheet 7
Dti2143 lab sheet 7
 
Canihas
CanihasCanihas
Canihas
 
หมั่นโถวเสริมโปรตีนจากใบมะรุม.
หมั่นโถวเสริมโปรตีนจากใบมะรุม.หมั่นโถวเสริมโปรตีนจากใบมะรุม.
หมั่นโถวเสริมโปรตีนจากใบมะรุม.
 
فن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميدفن استشر والترصيع بالقرميد
فن استشر والترصيع بالقرميد
 
MLP輪読スパース8章 トレースノルム正則化
MLP輪読スパース8章 トレースノルム正則化MLP輪読スパース8章 トレースノルム正則化
MLP輪読スパース8章 トレースノルム正則化
 
Rèdais & IED_Cugusi
Rèdais & IED_CugusiRèdais & IED_Cugusi
Rèdais & IED_Cugusi
 
Mahara
MaharaMahara
Mahara
 
Profileer jezelf met social media
Profileer jezelf met social mediaProfileer jezelf met social media
Profileer jezelf met social media
 
حصص المشاهدة
حصص المشاهدةحصص المشاهدة
حصص المشاهدة
 

Semelhante a Lab 5 2012/2012

Selection statements
Selection statementsSelection statements
Selection statementsHarsh Dabas
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c languagechintupro9
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.pptManojKhadilkar1
 
operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c languageshhanks
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in CRAJ KUMAR
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdfArkSingh7
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguageTanmay Modi
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsLovelitJose
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition StructureShahzu2
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in javaAtul Sehdev
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 

Semelhante a Lab 5 2012/2012 (20)

CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
Selection statements
Selection statementsSelection statements
Selection statements
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c language
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
 
Decision Making.pptx
Decision Making.pptxDecision Making.pptx
Decision Making.pptx
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 

Mais de alish sha

T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9alish sha
 
July 2014 theory exam (theory)
July 2014 theory exam (theory)July 2014 theory exam (theory)
July 2014 theory exam (theory)alish sha
 
Accounting basic equation
Accounting basic equation Accounting basic equation
Accounting basic equation alish sha
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifahalish sha
 
It 302 computerized accounting (week 1) - sharifah
It 302   computerized accounting (week 1) - sharifahIt 302   computerized accounting (week 1) - sharifah
It 302 computerized accounting (week 1) - sharifahalish sha
 
What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)alish sha
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13alish sha
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13alish sha
 
Purpose elaborate
Purpose elaboratePurpose elaborate
Purpose elaboratealish sha
 
Test 1 alish schema 1
Test 1 alish schema 1Test 1 alish schema 1
Test 1 alish schema 1alish sha
 
Lab 6 sem ii_11_12
Lab 6 sem ii_11_12Lab 6 sem ii_11_12
Lab 6 sem ii_11_12alish sha
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&aalish sha
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&aalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Attn list test
Attn list testAttn list test
Attn list testalish sha
 
Carry markdam31303
Carry markdam31303Carry markdam31303
Carry markdam31303alish sha
 
Final project
Final projectFinal project
Final projectalish sha
 

Mais de alish sha (20)

T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9
 
July 2014 theory exam (theory)
July 2014 theory exam (theory)July 2014 theory exam (theory)
July 2014 theory exam (theory)
 
Accounting basic equation
Accounting basic equation Accounting basic equation
Accounting basic equation
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifah
 
It 302 computerized accounting (week 1) - sharifah
It 302   computerized accounting (week 1) - sharifahIt 302   computerized accounting (week 1) - sharifah
It 302 computerized accounting (week 1) - sharifah
 
What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13
 
Lab 6
Lab 6Lab 6
Lab 6
 
Purpose elaborate
Purpose elaboratePurpose elaborate
Purpose elaborate
 
Lab sheet 1
Lab sheet 1Lab sheet 1
Lab sheet 1
 
Test 1 alish schema 1
Test 1 alish schema 1Test 1 alish schema 1
Test 1 alish schema 1
 
Lab 6 sem ii_11_12
Lab 6 sem ii_11_12Lab 6 sem ii_11_12
Lab 6 sem ii_11_12
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&a
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&a
 
Final project
Final projectFinal project
Final project
 
Final project
Final projectFinal project
Final project
 
Attn list test
Attn list testAttn list test
Attn list test
 
Carry markdam31303
Carry markdam31303Carry markdam31303
Carry markdam31303
 
Final project
Final projectFinal project
Final project
 

Lab 5 2012/2012

  • 1. UNIVERSITI TUN HUSSEIN ONN MALAYSIA FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 10202: COMPUTER PROGRAMMING LAB 5 : Control Statements- Part 1 (Selection) NAME : ____ _______________________________________ MARKS MATRICS NO.: _______________ DATE : _____________ Objective: Introduction to various concepts of selection control structure (Be able to choose and use the if, if..else, switch and goto selection structures among alternatives action) Theory: 1. if single selection structure Syntax: If the condition is TRUE, a set of if (conditional is true) statements are executed. { If the condition is FALSE, the statement Statement(s); are not executed and the program control } goes to the next statement (after the if next statement block) immediately. Statement executes repeatedly as long as the value of condition remains TRUE. 2. if double selection structure (if…else) Syntax: If the condition is TRUE, statement A if (condition is true) executes. When FALSE, statement { executes. statement A; } else { statement B; } Next statement; 3. if multiple selection structure (if… Syntax: else if) If (condition A) The ‘else if’ statement is to check for a { sequence of conditions. Statement A; If one condition is false, it checks for the } next condition and so on. When all the else if (condition B) conditions are false, the ‘else’ block is { executed. Statement B; } : : :else if (condition n) { Statement set-n; } else { Statement set-x; }
  • 2. a. Relational operators b. Equality operators Symbol Meaning Symbol Meaning > greater than == equal to >= greater than or equal to != not equal to < less than <= less than or equal to Note: If the selection structures have a few statements, then c. Logical operators we need to use the curly bracket { }. Example: Symbol Meaning If (marks>=60) printf(“Grade A”); && AND (Returns true if else if (marks<60&&marks>=30) both conditions printf(“Grade B”); are true) else (marks<30) { || OR (Returns true if printf(“Failed”); either of its printf(“Take a test again!”); } conditions is true ! NOT (Reverses the truth/falsity of its condition) 4. switch is useful when variable or Syntax: condition is tested for multiple switch (expression) { values. It consists of a series of case labels and an optional default case expression 1: case. statements1; The switch and case statements break; help control complex conditional and branching operations. case expression 2: statements 2; break; default: expression n; break; }
  • 3. Note: break is used to terminate loops/ exit from a switch. Can be used within a for,while, do-while or switch statement. 5. goto is a unconditional Syntax: branching statement used to transfer goto name_label; control of the program from one : statement to another. Note: please : ensure not to use too much goto name_label: statements in the program because its functionality is limited. It is only recommended as a last resort if structured solutions are much more complicated. Flowchart for selection structures Flowchart for switch control statements (Adapted from Deitel & Deitel, C How to Program, 6th ed., p. 111) Example: start: printf(“1. Apple juicen”); printf(“2. Mango juicen”); printf(“Choose the fruit juice that you like?”); scanf(“%d”,&juice); switch(juice) { case 1: printf(“You like to drink apple juice”);break; case 2: printf(“You like to drink mango juice”);break; default: printf(“None of the fruit juice that you like!”);goto start; break; } Exercises: 1. What is the output for the following source code: Output: #include <stdio.h> #include <conio.h> main() { int n = 0; loop:
  • 4. printf("n%d", n); n++; if (n<10) { goto loop; } getch(); } 2. Below is a program to print RED when ‘r’or’R’ is pressed and WHITE when ‘w’ or ‘W’ is pressed using a switch control statement. The program loops back to enter ‘r’or’R’ or ‘w’ or ‘W’ if other keys are pressed. Rewrite the coding using if-else control statements. SWITCH program #include<stdio.h> #include<conio.h> main() { char choice; choose: printf("nPlease enter 'R' for red and 'W' for whiten"); scanf("%s",&choice); switch(choice) { case 'r': case 'R':printf(" RED "); break; case 'w': case 'W':printf(" WHITE"); break; IF…ELSE program default: printf("Error!!"); goto choose; break; } getch(); }