iasteele

 

Anthony

Page history last edited by Anthony 2 yrs ago

Variable Types

 

Variable Types and Uses

float - to declare a floating (real) number

%f – To print a floating variable in a scanf or printf function

 

int - to declare an integer number

 

%i – To print an integer variable in a scanf or printf function

 

char - to declare a character variable

%c – To print a character variable in a scanf or printf function

 

Declaring Variables

To declare variables, use float, in or char, and then declare the variable:

int g;

would declare the integer variable "g"

 

To declare multiple variables, repeat, except separate them by commas:

int g, h, s, d;

would declare the integer variables g, h, s, and d

 

Printf

 

Use

printf is a function used to display characters, numbers, or variables on the screen.

 

Format

It is used like this:

printf("message - variables may be input here by the programmer (see variable types). To move the cursor to the next line, "\n" is used.", any variables used in the message must be declared here, in the same order that they were used.);

 

Scanf

 

Use

scanf is used in the same way as printf - however it lets the user enter variables.

 

Format

printf("Variables values may be input here by the user.", Any variables input into the message must be declared here, in the same order that they were used, separated by commas, and immediately preceded by the character &.);

So:

scanf("%i%i",&g, &c);

Would let the user enter values for the variables "g" and "c".

 

If/Else

 

If Uses

 

"If" runs a line of code IF a predetermined condition is true.

 

If Format

If is started by the word "if". Then, the conditions for the code to run are entered in parentheses. After that curlybraces are used to start and end the code sequence. So:

    if (g!=r)

    {

        printf("Wrong! Guess again:\n");

    }

means that if variable g is not equal variable r, the program will print "Wrong! Guess again:". If g is equal to r, it will skip the entire function.

 

Else Uses

The "else" function is always used after the "if" function. It tells the program that if the conditions specified in the "if" function are NOT true, it should run another bracketed line of code.

 

Else Format

Else comes the line after the "if" function's ending curlybrace. So:

    if (g!=r)

    {

        printf("Wrong! Guess again:\n");

    }

    else

    {

        printf("Correct! You guessed the number!:\n");

    }

Means that if g is equal to r, the program will print, "Correct! You guessed the number!." If the "if" condition is true, the "else" function brackets will be skipped entirely.

 

Loops (While and For)

 

While Uses

The "while" loop makes a bracketed section of code run as long as a certain condition is true.

 

While Format

The condition needed for the section of code to loop comes immediately after the word "while", on the first line of code. Then, the section is opened with a curlybrace and the code is entered in. The loop is closed with a second curlybrace. It is used like this:

while( g != r )

{

    printf("Print a number between one and 100:\n");

    scanf("%i%c",&g,&enter);

    s=s+1;

    if (g==r)

    {

        printf("Correct! You guessed the number! Your score was %i\n", s);

    }

    if (g>r)

    {

        printf("Wrong! Too high! Guess again:\n");

    }

    if (g<r)

    {

        printf("Wrong! Too low! Guess again:\n");

    }

}

This section of code will repeat until g is equal to r.

 

For Uses

For is used to loop a section of code until a "counter" is reached.

 

For Format

The "for" loop is announced and divided into three sections, all in parentheses on the first line of code, directly after "for", and all ended by semicolons.

The first part anounces the counter (can be any variable, called counter in example below) and its initial value. The second part announces when the loop will stop (in this case when the counter is less than predetermined variable "b"). The third part tells how the counter will change every time the loop is run once (not counting the first time).

    for (int counter = 0; counter < b; counter = counter +1)

    {

        rval = rval * a;

    }

 

    return rval;

This means that until the "counter" integer variable is less than variable "b", the loop will run. Every time the loop runs, the "counter" variable will increase by one.

 

Arrays and Strings

 

Arrays

Arrays are simply used to declare large numbers of number variables:

    num[5]

would create 5 variables - num[0], num[1] num[2] num[3] num[4]. Arrays can be made very useful with for loops.

 

Strings

Strings are used almost exactly like arrays, except they instead store large numbers of characters. The bracketed number indicates the maximum number of characters that can be entered.

 

Functions

 

Functions are used to neatly store code that can be accessed at any time in the main program code.

int add(int a, int b)

{

    return a+b;

}

This function takes two given integers - a and b, adds them together, and then returns that value to the main code. This code could be accessed by writing:

add (2, 5);

The function would then return 7 to the main code.

 

 

 

 

Comments (0)

You don't have permission to comment on this page.