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.