Understanding DART syntax: Identifiers, Datatypes, variables, comments; Decision making (if, if..else, if..else if..., switch..case); Iterative statements (for, for...in loop, while, do..while); break, continue, label

Java eyes से Dart syntax: familiar if/switch/loops, plus वे accents जो अलग हैं: var एक fixed type infer करता है, List arrays replace करता है, for-in collections walk करता है, और labels break को nested loops से escape करने देते हैं।

12 min read · 9 cards · 2 checks

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


Theory

Cold पढ़िए

किसी भी teaching से पहले, यह Dart पढ़िए और इसका output predict कीजिए:

int day = 2;

if (day == 2) {

print('Workshops today');

} else {

print('Regular schedule');

}

आपने इसे perfectly पढ़ा: यह Workshops today print करता है: क्योंकि Dart का control flow IS आपकी Java, एक lighter jacket में। यह lesson पूरी syntax को fast way से tour करता है: familiar पर skate कीजिए, उन 4 जगहों पर hard brake कीजिए जहाँ Dart अलग है।

At a glance

Datatypes (Rows 3, 6 और 7 पर Brake कीजिए)

Typeक्या Hold करता हैExample
int, doubleपूरे, decimal numbersint seats = 350; double fee = 49.5;
numint या double, EITHERnum anyNumber = 350;
StringText: capital S, single quotes idiomaticString fest = 'TechnoUtsav';
booltrue / falsebool open = true;
ListDart का array: growable, 0 से indexedvar events = ['Garba', 'Robo'];
MapKey-value pairsvar seats = {'Garba': 350, 'Robo': 80};
var / dynamicvar पहले INFER करता है फिर fix करता है; dynamic checking से opt out करता हैvar day = 2; // an int forever

Theory

Variables और Comments: Accents

var initializer से type deduce करता है और इसे fix करता है: var seats = 350; seats को एक int बनाता है; seats = 'full'; बाद में एक compile error है। Inference looseness नहीं है।

dynamic असली opt-out है (checking runtime तक deferred होती है): rare, deliberate।

final = runtime पर एक बार set (final festName = fetchName();); const = एक compile-time constant (const maxSeats = 350;)।

Comments: // और /* */ Java की तरह, plus /// documentation comments जो tooling पढ़ता है: Flutter की libraries इसी तरह annotated हैं।

Practical

Decisions, हर Loop, और Label Trick

void main() {
  var events = ['Garba Night', 'Coding Contest', 'Robo Race'];
  int day = 2;

  // switch: Java-shaped, break per non-empty case
  switch (day) {
    case 1:
      print('Inaugration day');
      break;
    case 2:
      print('Workshops today');
      break;
    default:
      print('Regular schedule');
  }

  // for-in: the collection walker (new accent)
  for (var e in events) {
    print(e);
  }

  // classic for, while, do-while: exactly your Java
  for (int i = 0; i < 2; i++) { print('pass $i'); }

  // LABEL: break out of BOTH loops in one jump
  outer:
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      if (i * j == 4) {
        print('found at i=$i, j=$j');   // i=2, j=2
        break outer;                    // exits both loops
      }
    }
  }
}

Quiz

var seats = 350; और 2 lines बाद: seats = 'Housefull'; Dart क्या कहता है?

  1. ठीक है: var का मतलब है type कभी भी बदल सकता है
  2. Compile error: var ने initialization पर int infer किया और type हमेशा के लिए fixed है
  3. Runtime error जब String पहली बार एक number की तरह इस्तेमाल हो
  4. ठीक है, पर seats तब से dynamic बन जाता है
Show the answer

Compile error: var ने initialization पर int infer किया और type हमेशा के लिए fixed है

var INFERENCE है, permissiveness नहीं: initializer का type (int) variable का fixed, compiler-checked type बन जाता है, और एक String enter नहीं हो सकता: एक Java reader के लिए single most important Dart accent, और इस unit का favourite exam question। Option A dynamic describe करता है, वह deliberate opt-out जो आपको स्पष्ट रूप से spell out करना होता है। Option C एक error postpone करता है जो Dart compile time पर raise करता है (static typing का पूरा point)। Option D एक type mutation invent करता है जो कोई assignment perform नहीं कर सकता। Rule: var एक बार, birth पर, देखकर decide करता है।

Think first

Label Trace कीजिए

Listing के nested loop में, i और j को hand से walk कीजिए: break से पहले कौन से (i, j) pairs चलते हैं, क्या print होता है, और plain break (बिना label) अलग क्या करता?

Show the answer

Order में pairs: (1,1), (1,2), (1,3), (2,1), फिर (2,2) जहाँ ij = 4: prints found at i=2, j=2, और break outer; दोनों loops एक साथ exit करता है। एक plain break सिर्फ़ INNER loop को मारता है: outer i=3 के साथ continue करेगा, (3,1) चलाते हुए... जब तक ij फिर 4 hit न करे... actually (3, कुछ भी) 3, 6, 9 देता है: कोई और 4 नहीं, पर outer loop फिर भी wastefully अपना course चलाता है। Labels exactly इस एक honest need के लिए exist करते हैं: nested loops को एक jump में escape करना: Java के पास भी थे; Dart इन्हें examinable रखता है।

Watch out

4 Accents जो Java Speakers को पकड़ते हैं

var Fixed-Typed है: quiz; इसे अपनी wrist पर लिखिए।

String में Capital S है: string compile नहीं होता; और single quotes house idiom हैं।

List, Arrays नहीं: कोई int[] syntax नहीं; events[0] काम करता है, .length एक property है (कोई brackets नहीं: Java का array .length, String का .length() नहीं)।

कोई Standalone char नहीं: 'G' सिर्फ़ एक 1-character String है।

Theory

Skate और Brake: Second-Language Method

आपने अभी एक lesson में एक language का 90% सीखा क्योंकि आपने Java में properly invest किया: यही transfer है, और यही असली syllabus है। जहाँ आप revise करते हैं वहाँ accent list रखिए, और सिर्फ़ accents practise कीजिए: familiar parts खुद maintain होते हैं। Flutter से पहले एक language piece बाकी है: functions, जहाँ Dart अपनी सबसे best new tricks छुपाता है (arrow bodies, named parameters), अगला।

Summary

Key takeaways

  • Identifiers और comments: Java के rules, plus /// documentation comments।
  • Types: int, double, num (either), String (capital S), bool, List (growable array), Map।
  • var initializer से ONCE type infer करता है और fix करता है; dynamic rare opt-out है; final runtime-once, const compile-time।
  • if/else if/else और switch-case: Java-shaped, break per non-empty case।
  • Loops: for, collections के लिए for-in, while, do-while; break और continue known की तरह।
  • एक label (outer:) break outer देता है; nested loops को एक jump में escape कीजिए: i=2, j=2 पर traced।
  • Memory hook: familiar पर skate कीजिए, var, String, List, for-in पर brake कीजिए।

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 Working with DART

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

Understanding DART syntax: Identifiers, Datatypes, variables, comments; Decision making (if, if..else, if..else if..., switch..case); Iterative statements (for, for...in loop, while, do..while); break, continue, label · Mobile Application Development - 2 (option B) · Gri-Learn