Thursday, 18 January 2018

Basic1


Signed and unsigned variables

The difference between signed and unsigned variables is that signed variables can be either negative or
positive but unsigned variables can only be positive. By using an unsigned variable you can increase the maximum positive range. When you declare a variable in the normal way it is automatically a signed variable. To declare an unsigned variable you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable as signed since they already are.

Calculations and variables

There are different operators that can be used for calculations which are listed in the following table:
Operator
Operation
+
Addition
Subtraction
*
Multiplication
/
Division
%
Modulus(Remainder of integer
division)

         int main()
                                  {
                  
                                     int a ,b;
                                  a=1 ;
                           b=a+1;
                      a=b-1;
               return 0;
                                              }
      

Reading and printing

Calculating something without reading input or printing something on the screen is not much fun. To read input from the keyboard we will use the command scanf. (How to print something to the screen we all ready know).
    
    So let’s make a program that can do all these things:

#include<stdio.h>

int main()
{
 int a;

 scanf("%d", &a);
 a= a * 10;
 printf("Ten times the input equals %d\n",inputvalue);
 return 0;
}


the %d is for reading or printing a decimal integer value (It is also possible to use %i). In the table below you can find the commands for other types:
%i or %dint
%cchar
%ffloat
%lfdouble
%sstring

  

Boolean Operators

Before we can take a look at test conditions we have to know what Boolean operators are. They are called Boolean operators because they give you either true or false when you use them to test a condition. The greater than sign “>”
for instance is a Boolean operator. In the table below you see all the Boolean operators:
==Equal
!Not
!=Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
&&And
||Or
  
    

The if statement

The if statement can be used to test conditions so that we can alter the flow of a program. In other words: if a specific statement is true, execute this instruction. If not true, execute this instruction. So lets take a look at an example:
       #include<stdio.h>
     int main()
     {
             int mynumber;
            scanf("%d",&mynumber);
            if (mynumber==10)
              printf("is equal to");
          return 0;
          }
In the example above the user can input a number. The number is stored in the variable mynumber.
Now take a look at the “if statement”: if the number stored in the variable mynumber is equal to ten, then print “is equal” on the screen. Simple, isn’t it. If the number is not equal to ten, then nothing gets printed.
Now we like to also print something if the “if statement” is not equal. We could do this by adding another “if statement” but there is an easier / better way. Which is using the so called “else statement” with the “if statement”.
  #include<stdio.h>
int main()
      {
       int mynumber;
          scanf("%d",&mynumber);
              if (mynumber ==10)
                     {
                           printf("is equal \n");
                             printf("close program\n");
                       }
                            else
                 {
                          printf("not equal \n");
                       printf("closing program\n");
                    }
                             return 0;
                  }





























     



























































No comments:

Post a Comment