Author Topic: Coding and Scripting  (Read 12528 times)

Offline Chomp

  • Full Member
  • ***
  • Posts: 173
Re: Coding and Scripting
« Reply #75 on: December 01, 2009, 02:11:33 PM »
Okay, so lets start with some basic stuff.

In both coding and scripting languages, symbols other than letters and numbers are used. This example -->
Code: [Select]
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

contains such symbols. (This is C++ -- QT Creator was used in its creation.) The { and the } wrap the rest of the code. Used in this manner, they show a block of code. There may be many blocks of code in any given program. Other languages may define these differently.
The other symbols, (,  ),  ;,  .  and ,  have their "definitions," also.
So add to this. Define the usage of these symbols, please. Create a block of code, if you will and define how and why the symbol is used.

I realize we're currently voting on which language to start learning but I said I was going to do this so I am doing it.  I'm also hoping other would-be programmers find this a little useful, so here is the "assignment" with my own personal notes:

Code: [Select]
// hello_pclos.cpp example

// ( and ) are for indicating a function declaration(or definition).  You may include a list of parameters within them.

// ; indicates the end of a statement.

// ' allows you to separate identifiers to be used in a single statement

// . does what now?

// Code starts below

#include <iostream>

// Declaring header(library) to be used.  iostream(input/output) is an important standard c++ library that(among other things) provide classes that allow you to read from files and the keyboard, and write to files and the display(which appears in the console, but may be redirected to files or devices).

using namespace std;

// The definitions used in the iostream library are "wrapped" in a namespace.  Since the compiler will not find the functions(or objects) included in the 'iostream' library because of this you need to use the 'using' command to let the compiler know you want to use the declarations and definitions in the namespace.  All standard C++ libraries are wrapped in a single namespace, which is 'std'(for standard).

int main() {

// main() is a function definition.  You put your primary code here in between { and }, which is the body of your code.  Main has a return type of 'int'(integer?).  Note that main has an empty argument list(no parameters) at the moment.

cout << "Hello PCLinuxOS user! "

// cout is a variable(object) provided by iostream.  It stands for Console Output.
  
// The '<<' means "send to". So this line of code means "Display the following text in the console".  Because the operator '<<' can be "overloaded" with a variety of meanings when used with 'cout', you can send a variety of different argmuments and it will figure out what to do with them.  Note how ';' is not necessary to use until the end of all the (overloaded) statements.
  
// Text inside double quotes is tradionally called a "string".  However, the Standard c++ library has a powerful class called "string" for manipulating text, so author will be referring to the more precise term "character array".  Ugh.

<< "I like the number "
<< 7 << " for some reason." << endl;

// 'endl' is a special iostream function that outputs the line and a new line(ensuring that when the output is generated by 'cout' and ends that it starts you on a new line instead of displaying it like so:

// [foo@localhost ~]$ ./hello_pclos
// Hello PCLinuxOS user! I like the number 7 for some reason. [foo@localhost ~]$

}

// To see what this does open up Synaptic(make sure you check for and install updates first, and then insure that 'gcc-c++' is installed).  Either copy and paste this and save as "hello_plcos.cpp" or download the attachment I provided(.cpp is not an allowed file type so make sure you change the .txt extension to .cpp before trying to compile).  Open up a console and type in "g++ hello_pclos.cpp -o hello_pclos".  To execute it type in "./hello_pclos".

Here's what it looks like without all of the notes:

Code: [Select]
#include <iostream>
using namespace std;

int main() {
  cout << "Hello PCLinuxOS user! "
       << "I like the number "
       << 7 << " for some reason." << endl;
  }

[attachment deleted by admin]
« Last Edit: December 01, 2009, 03:53:03 PM by Chomp »

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15847
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Coding and Scripting
« Reply #76 on: December 01, 2009, 03:26:53 PM »
Hey thanks, Chomp. :D


