Showing posts with label rand. Show all posts
Showing posts with label rand. Show all posts

Sunday, March 09, 2025

Efficient Code Repetition, Using Loops in C Programming

Loops can be described as key elements in programming, enabling the repetition of certain blocks of code while a condition is met. Most modern programmers couldn't imagine coding without loops. However, if you were to go back to 1972, in the earliest versions of the BASIC language, loops as we know them today did not exist in a structured form, and programmers relied on commands like GOTO or jump to control the program flow, including simulating loops. This was characteristic of the first iterations of BASIC, which was designed in 1964 by John Kemeny and Thomas Kurtz at Dartmouth College, with the goal of being simple for beginners.

However, was the GOTO command alone sufficient? Theoretically, the GOTO command is sufficient to simulate any loop because it is a Turing-complete mechanism – any control structure can be expressed with jumps and conditions. However, in practice, it was inefficient and prone to errors. That's why loops were introduced as a response to criticism, including Edsger Dijkstra's famous 1968 article, 'Go To Statement Considered Harmful,' which pointed out the problems of unstructured code.

In the C programming language, the loops: for, while, and do-while, existed from the very beginning, that is, from its creation in 1972, when Dennis Ritchie developed it at Bell Labs. The C programming language was designed as a successor to the B programming language, which itself was a simplified version of BCPL, and one of the goals was to provide a higher level of program flow control compared to its predecessors, while maintaining closeness to machine code.

Loops have always existed in the C programming language

Loops have always existed in the C programming language

Loops were an integral part of that design, so there was no moment in the history of the standard C programming language where they were absent. Unlike BASIC, which initially relied only on GOTO, the C programming language was designed with loops as a standard tool, inspired by higher-level languages like ALGOL, but adapted for low-level efficiency.

for: For iterations with a known number of repetitions.

while: For conditional loops where the condition is checked before execution.

do-while: For conditional loops where the condition is checked after execution.

In early versions, variable declaration within the for loop header was not allowed; variables had to be declared outside the loop. This changed with the C99 standard, which introduced block scope. With the ANSI C standard in 1989, known as C89/C90, loops were formally defined and have not undergone significant changes in later standards C99, C11, C17, and C23. All three loops have remained consistent in syntax and semantics, showing that Ritchie's design was robust from the very start.

Why Doesn't the C Programming Language Have a 'foreach' Loop, While C++ Has an Alternative?

Saturday, October 31, 2015

Standardne funkcije u C++ programskom jeziku

Najčešće standardne funkcije koje se najviše koriste u C++ programskom jeziku su C-String funkcije, funkcije za pojedinačne znakove, matematičke funkcije, funkcije za slučajno biranje brojeva i funkcije za datum i vreme. Rad sa funkcijama iz standardne biblioteke je zaista velika pomoć prilikom programiranja i pored toga što se na primer u C# programskom jeziku, ništa ne može porediti sa .NET Framework klasama, tako se ništa ne može porediti sa konstantnom upotrebom biblioteke standardni funkcija u C programskom jeziku, ali za  C++ programski jezik, standardna biblioteka nije baš bezbedna dok postoje i bolja rešenja. Ono što izdvaja standardne funkcije je njihova jednosatvna upotreba i navika C programera. Upotreba standardne biblioteke zahteva da uvezete određeni header fajl u vaš program. Najbolje će te shvatati standardne funkcije njihovom upotrebom kodiranjem.


( The C++ Standard Library )

C-String funkcije

C-String funkcije se primenjuju na klasične char* stringove, C programskog jezika <cstring>. Nemojte to da mešate sa funkcijama klase string <string> o kojoj je bilo više reči u postu „Rad sa stringovima u C++ programskom jeziku“. Ja lično ne preporučujem da koristite <cstring> ili objekte Cstring u vašim C++ programima. Uvek koristite <string>. Ali pošte će te često nailaziti na ovakav kod u nekim primerima i starijim projektima, čisto da prepoznate o kakvim funkcijama se radi. Upotreba <cstring> funkcija nije bezbedno koristiti i zato će Microsoft Visual Studio prijavljivati vam grešku kad pokušate da koristite <cstring> osim ukoliko ne naznačite da ne prijavljuje; kliknite desnim tasterom miša na Project u Solution Explorer.

Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions

Unesite:

               _CRT_SECURE_NO_WARNINGS

Pogledajte primer i analizirajte kod programa kako se upotrebljavaju <cstring> funkcije: