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

if code ત્યારે જ ચલાવે છે જ્યારે condition સાચી હોય, if-else 2 રસ્તા વચ્ચે પસંદ કરે, else-if ladder ઘણામાંથી એક પસંદ કરે (grades માટે perfect), અને nested if એક સવાલની અંદર સવાલ પૂછે.

11 min read · 10 cards · 3 checks

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


Theory

Program નો પહેલો નિર્ણય

અત્યાર સુધી marks program 92 અને 12 ને સરખાં ગણે છે: compute કરો, print કરો, બસ. કોઈ judgement નહીં.

પણ ખરી marksheet કંઈક કહે છે: pass કે fail, અને કયો grade.

આજે program પોતાનો રસ્તો 2-ફાંટા બનાવે છે: if આ, તો એ કરો, else બીજું કરો. Decision-making એ જ જગ્યા છે જ્યાં programs calculator હોવાનું છોડીને software બનવા લાગે છે, અને exams માં Unit 2 સામાન્ય રીતે બરાબર આ જ પરિવારથી ખૂલે છે.

Theory

Toll booth ની હાર

હારમાં ગોઠવેલા toll booths વિચારો, દરેક પાસે એક સવાલ. Simple if: એક booth, એને સંતોષો તો gate ખૂલે, નહીં તો તમે બસ આગળ નીકળી જાઓ. if-else: એક ફાંટો, એક સવાલ, 2 રસ્તા, એક તો લેવો જ પડે. else-if ladder: હારમાં booths, જે PEHLA ને સંતોષ્યો એ તમને બહાર વાળી દે, પછીના booths તમને જુએ પણ નહીં. Nested if: toll plaza ની VIP lane ની અંદર એક booth, એક સવાલ જે પહેલો પાર કર્યા પછી જ સામે આવે.

Theory

ચારેય રૂપ, ચોખ્ખાં

  • Simple if: if (marks >= 35) { printf("Pass"); } block ત્યારે જ ચલાવે જ્યારે condition સાચી (non-zero) હોય.
  • if-else: બીજો રસ્તો ઉમેરે: else { printf("Fail"); }
  • if-else if-else ladder: ઘણામાંથી એક રસ્તો, ઉપરથી નીચે ચકાસાય, પહેલો સાચો જીતે, બાકીના છોડી દેવાય.
  • Nested if: કોઈ if ના block ની અંદર એક if: પહેલા pass ચકાસો, અને એની જ અંદર distinction ચકાસો.

Braces { } દરેક રસ્તાના block ને ઘેરે છે. એક statement માટે પણ વાપરો; ભવિષ્યના તમે આભારી થશો.

Practical

Grade વાળી ladder

#include <stdio.h>

int main() {
    int marks;
    printf("Enter marks: ");
    scanf("%d", &marks);

    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.

Think first

Ladder પર ચાલો

ઉપરના code માં marks = 65 ને booth-દર-booth trace કરો. કઈ conditions ચકાસાય છે, કઈ જીતે છે, અને શું print થાય છે?

Show the answer

65 >= 70? ના, આગળ વધો. 65 >= 60? હા: Grade B print, અને ladder બહાર નીકળી જાય: 65 >= 35 ક્યારેય ચકાસાતું જ નથી. પહેલો સાચો જીતે, બાકીના છૂટી જાય.

એટલે જ ladder સૌથી ઊંચો grade પહેલા ચકાસે છે: ક્રમ ઉલટાવો (35 પહેલા) અને દરેક pass થતો student Grade C બની જાય, એક logic bug જેની compiler ને કોઈ ફરિયાદ નથી.

Quiz

marks = 20 માટે આ શું print કરે છે? if (marks >= 35); { printf("Pass\n"); }

  1. Pass, semicolon એ if પૂરું કરી નાખ્યું, એટલે block હંમેશાં ચાલે છે
  2. કંઈ નહીં, કારણ કે 20 condition માં fail છે
  3. semicolon પર compile error
  4. Fail
Show the answer

Pass, semicolon એ if પૂરું કરી નાખ્યું, એટલે block હંમેશાં ચાલે છે

if(...) ના બરાબર પછીનો એ semicolon એક પૂરું, ખાલી statement છે: "if condition, કંઈ ન કરો". એ પછીનો { printf } block ALAG code છે જે વગર શરતે ચાલે છે, એટલે 20 પણ Pass print કરે છે. એ ચોખ્ખું compile થાય છે, એટલે જ = વિરુદ્ધ == પછી આ C નો સૌથી જાણીતો trap છે. દરેક if ને રખડતા semicolon માટે ચકાસો.

Quiz

Grade ladder માં marks = 90 એ marks >= 70 અને marks >= 60 બંને સંતોષે છે. શું print થાય છે?

  1. ફક્ત Grade A: ladder પહેલી સાચી condition પર અટકે છે
  2. Grade A અને Grade B, બંને conditions સાચી છે
  3. Grade B, પછીનો match પહેલાને override કરે છે
  4. Compile error: overlapping conditions
Show the answer

ફક્ત Grade A: ladder પહેલી સાચી condition પર અટકે છે

else-if ladder એ એક નિર્ણય છે, ઘણી અલગ ચકાસણીઓની શ્રેણી નહીં: ઉપરથી નીચે, PEHLI સાચી condition execution પોતાના નામે કરી લે અને બાકીના rungs સાવ છૂટી જાય. બંને-print વાળા જવાબ ladder ને અલગ ifs ગણે છે, એ જ ગેરસમજ પકડવા આ સવાલ છે.

Watch out

3 if killers

if(...) પછી semicolon: ખાલી statement, block હંમેશાં ચાલે (ઉપર). condition માં == ને બદલે =: assign કરે અને સાચું વંચાય, Unit 1 નો trap એના ખરા ઘરમાં. ખોટો ladder ક્રમ: 70 પહેલા 35 ચકાસો અને toppers ને Grade C મળે. Nested ifs ની bonus ઝીણવટ: એક else સૌથી નજીકના unmatched if સાથે જોડાય છે, એટલે braces થી એ જ કહો જે તમારો અર્થ છે.

Theory

Selection ખૂલી ગયું

Structured programming ના 3 આકાર યાદ છે? હવે બીજો તમારી પાસે છે: selection. Marks program judge કરી શકે છે. પણ 5-option વાળા MENU (marks જુઓ, percentage, grade, exit...) માટે if-else ladder ઝડપથી બેડોળ થઈ જાય, અને C exact-match menus માટે એક ખાસ ઓજાર રાખે છે: switch-case, હવે પછીનો lesson, break statements સહિત.

Summary

Key takeaways

  • Simple if: condition સાચી હોય તો block ચલાવો; if-else: 2 અલગ રસ્તા.
  • else-if ladder: ઘણા રસ્તા, ઉપરથી નીચે ચકાસાય, PEHLO સાચો જીતે, બાકીના છૂટે.
  • Ladder ક્રમ મહત્વનો છે: સૌથી ઊંચો grade પહેલા ચકાસો.
  • Nested if: સવાલની અંદર સવાલ (pass, પછી distinction).
  • Killer traps: if(...) પછી semicolon, == ને બદલે =, ખોટો ladder ક્રમ.
  • યાદ રાખવાની યુક્તિ: toll booths, પહેલો સંતોષાયેલો booth તમને બહાર વાળે.

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