if statements: simple if, if-else, if-else if-else, Nested if

if પોતાનો block ત્યારે જ ચલાવે છે જ્યારે condition nonzero હોય, else બીજો રસ્તો લે છે, અને ladder ફક્ત પોતાની પહેલી true rung ચલાવે છે.

5 min read · 12 cards · 5 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

જૂઠું બોલતો program

રાહુલને 20 marks આવ્યા, અને result program એ Pass print કરી દીધું:

if (marks >= 35);

printf("Pass");

આ compile થઈ જાય છે, છતાં એક character જૂઠું બોલે છે. કયો?

Think first

જૂઠાને પકડો

જે character પર શંકા હોય તે type કરો.

Show the answer

(marks >= 35) પછીનો semicolon. એ if ને એક ખાલી statement પર પૂરું કરી દે છે, એટલે printf if ની બહાર છે અને દરેક માટે ચાલે છે.

Theory

if, else, else if

if (condition) { } પોતાનો block ત્યારે જ ચલાવે છે જ્યારે condition true હોય, અને C માં true એટલે nonzero. else { } બીજો રસ્તો ઉમેરે છે, એટલે બેમાંથી બરાબર એક જ ચાલે છે. else if ની rungs જોડતા જાઓ તો એક ladder બને છે, જે ઉપરથી ચકાસાય છે.

Practical

Grade વાળી ladder

#include <stdio.h>

int main() {
    int marks = 65;

    if (marks >= 70) {
        printf("Grade A\n");
    } else if (marks >= 60) {
        printf("Grade B\n");
    } else if (marks >= 35) {
        printf("Grade C\n");
    } else {
        printf("Fail\n");
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

ઉપરની ladder માં marks 65 છે. શું print થાય છે?

  1. Grade A
  2. Grade B
  3. Grade B, પછી Grade C
  4. Grade C
Show the answer

Grade B

65 >= 60 પહેલી true rung છે, એટલે Grade B print થાય છે અને ladder અટકી જાય છે; 65 >= 35 ચકાસાતું જ નથી.

Think first

Ladder ઊંધી કરો

Rungs ઊંધી કરો: પહેલાં >= 35, છેલ્લે >= 70. 92 marks વાળા topper ને શું મળશે?

Show the answer

Grade C. 92 >= 35 પહેલી જ rung પર true છે, એટલે ladder ત્યાં જ અટકી જાય છે, ન કોઈ error, ન કોઈ warning. સૌથી કડક rung સૌથી પહેલાં મૂકો.

Theory

if ની અંદર if

Nested if બીજા if ના block ની અંદર હોય છે, એટલે એ બહારના if ની હા પછી જ ચાલે છે. Braces વગર else એવા સૌથી નજીકના if સાથે જોડાય છે જેનું પોતાનું else હજી નથી, એટલે braces મૂકો.

Practical

Nested if

#include <stdio.h>

int main() {
    int marks = 80;

    if (marks >= 35) {
        printf("Pass\n");
        if (marks >= 75) {
            printf("Distinction\n");
        }
    } else {
        printf("Fail\n");
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Trace કરો

ઉપરના program માં marks 80 છે. જે કંઈ print થાય તે બધું type કરો.

Show the answer

Pass, પછી Distinction. અંદરનું if બહારના block ની અંદરનું એક વધુ statement છે, એટલે એ એક line ઉમેરે છે; Pass ની જગ્યા નથી લેતું.

Practical

Exam-style: output કહો

#include <stdio.h>

int main() {
    int x = 0;

    if (x = 5)
        printf("Five");
    else
        printf("Zero");

    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

Exam-style: ઉપરના program નું output શું છે?

  1. Five
  2. Zero
  3. Compile error
  4. કંઈ print થતું નથી
Show the answer

Five

x = 5, x માં 5 assign કરે છે, અને એની value 5 nonzero છે, એટલે condition true છે. = ને == વાંચવાથી ખોટો જવાબ, Zero, આવે છે.

Summary

Key takeaways

  • if પોતાનો block ત્યારે જ ચલાવે જ્યારે condition nonzero હોય.
  • Ladder ફક્ત પોતાની પહેલી true rung ચલાવે છે, એટલે સૌથી કડક rung પહેલાં મૂકો.
  • Nested if ત્યારે જ ચાલે જ્યારે બહારનું if હા કહે; else એવા સૌથી નજીકના if સાથે જોડાય છે જેનું else હજી નથી.
  • યાદ રાખવાની યુક્તિ: if પછી semicolon નહીં, comparison માટે ==.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Decision Making and Iterative statements

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

if statements: simple if, if-else, if-else if-else, Nested if · Computer Programming and Programming Methodology (CPPM) · Gri-Learn