Monday, September 8, 2008

C...learning: Chapter 2 of 8 "Control Commands"

Well Yesterday Proceeded to the 2ond chapter "Control Commands". Which included if, if-else, switch, while, do-while, for, continue, return, goto, exit()

It was a quick look more likely since i know most of them from my short C++ experience.

In switch however there is the difference that in the end, you have "default()" rather than "else".

The triple operand was something new to me, but despite the initial complexity I got it. A well shrinked form of if-else but which on first look I think has restricted capabilities - only variables checking - and so I dont think I will be using it much.

edit After Project was Back Online

-------------------------------------------------------------------------
This is an image showing the nature of the triple operand. With the experience of a Project like battleship i came up that it can come usefull in saving you tons of code.





-----------------------------------------------------------------------------

Continue and exit() were new to me too despite tehy existed in C++.

The for form in Confused me with the initialization command. I didnt remembered it. The basic form means

Form Expalnation

//-----------------------------------------------------------------------------------------------

for (a=0/*assign 0 to a*/; a>0/*while a>0*/,a++/*repeat (while a>0)*/)

//-----------------------------------------------------------------------------------------------

Appears idiot to be found there an initialization function and thats why I was confused

I had also some issues with return(). I couldnt understand why it was termintaing my programms in main despite I wasnt putting "return 0"

printf("Start. Give a\n");
scanf("%d",&a);
printf("\na is %d\n",a);
if (a==2)
{
return a;
printf("a is 2\n");
}
return 0;


Well thanks to the forums I've cleared up things.

"If you are in main(), the runtime library has called main() function. In the case of Windows, the routine that calls main () is mainCRTStartup() ). So using return; within main just goes back to mainCRTStartup(). mainCRTStartup() returns to kernel32.dll which terminates your program.

In other words, in all cases, return; only returns from a function. It is not what terminates the program--the only thing that can is the OS
."

"The return keyword calls the end of a function that has a return value. So if you had a different function that returned the length of a string, for instance, you would not leave that function until you reached a return statement or else some exception occurred and you went out of the functions scope. So when they say that it returns control to the main function it means its then end of the previous function. It does not output the value of two to the screen because that would require a specified output stream and no it does not work like the break keyword."

example

#include
using namespace std;

float add (float x, float y) // a simple addition function
{
//---------------------------------\/HERE\/-----------------------------
return x + y;
// this will return to wherever it was called from//<---
//---------------------------------/\HERE/\-------------------------------
}

int main ()
{
float t = add (4.33f, 299.3f);
return EXIT_SUCCESS; // nowhere to return from, so we exit
}

So basicaly its usance is to pass values back in the "mother functions" for asignation of variables from another function

No comments: