⇦ Ramen Development_

Every program can just only use WHILE and IF structures

Cover Image for Every program can just only use WHILE and IF structures

Do we need "for", "foreach", "else", "else if", "switch"?
The answer may depend on the programming language, but, no, you can replace it by doing some tricks with your best friends "continue", "break", "return", and organizing your code. Disclaimer: The example codes were made in C#.

Repetition Structures

FOR

Let´s start with the basic, the for that its just a compressed while.

Before

for(int i=0;i<10;i++)
{
  //Some code
}

After

int i=0;
while(i<10)
{
	//Some code
	i++;
}

FOREACH

The same we do with a for, but, using the iterator inside a data structure .

Before

foreach(var i in variables)
{
  Debug.WriteLine(i.ToString());
}

After

int i=0;
while(i<variables.Count)
{
	Debug.WriteLine(variables[i].ToString());
	i++;
}

DO-WHILE

It doesn´t even need an explanation.

Before

int option;
do
{
  //Some code
}while(option != 10);

After

int option=-1;
while(option != 10)
{
	//Some code
}

Control Structures

ELSE/ELSE-IF/SWITCH

This even makes your code more beautiful, if you think you need an else/else if, just put ifs, and if it gets in, stop. The Switch its just a lot of if-elseif-else

RETURN Example
Before

void function(int x)
{
  if(x==1)
  {
    //Some code
  }
  else if(x==2)
  {
    //Another code
  }
  else
  {
    //Another else code
  }
  
  return;
}

After

void function(int x)
{
  if(x==1)
  {
    //Some code
    return;
  }

  //This replaces an else if statement
  if(x==2)
  {
    //Another code
    return;
  }

  //This replaces the else
  //Another else code
  return;
}

REPETITION Example
Before

int i=0;
while(i<variables.Count)
{
	if(variables[i]==1)
  {
    //Some code
  }
  else if(variables[i]==2)
  {
    //Another code
  }
  else
  {
    //Another else code
  }
  

	i++;
}

After

int i=0;
while(i<variables.Count)
{
	if(variables[i]==1)
  {
    //Some code
    break;
    //or
    continue;
  }

  if(variables[i]==2)
  {
    //Another code
    break;
    //or
    continue;
  }

  //Another else code
  break;
  //or
  continue;
	i++;
}

You are free to use whatever you want when you´re programming, this is just a guide if you want to change for any reason your code, or if you are creating a compiler and you are creating some rules, this way you can optimize your work, doing less rules.

More entries

Cover Image for How I translated my portfolio with JavaScript and JSON

How I translated my portfolio with JavaScript and JSON

I didn't want to get into any extra trouble on my personal portfolio to have it ready a bit faster and try something new. So, I came up with a straightforward solution using only JavaScript. Let's get into it.

Ivan Orozco
Ivan Orozco
Cover Image for Flags to represent languages?

Flags to represent languages?

Should I use the flags of the countries with the most population speaking that language? what’s the best way to design the options for language switching?

Ivan Orozco
Ivan Orozco
Cover Image for Coding with emojis?

Coding with emojis?

Emojis are truly useful in some cases, they can add emotions to our messages so their true meaning doesn't fly over our recipient's head, but should we use them for coding?

Ivan Orozco
Ivan Orozco
Cover Image for Easy Simple Job backstory

Easy Simple Job backstory

The story behind our game isn´t complicated, inspired by a video narrating a story on Reddit that tells about a guy that gets a simple but suspicious job in an office, and certain scary things happen.

Ramen Development Team
Ramen Development Team
Cover Image for Dear LinkedIn recruiters...

Dear LinkedIn recruiters...

It is important for recruiters to take the time to review a candidate's profile before sending a job offer.

Ivan Orozco
Ivan Orozco
Cover Image for Lexical analyzer automaton

Lexical analyzer automaton

In this article you will understand how to code your own lexical analyzer.

Gustavo Padilla
Gustavo Padilla