Offline Chomp

  • Full Member
  • ***
  • Posts: 173
Re: Coding and Scripting
« Reply #77 on: December 01, 2009, 03:42:19 PM »
You're welcome Neal.  I know others have started their C++ lessons so if anyone would like to add to this or make corrections please do so.  . , int and main still need to be defined.

Offline gseaman

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 3793
Re: Coding and Scripting
« Reply #78 on: December 01, 2009, 03:43:21 PM »
Thanks, Chomp.

Did you know that for blocks of comments you can do this instead?

/*
Here is a bunch of comments
Here is a bunch of comments
Here is a bunch of comments
*/

This is a little easier to read than '//' in front of every line.

Thanks, maik3531, for your contribution. Can you explain the argv[][] array and the color codes?

Galen

Offline Chomp

  • Full Member
  • ***
  • Posts: 173
Re: Coding and Scripting
« Reply #79 on: December 01, 2009, 03:59:49 PM »
Thanks, Chomp.

Did you know that for blocks of comments you can do this instead?

/*
Here is a bunch of comments
Here is a bunch of comments
Here is a bunch of comments
*/

This is a little easier to read than '//' in front of every line.

Yes I knew that.  '//' seems to be commonly used in c++ as opposed to '/* */'.  However, I can see how '/* */' can be less confusing when posting it on the forums with a code block.  I use Kate so the syntax highlighting really makes everything clear, but there's no syntax highlighting in code block.  I'll use '/* */' in future postings.  In any case you can copy the code into a text editor(or whatever you prefer) that provides syntax highlighting for c++ and everything will be much easier on the eyes.

Offline critter

  • Full Member
  • ***
  • Posts: 220
Re: Coding and Scripting
« Reply #80 on: December 02, 2009, 03:28:30 AM »
Quote
Here's what it looks like without all of the notes:

Code:
Code: [Select]
#include <iostream>
using namespace std;

int main() {
  cout << "Hello PCLinuxOS user! "
       << "I like the number "
       << 7 << " for some reason." << endl;

  }
And here's what it looks like in plain old C

Code: [Select]
#include <stdio.h>

int main()
{
printf("Hello PCLinuxOS user!"
" I like the number 7"
" for some reason.\n");
}

Just for comparison :)
Motherboard   Gigabyte Z68X-UD3H-B3
Hard Drives      2 x Maxtor STM350032 500GB SATA
Memory      16GB RAM
Processor      Intel core i5 3.30GHz
Video         nVidia GeForce GT430
Sound      HDA Intel PCH
PCLinuxOS          KDE

Offline maik3531

  • Full Member
  • ***
  • Posts: 129
Re: Coding and Scripting
« Reply #81 on: December 02, 2009, 08:46:35 AM »
... Can you explain the argv[][] array and the color codes?


as in the bashrc

Code: [Select]
# Define some colors first:
BLACK='\e[0;30m'
red='\e[0;31m'
RED='\e[1;31m'
green='\e[0;32m'
GREEN='\e[1;32m'
yellow='\e[0;33m'
YELLOW='\e[1;33m'
blue='\e[0;34m'
BLUE='\e[1;34m'
purple='\e[0;35m'
PURPLE='\e[1;35m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
white='\e[0;37m'
WHITE='\e[1;37m'
nc='\e[0m'
NC='\e[0m' 

# Sieht am besten auf einem Terminal mit schwarzem Hintergrund aus .....
if [ "$(id -u)" -eq 0 ]; then # nur für root
echo -e "\n\t\t    ${WHITE}HIER BIN ICH ${RED}ROOT${WHITE}, HIER DARF ICH's SEIN!${NC}\n"
fi
if [ "$(id -u)" != "0" ]; then # nicht für root
echo -e "\n\t\t    ${CYAN}${DISTRI} ${DISTVER} / BASH ${RED}${BASH_VERSION%.*}${CYAN} - DISPLAY ${RED}$DISPLAY${NC}\n"

  if [ -x /usr/games/fortune ]; then
     /usr/games/fortune -s              # Macht unser Tag ein bisschen mehr Spaß... :-)
  fi
