then and goto

I did a search here but found no talk about “then” and “goto” I remember that we used it in “basic” can you use it in easy C?, maybe in C? Thanks

Update, making great progress with our robot. have all the senors working, have the robot running around the house chasing the cat!

Is “switch” “Case” and “Default” The same thing as then and goto? I think it is, but more reading and trial and error are called for! But that is enough for tonight. From 3:00pm to 11:15pm, yesterday and today. But what the heck it is more fun than a person should be allowed!

[FONT=“Courier New”]Switch[/FONT], [FONT=“Courier New”]case[/FONT], and [FONT=“Courier New”]default[/FONT] in C aren’t really the same thing as [FONT=“Courier New”]then[/FONT] and [FONT=“Courier New”]goto[/FONT] from BASIC. In BASIC, you would do something like

10 If variable > 10
20 Then
30 variable = variable + 1
40 Goto 10
50 Else
60 variable = 0
70 End

Where [FONT=“Courier New”]then[/FONT] and [FONT=“Courier New”]end[/FONT] are used like the curly brackets { } in C programming. They tell conditional loops which contain multiple statements where to begin and end. But since C programming does not use [FONT=“Courier New”]goto[/FONT], you’ll need to do recursive functions or use something else like a [FONT=“Courier New”]for[/FONT] or [FONT=“Courier New”]while[/FONT] loop.

[FONT=“Courier New”]Switch[/FONT], [FONT=“Courier New”]case[/FONT], and [FONT=“Courier New”]default[/FONT] in C programming are just a fancy way of doing a special [FONT=“Courier New”]if else[/FONT] style loop. Here’s the basic syntax:

switch (variable)
{
    case 1:
        printf('variable is equal to 1');
        break;
    case 2:
        printf('variable is equal to 2');
        break;
    default:
        printf('In case variable does not equal 1 or 2, the default clause will catch every other result. Note you do not need a break here.');
}

Thank you, looks good

The if then else C structure is easier for a small number of conditional statements. The “then” translates in C to “{” and end translates to “}”. You can code the BASIC example in C as follows:

if (variable > 10)
{
variable++;
}
else
{
variable = 0;
}

Remember that ‘C’ is just a “high level” Assembler

Actually, ‘C’ does have goto, but the “Tenets of Structured Programing” frown upon the use.

In ‘C’ the “test conditions” of the “if”, “while”, and “do…while” are any TRUE/FALSE condition, which can be a large “performance hit” when testing Floating Point values or De-Referencing Pointers for comparisons.
(IIRC) The “test conditions” of the “switch…case” are always done in an Integer (The Integer being the same size of the Register Size of the Target Machine), thus leading to the most efficient Test and Branch code.

Depending on the ‘C’ Compiler and its optimizations, a “switch…case” (with Integers) could be executed more efficiently than an “if…else” with Integers.

As usual… “Use the Right Tool for the Job”… If you can perform your choices with Integers for tests, use the “switch…case” over the “if…else”.

The “END” terminates the “ELSE”, if there is one, otherwise it terminates the “IF”. In ‘C’ (and ‘C++’ and maybe Java) what “falls” between the Beginning Brace ( { ) and the Ending Brace ( } )is called the Scope. “IF”, “ELSE”, and “END” are just BASIC’s Scope delimiters.