ForumCoin Special Offer: .info Domain for only 60 ForumCoin Close
Forum Coin is an online community where you earn Forum Coin for making posts and referring other members. You can use that Forum Coin as a virtual currency to buy and sell goods and services and to exchange for traditional, real world currencies. Find out more!
Get paid up to 150 ForumCoin to submit your article.

[C Programming] Operators and Statements [Win 50 FC.]

Postby JusApee » 05 Jan 2017, 21:51

C Programming - Course #4

Operators and Statements (Part II)


ThinkIT! Win 50 FC...
Look around the course for ThinkITs and answer them. The one who has the most ThinkITs at the end of the course (the entire course, not just course #2) gets 50 FC. If more of you have the maximum amount of ThinkITs, I'll choose two of you with random.org
Please comment your answers and try not to copy others' answers. I can figure out who copied who.

Catching up...
So last time we talked about operators and statements. In this course we'll continue pointing out some C-language keywords and try to explain them before moving on to something new.

break statement
In the last course, we used the statement in order to stop a switch from executing further instructions when meeting our required case. However, break has more uses than this. break is actually used to STOP a repetitive statement from further executing.
Code: Select all
for (i = 1;i<200;i++) {
   if (i == 50) {
      break;      // this will stop the for at the 50th step
   }
}
other_statement;   // after break, this will be executed


Code: Select all
while (some_condition) {
   ...
   ...
   if (some_other_condition) {
      break;
   }
   ...
   ...
}

Therefore, you see, break can be used to quit a repetitive instruction before it's finished. You may find this useful in numerous cases.

Example problem: Given a series of integers (not 0), print the sum of all the integers until the first 0 is read. (add all read numbers until 0 is read)
[+] Spoiler
Code: Select all
#include <stdio.h>

int main() {
   int x, sum = 0;
   while (true) {
      scanf("%d", &x);
      if (x == 0) {
         break;
      }
      else {
         sum = sum + x;
      }
   }
   
   printf("The sum of the given numbers is: %d", sum);
   return 0;
}

Explanation:
The code above will read numbers from the console until a 0 is read. If 0 is read, we call the break instruction, therefor we jump out of the while, printing out the sum until then.


continue statement
The continue statement is basically the opposite of break. continue jumps to the next step of the repetitive instruction preventing the following instructions to be executed. Why is this useful?

Example problems: Print the amount of odd and even integers in a non-0 int series.
[+] Spoiler
Code: Select all
#include <stdio.h>

int main() {
   int x;
   int nrEven = 0, nrOdd = 0;
   while(true) {      // after the continue, the code comes here.
      scanf("%d", &x);
      if (x == 0) {
         break;
      }
      
      if (x%2 == 0) {   // means number is even
         nrEven++;   // increments the number of even integers
         continue;   // stops further executing of the while and goes to another iterations
      }
      
      nrOdd++;      // if number is Odd and the if is not executed
   }
   return 0;
}


goto statement
The goto statement is used to jump from a certain statement to another.
Code: Select all
   ...
   ...
   goto myDestination;
   ...
   ...
myDestination: some_statement;
   some_other_statement;
   ...
   ...

This can be done the other way around:
Code: Select all
myDestination:   some_statement;
   ...
   ...
   ...
   if (apples.color == GREEN) {
      goto myDestination;
   }
   ...
   ...
   ...


In general, the usage of goto is avoided because it can result in "spaghetti" code, which is a bad coding habit due to it's hard readability. When is it useful, though?
Well, it can be useful for all sorts of things, but the most useful thing I can think off right now is "fast exiting" a multi-level repetitive instruction.

Code: Select all
while (some_condition) {
   ...
   for (exp_1;exp_2;exp_3) {
      ...
      while (some_other_condition) {
         ...
         if (BIRDS_ARE_SINGING)
            goto outOfLoops;
         ...
         ...
         if (BIRDS_ARE_JUST_FLYING)
            break;
         ...
         ...
      }
      // break will set the code here;
      ...
   }
   ...
}

outOfLoops: next_statement;      // goto jumps all the way out of the loop
rest_of_the_program;
...


Bitwise operators
For these types of operators, we require special types of operands:
  • integers (signed/unsigned)
  • characters

The full list of bitwise operators is:
  • ~ (NOT) - negates all the bits of the number
  • << - shift to left (below)
  • >> - shift to right
  • & (AND) - logic AND between the bits of the 2 numbers
  • ^ (XOR) - logic XOR between the bits of the 2 numbers
  • | (OR) - logic OR between the bits of the 2 numbers


Code: Select all
1: a<<b;
2: a>>b;


In the first case (a<<b), all the bits in a's binary representation will move to the left by b places. The equivalent (numeric) result is a*(2^b), where '^' means 'to the power'.
In the second case (a>>b), all the bits in a's binary representation will move to the right by b places. The equivalent (numeric) result is a/(2^b), where '^' means 'to the power'.
Here is an example of bitwise shifting:
Image

Example: Multiply a number by 10 without using the * operator.
[+] Spoiler
Code: Select all
#include <stdio.h>

int main() {
   int n, m, k;
   printf("n: ");
   scanf("%d", &n);
   m = n<<1;   // equivalent to m = 2*n;
   k = n<<3;   // equivalent to k = 8*n;
   n = m+k;   // equivalent to n = 2*n + 8*n = 10*n;
   printf("10*n = %d", n);
   
   return 0;
}


Operator &
Bit-level AND operation:
105 = 0000 0000 0110 1001
186 = 0000 0000 1011 1010
Code: Select all
105 & 186 = 0000 0000 0010 1000 = 40;


Operator |
Bit-level OR operation:
Code: Select all
105 | 186 = 0000 0000 1111 1011 = 251;


Operator ^
Bit-level XOR operation:
Logical XOR is an OR operation where identical bits result in a false output.
Image

Type conversion operators
  • explicit - special operator: (TYPE)expression;
  • implicit - by the compiler

Implicit data conversion works like this:
Code: Select all
integer_variable = float_variable;   //   the float is approximated to the closest integer
float_variable = integer_variable;   //   the integer is represented as a floating number (integer.0000)


Explicit data conversion:
Code: Select all
int k, m;
float average;

average = (float)(k+m)/2;      // arithmatic average of k and m


In the end...
This sums up everything about operators and statements. There were no ThinkITs this course, but stay tuned!
  • 2

JusApee
 
Posts: 46
Reputation: 15
ForumCoin: 341

Return to Articles & Tutorials *NEW

Who is online

Users browsing this forum: sprite1950 and 1 guest

Reputation System ©'