Theory
2 જાતના class notes
Exam પહેલાં 2 toppers તમને notes ઉછીના આપે છે.
Notebook એક ઉપરથી નીચે વંચાય છે: headings, numbered points, પતી ગયું.
Notebook 2 એક ભુલભુલામણી છે: "page 12 જુઓ", page 12 કહે છે "page 3 થી આગળ વાંચો, જો page 7 થી ન આવ્યા હો તો". દરેક line ક્યાંક બીજે ઈશારો કરે છે. 10 minute માં તમે ખોવાઈ ગયા.
શરૂઆતના programs notebook 2 જેવા હતા, અને એમના પર planes અને payrolls ચાલતાં. જે સફાઈ થઈ એનું નામ છે structured programming, અને C એને લાગુ કરવા માટે જ બની છે.
Theory
Spaghetti વિરુદ્ધ બાંધેલી ચોપડી
આડેધડ jumps થી ભરેલા code નું nickname પડ્યું spaghetti code: એક strand ખેંચો અને 20 હલે છે, કારણ કે goto statements program ને વાટકાના noodles ની જેમ આરપાર જોડે છે. Structured program બાંધેલી ચોપડી છે: chapters (functions), sections (blocks), દરેક ઉપરથી નીચે, એક entry, એક exit. તમે chapter 5 ખોલીને આખો વાટકો ખોલ્યા વગર એને સમજી શકો છો.
Theory
3 shapes, બરાબર
Structured programming નો દાવો, 1966 માં mathematically સાબિત: કોઈ પણ program logic ફક્ત 3 structures થી બની શકે:
- Sequence: statements એક પછી એક, ક્રમમાં.
- Selection: condition થી રસ્તો પસંદ કરવો (
if,if-else,switch). - Iteration: જ્યાં સુધી condition સાચી છે ત્યાં સુધી દોહરાવવું (
while,do-while,for).
બધું blocks { } અને functions ની અંદર nest થાય છે, દરેકની single entry અને single exit. કોઈ જંગલી કૂદકા નહીં. C તમને બરાબર આ જ tools આપે છે, એટલે જ એ structured language કહેવાય છે.
At a glance
ત્રણેય control structures
| Structure | કયા સવાલનો જવાબ | C ના constructs |
|---|---|---|
| Sequence | આગળ શું થશે? | Statements, એક એક line |
| Selection | કયો રસ્તો લઈએ? | if, if-else, switch |
| Iteration | કેટલી વાર? | while, do-while, for |
Quiz
આમાંથી કયું structured programming ના 3 control structures માં NATHI?
- આડેધડ કૂદકા (goto)
- Sequence
- Selection
- Iteration
Show the answer
આડેધડ કૂદકા (goto)
Structured programming બની જ એટલા માટે કે આડેધડ goto jumps ની જગ્યાએ શિસ્તબદ્ધ 3 આવે: sequence, selection, iteration. C માં goto keyword હજી છે, દુર્લભ emergencies માટે (Unit 2 માં મળશે), પણ discipline નો આખો અર્થ એ છે કે એની જરૂર લગભગ ક્યારેય ન પડે.
Think first
તમારા જ program ની ઝલક
Marks program માટે આગળ વિચારો: student નું નામ print કરવું, પછી pass કે fail નક્કી કરવું, પછી class ના બધા 60 students ને process કરવા. દરેક ભાગ ત્રણમાંથી કયું structure વાપરશે?
Show the answer
Lines ક્રમમાં print કરવી: sequence (આજે program પાસે બસ આ જ છે). Pass કે fail: selection, એ જ if-else જે તમે Unit 2 માં લખશો. બધા 60 students: iteration, એના પછીના જ loops. આખા subject નો roadmap ખરેખર 3 structures જ છે, ક્રમમાં ભણાવેલા. આ સંજોગ નથી; આ syllabus નું theory પાછળ ચાલવું છે.
Watch out
Marks ક્યાં કપાય છે
Structured ને object-oriented સાથે ભેળવી દેવું: C structured છે, OOP નહીં; classes અને objects આગળના semesters માં C++/Java ના છે, અને definition માં બંને terms ભેળવવાથી marks કપાય છે. બીજું leak: ત્રણેય structures ને ગોળગોળ કહેવું ("loops વગેરે"). ત્રિપુટીને sequence, selection, iteration નામથી યાદ રાખો, દરેક સાથે C નું એક construct, એ જ exact wording definition ના marks લાવે છે.
Theory
આ discipline તમારો પગાર કેમ બનશે
Structured code શણગાર નથી: આ જ કારણે teammate તમારો આખો program વાંચ્યા વગર તમારું function સુધારી શકે છે, testers blocks ને અલગ અલગ test કરી શકે છે, અને million-line systems સંભાળી શકાય છે. દરેક code review ચૂપચાપ આ જ પૂછે છે: "is this structured?". આગળનો lesson એ tools નાં નામ આપશે જે આવા code ને ચલાવે છે: editor, compiler, interpreter.
Summary
Key takeaways
- Structured programming આખી logic 3 structures થી બનાવે છે: sequence, selection, iteration.
- Code blocks અને functions માં ગોઠવાય છે, single entry અને single exit સાથે.
- આડેધડ goto jumps spaghetti code બનાવે છે; discipline એનાથી જ બચવા માટે છે.
- C structured language છે; structured એટલે object-oriented NAHI.
- ફાયદા: ઉપર-થી-નીચે readability, અલગ testing, teamwork.
- Memory hook: sequence, selection, iteration, બસ આ 3 shapes જોઈએ.