fi

function _exit()                        # Funktion ausgeführen beim Beenden der Shell.
{
    echo -e "${RED}Hasta la vista, baby${NC}"
}
trap _exit EXIT





Example: echo -e "${RED}Hasta la vista, baby${NC}" equal "argv[1] Hasta la vista, baby argv[2]"


regards
maik3531
Please excuse my English

Offline Chomp

  • Full Member
  • ***
  • Posts: 173
Re: Coding and Scripting
« Reply #82 on: December 02, 2009, 01:59:47 PM »
Thanks critter  ;D

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2250
Re: Coding and Scripting
« Reply #83 on: December 04, 2009, 09:43:33 AM »
You make it seem relatively simple to learn the basics of C. How difficult is it to learn the libraries and operators?
The operators just take some memorization - learning the difference between "=" and "==" for instance, or between "<" and "<<".  And how to use them (binary and unary operators).

I love the unary operators,  Rather than typing:
   variablename = variablename + 1;
you can type:
   variablename++;
and get the same results... 

It's even more useful with pointers - the compiler knows the size of the thing pointed to, and will increment the pointer by the correct amount (number of bytes).

And if it's in a conditional, you can put the ++ either before the name (to increment before evaluation) or after the name (to increment after evaluation).

The libraries are just a matter of usage.  The things you use all the time, you soon learn all the functions and their parameters.  The stuff you seldom use, you'll need to look up. 

That's the way it is in any technical field - the common stuff you soon memorize, but you need references for the seldom used stuff (just like we use dictionaries for spelling/meaning).

Quote
Are there any good books / references / how-tos available on the web gratis?

I gave a few in my original post.  You can back up to find it, or, if you want, I'll come up with a more comprehensive list and post it here.  Just let me know.

While we're on the subject of books, I get a lot of them from my local library - they always have a couple of carts by the door with your choice for a dollar - and since programming or computer books are not that popular, I usually find one or two that I would like to have.  A buck a piece is a darned good price!  Might pay you to check it out.

Retired Senior Chief, Retired Software Engineer, Active GrandPa

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2250
Re: Coding and Scripting
« Reply #84 on: December 04, 2009, 10:01:23 AM »
Just want to point out (Chomp kinda glossed over it) that every C or C++ program must have a "main" function that is the actual program itself.  You will get an error if there is no main. 

However, there can be many other functions, but execution always starts at the beginning of the "main" routine.

It is customary, although not required, for "main" to follow (that is, appear after) any other functions defined in the file.

Retired Senior Chief, Retired Software Engineer, Active GrandPa

Offline Joble

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6804
  • USA - Mountain Time
Re: Coding and Scripting
« Reply #85 on: December 04, 2009, 12:59:21 PM »
For those who already know how to code and just need a project, here's one idea:
http://www.pclinuxos.com/forum/index.php/topic,65877.0.html
Needs to be modified to work with the current ffmpeg, if any of you want to take it on.  I wouldn't dare but it could make for some good discussion.
Search First.
Forum Rules
Hero means I talk a lot, nothing more, nothing less!
Have an Awesome Day!
Healthy System

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2250
Re: Coding and Scripting
« Reply #86 on: December 05, 2009, 09:49:09 AM »
Thanks, Chomp.

Did you know that for blocks of comments you can do this instead?

/*
Here is a bunch of comments
Here is a bunch of comments
Here is a bunch of comments
*/

This is a little easier to read than '//' in front of every line.


Yeah, but it's a lot harder to maintain!  And much easier to break something!  In a page and a half of comment, you're easily tempted to mistake some of it for code and slap another statement in the middle - which promptly gets ignored...

Retired Senior Chief, Retired Software Engineer, Active GrandPa