Showing posts with label constant. Show all posts
Showing posts with label constant. 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?

Sunday, September 29, 2024

Understanding PHP Data Types, A Practical Guide

Even though we have already installed and prepared everything we need to learn and program a programming language, as we prepared to learn the PHP programming language in the previous PHP tutorial post, see here; and since we have printed the famous "Hello World" on the local server web page; the first steps in programming begin with variables. Variables in programming are places in computer memory where data is stored that the program uses for calculation, processing, and manipulation. In the PHP programming language, as in other programming languages; variables are names used to reference places in memory. The easiest way to understand variables is to think of them as memory boxes. But not all boxes are the same. In some, you can put a value that represents an integer, while in others you can put floating-point numbers. There are also boxes in which you can put a set of any characters. To know what value, you can put in a variable, you need to know what data type you have decided to assign to a variable. All programming languages have variables and certain data types.

Most basic variable types are essentially the same or similar. However, some programming languages have more data types while others have fewer. Also, even the same name of a variable type in many programming languages can have different limitations. For example, in the PHP programming language, the float data type has the precision of the double data type, which means that the value of floating-point numbers is much larger than it would be if the data type was float in the C# programming language. Or instead of the char data type in C#, the string type is used in the PHP programming language, while char is a function used to convert an ASCII number to a character. So that you are not confused at the very beginning of learning data types, it is best to concentrate on the data types that the PHP programming language has.

The boss explains the importance of data types in PHP to a new employee

The boss explains the importance of data types in PHP to a new employee

Variables are the fundamental building blocks of every program. They serve as containers for data that can be changed and manipulated throughout the program's execution. In PHP, variables are declared simply by assigning a value to a name. The data type of a variable is determined automatically based on the assigned value. Variable names can include letters, numbers, and underscores, but they must start with a letter or an underscore. Additionally, PHP requires a dollar sign ($) before each variable name to identify it. For example, in this line, we've created a variable named $number and assigned the integer value 10 to it.

$number = 10;

It's essential to choose clear and descriptive variable names to enhance code readability. For instance, if a variable holds the value of the 13th salary, a suitable name would be $thirteenthSalary. Remember that PHP is case-sensitive, meaning $number and $Number are considered different variables. A common convention in PHP is to use camelCase for variable names, as seen in $thirteenthSalary.
Unlike many other programming languages, PHP doesn't require explicit type declarations. When you assign a value to a variable, its data type is inferred automatically, and the necessary memory is allocated.

Basic Data Types in PHP: Simply Explained