Za petlju u C # - kako to radi
U C # programima vam je redovno potrebna petlja. Objašnjavamo kako to funkcionira.
Stvori za petlju u C #
A for petlja u C # uvijek je strukturirana po istom principu: započinje izrazom "za" i uvjetima u zagradama: "for ()"
- U zagrade prvo unosite vrstu podataka varijable pokretanja (poput "int", "short" ili "float") sa početnom vrijednošću (podcrtanom crvenom bojom na slici). Točka zarezom zatvara definiciju početne vrijednosti. Pregled tipova podataka u C # možete pronaći u ovom praktičnom savjetu.
- Sada definirajte uvjet prekida (podcrtano narančastom bojom na slici). Često se događa da varijabla pokretanja dosegne određenu vrijednost. Uvjet prekida može se odnositi i na ostale varijable. Ovaj dio odvajate i od ostatka zarezom.
- Na kraju definirate što bi se trebalo dogoditi nakon svakog prolaska petlje (podcrtano zelenom bojom na slici). To je često povećanje ili smanjenje varijable pokretanja. "++" je sinonim za "+ = 1". Možete promijeniti i ostale lokalne varijable nakon svakog pokretanja.
- Ako se kôd za petlju širi preko nekoliko linija, oko vanjske strane morate staviti kovrčave nagrade.
- Opasna točka za petlje je beskrajna petlja: Uvijek se uvjerite da će se u određenom trenutku pojaviti uvjet prekida.
- Posebno je lako stvoriti petlje u uređivačima kao što su "Visual Studio Express" ili "Notepadd ++" jer ti urednici ističu naredbe u boji i prikazuju poruku ako se nedefinirane varijable slučajno koriste.
- Za bolje razumijevanje petlje, pomoći će vam objašnjenje petlje za ostale programske jezike, poput praktičnog savjeta o petlji za javu u Javi ili našeg videa o petlji u C-u:
Možete dobiti pomoć u programiranju i za svoj prvi C # program kao i linkove za mrežno programiranje u ostalim CHIP praktičnim savjetima.