பாடம் 1 / 6 · Control Flow · படிக்க 2 min · புதுப்பிக்கப்பட்டது

if வெச்சு முடிவு எடுக்கிறது

ஒரு condition சரியா இருந்தா மட்டும் அந்த code-ஐ ஓட வைக்கிறது. இதுல ஒரு சின்ன typo வந்தாலும், program-ஐ கெடுத்துடும்.

இதுவரைக்கும் நம்ம எழுதின ஒவ்வொரு program-மும் மேல இருந்து கீழ வரைக்கும், ஒவ்வொரு line-ஆ, ஒவ்வொரு தடவையும் ஓடிக்கிட்டே இருந்துச்சு. if வந்ததுக்கப்புறம் தான், condition-ஐ பார்த்து program எந்த code-ஐ ஓட்டணும்னு choose பண்ண முடியும்.

c
#include <stdio.h>

int main(void)
{
    int marks;

    printf("Marks out of 100: ");
    scanf("%d", &marks);

    if (marks >= 90) {
        printf("Distinction\n");
    } else if (marks >= 50) {
        printf("Pass\n");
    } else {
        printf("Try again\n");
    }

    return 0;
}
Output
Marks out of 100: 72
Pass

Comparison operators

c
a == b   equal to
a != b   not equal to
a <  b   less than
a <= b   less than or equal to
a >  b   greater than
a >= b   greater than or equal to

Warning

=-ன்னா assign பண்ணுறது, ==-ன்னா compare பண்ணுறது. if (x = 5)-ன்னு எழுதிட்டீங்கன்னா, x-க்கு 5 assign ஆகிடும்; அதனால condition எப்பவும் true-ஆவே இருக்கும். இது error வராமலேயே compile ஆகிடுற ஒரு bug — இதனால நிறைய பேரோட முழு மதியமே போயிருக்கு. நிறைய compilers இதுக்கு warning கொடுக்கும்; compiler warnings-ஐ படிக்கணும்னு சொல்லுறதுக்கு இதுவும் இன்னொரு நல்ல காரணம்.

Conditions-ஐ சேர்க்கிறது

c
if (age >= 18 && has_id) {
    printf("Allowed\n");
}

if (day == 0 || day == 6) {
    printf("Weekend\n");
}

if (!found) {
    printf("Not found\n");
}
  • && — and. ரெண்டு பக்கமும் true-ஆ இருக்கணும்.
  • || — or. ஏதாவது ஒரு பக்கம் true-ஆ இருந்தாலே போதும்.
  • ! — not. true-வை false-ஆவும், false-வை true-ஆவும் மாத்தும்.

Note

பழைய C standards-ல தனியா true / false type கிடையாது. 0-ன்னா false; 0 தவிர வேற எந்த value-ஆ இருந்தாலும் true. அதனால if (count)-ன்னு எழுதினா, அதோட அர்த்தம் "count zero இல்லனா".

if (x = 5) என்ன செய்யுது?

Create a free account to save your progress, or sign in.