Friday 22 November 2013

if Decision Control Structure in C

Decision Control Structures मध्ये एकुण ४ प्रकारची control structures आहेत. अर्थात control म्हणजे काय हे माहीत हवे. Program execute होतांना प्रत्येक स्टेटमेंट हे compiler कडुन compile केले जाते. मात्र execute करण्यासाठी OS कडुन व CPU कडुन मदत घ्यावी लागते. पण हि सर्व स्टेटमेंट कोणत्या order ने execute होतात हे महत्वाचे. 

जर program मध्ये लिहीलेली सर्व च्या सर्व स्टेटमेंट विनासायास एका मागोमाग execute होत असतील तर त्याला Sequential Control Structure म्हणतात. 

पण जर एखादे स्टेटमेंट किंवा काही स्टेटमेंट्स execute करायची कि नाही हे जर काही conditions satisfy झाल्या तरच ठरवायचे असेल तर लिहीलेल्या स्टेटमेंट्स ना अथवा ब्लॉक ला Decision Control Structure म्हणतात. 

C language मध्ये decision घेण्यासाठी ३ पद्धती आहेत. 


  • पहीली म्हणजे if control structure
  • दुसरी म्हणजे if else control structure
  • तिसरी म्हणजे case control structure


if control structure

syntax:

if(condition)
{
do this;
do this; 
}

या control structure मध्ये if हा keyword आहे. या keyword नंतर लगेच pair of round brackets मध्ये condition लिहीली जाते. 


condition लिहीण्यासाठी मात्र C language मधील 6 relational operators वापरता येतात ते म्हणजे 

  1. less than (<) 
  2. Less than or equal to (<=)
  3. Greater than (>)
  4. Greater than or equal to (>=)
  5. Equal to (==)  आणी 
  6. Not equal to (!=)


वरुन operators वरून हे कळतेच की हे सर्व relational operators Binary Operators आहेत. प्रोग्रॅम लिहीतांना assignment operator (=) आणी equal to operator (==) मध्ये गोंधळ होण्याचीई शक्यता असते. या ठिकाणी equal to operator हा relational operator आहे हे लक्षात घेतले पाहीजे. 

वरील syntax मध्ये हे लक्षात घेतले पाहीजे की condition हि दोन operands मध्ये टेस्ट केली जाते. व या expression चा result एकतर True (non-zero म्हणजेच 1) किंवा False (Zero म्हणजेच 0) असतो. 

ज्यावेळी result True असेल त्याच वेळी if condition शी associated statements execute केली जातात. 
जर result False असेल तर if block किंवा if condition शी associated statements execute केली जाणार नाहीत. 

interview किंवा viva मध्ये तुम्हाला 

if(x)

असे स्टेटमेंट दिसून येइल. या ठिकाणी relational operator वापरलेला नाही. पण वर सांगीतल्या प्रमाणे round brackets च्या आत असलेला x हे expression म्हणून सोडवले जाते. आता x मध्ये non-zero value असली तर ते true असेल व x मध्ये 0 असेल तर expression false होइल. 

Click here if (condition) च्या व्हिडीओ साठी...! 


No comments:

Post a Comment

Do you like the C Marathi e-learning concept?