--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Fri Aug 17, 2007 3:08 am Post subject: Coding with C - Part 2 |
|
|
| Pillboi wrote: | | Remember: Programming with C - Part 1? It was written "By silenthacker aka nigtronix on CE forums". It just so happens, I was searching around on hts, HackThisSite, and found the second part, written by silenthacker. He had forgotten to post it here! I know it is full with grammatical errors, but his work, not mine! So without further ado. Here it is, what you've all been waiting for! |
---------------------------------------------
| Code: | Coding With C : By silenthacker aka Nigtronix from CEF.
Part #: 2
Copyright 2004
www.silenthacker.tk
Email: [email protected]
---------------------------------------------
You can use this Tutorial Freely, As long as you keep the above Header Unchanged
Note: Disgard the <b> and </b> in this tutorial, It was originally written for HTS with HTML |
------------------------------------------------
This is the Second Part of my C programming series, if you read my first part - http://www.hackthissite.org/articles/read/24/
Then you should have a basic feel of the C programming language. Today I well expand on that knowledge, In this version we well talk about:
<b>Comparison Operators
Logic Operators
If/Else statements
Switch Statement and the break; statement
For, While, and Do While Loops
Basic Functions</b>
So this version is basicly dedicated to <b> program flow </b> which is the execution of statements from start to finish. Normally your programs execute statements in sequence, that is from start to finish, but with conditional statements and selection structures, we can have our programs make decision's and take a path of execution based on the result!
COMPARISON operators, also known as RELATIONAL. Are used to compare numerical values.
---------------------------------------------------
<b> COMPARISON Operators</b>
There are six basic comparison operators in C:
Less than: <
Less than or equal to: <=
Greater than: >
Greater than or equal to: >=
Equal to: ==
Not Equal to: !=
I think I dont need to explain each one's Function, See the Examples below.
Conditions are expressions that can return one of two values, 1 (true) or 0 (false). Also any non zero number is considered TRUE also in C. But 0 is always FALSE.
<b>Examples of Comparison Operators:</b>
1 < 4 returns True(1)
1 <= 1 returns True(1)
1 > 3 returns False(0)
1 >= 2 returns False(0)
1 == 1 returns True(1)
2 != 9 returns True(1)
A note on the, Equal to operator(==), Dont confuse it with the assignment operator(=)!!!
---------------------------------------------------
<b> Logical Operators </b>
Sometimes in programming you might need to ask more than one relational question. Logic Operators let you do just that, they combine two or more relational expressions to return a true or false value.
<b> C's Three Logical Operators </b>
AND: &&
OR: ||
NOT: !
<b>For the AND (&&) logical Operator</b>, both relational statements it combines MUST be true, for it to return true, EXAMPLE:
(1 < 2) && (2 > 1) returns TRUE(1)
(5 < 1) && (1 < 5) returns FALSE(0), Even if (1 < 5) is TRUE!!!, It still returns FALSE because both sides MUST be true to return a TRUE value!
----------------------------------------------------
<b>For the OR (||) logical Operator</b>, It only returns TRUE(1) if one side is true or both sides are true. And only returns FALSE(0) If both sides are false. Examples:
(2 < 5) || (2 > 1) returns TRUE(1) cause (2 < 5) is true
(2 < 1) || ( 3 > 5) returns FALSE(0) cause no statement is true
----------------------------------------------------
The NOT(!) Operator reverses the TRUE or FALSE Value of a conditional statement, and can only have one input(Operand) Example:
!1 returns 0
!0 returns 1
----------------------------------------------------
<b>The If/Else Structure</b>
Relational operators are used mainly to construct relational expressions used in if and else statements. In this section we well see how we can test conditions using IF and ELSE statements.
Statements in a C program normally execute
from top to bottom (In sequence) A program control statement is a statement that alters the execution of your program, The IF statement is one of many program control statements in C.
The if statement evaluates an expression and directs program execution depending on the resultof that evaluation. Example of the IF statement:
if (expression)
statement;
If the expression evaluates to true, the statement is executed. Otherwise if it evaluates to false, the statement(s) are not executed.
When the IF statement is executed, the statements inside it are executed, then program execution returns after the if statement in sequence order. The statements of a IF statement are usually inside a block with the { and } brackets. Example
if (expression)
{
statement(s);
}
real example:
int x;
x=10;
if(x < 20)
{
printf("I got executed!, cause 10 is less than 20!\n");
}
<b>ELSE Statement</b>
An If statement can optionally include an else statement.The main IF statement is first evaluated, If it is true, the code in the if statement is executed, if it is false, it executes the code in the ELSE statement. Example:
int hours,raise; //Declare two variables of type int
hours=45;
if( hours >= 45 )
{
raise = 20;
else
raise = 0;
}
The main if statement code is executed, and the ELSE is left ignored, cause the if statement returned TRUE.
------------------------------------------------
<b>The SWITCH statement</b>
Now, IF statements are nice, but you may find yourself writing Huge blocks of confusing if/else statemnts. The Switch statement is used to simplify it a bit.
The General setup of the SWITCH statement is as follows:
switch (expression) {
case expression1:
//Statements to execute
case expression2:
//statements to execute
default:
//Statements to execute if all above case statements fail
}
A REAL EXAMPLE PROGRAM WITH SWITCH STATEMENT:
include <stdio.h>
int main() {
int number1;
printf("Type a number between 5 and 10\n");
scanf("%d", &number1);
switch (number1) {
case 5:
printf("You chose number 5\n");
break;
case 6:
printf("You chose number 6\n");
break;
case 7:
printf("You chose number 7\n");
break;
case 8:
printf("You chose number 8\n");
break;
case 9:
printf("You chose number 9\n");
break;
case 10:
printf("You chose number 10\n");
break;
default:
printf("Thats not a number between 5 and 10!\n");
break;
}
return 0;
}
What the hell is that break; doing in there?!?!?!
The break; statements Break out of the switch case statement block, so it wont execute the case and default statement below it. Cause normally it well run through all the case statements and the default statement until the end } is reached!
Note: One draw back to the switch statement is, that you cant use logical operators in the case statement expressions. Thats why I kinda like IF/ELSE better
----------------------------------------------------
<b> Loop's</b>
Now This has been said many times before, Regular execution in a program is from top to bottom(sequence). Looping is known as the repition structure, cause it usually repeats execution of a block of statements more than once.
<b> The For Loop </b>
The for loop is, in my opinion, used most often than any other loop, I rarely use the Do While loop, but I use the While loop ocasionally also.
The basic setup of the For loop is:
for ( initial; condition; increment )
{
//statements
}
The initial statement is usually used to assign a variable to a certain value before it is used in the loop.
The condition statement tests a condition and if TRUE the FOR statement starts executing and looping, otherwise if FALSE, the FOR statement terminates.
The increment is used to increase, decrease the variable condition being tested, each time it loops, after then the condition statement is evaluated and the process repeats until the condition becomes FALSE and the for statement terminates.
If the conditon remains true forever, then it is said to have a Infinate loop, and it well just suck processor and memory cycles and never terminate, and usually crash the program.
Example of a For loop in action:
//prints out numbers 1 to 10
int x;
for(x=1;x<=10;x++)
{
printf("%d",x);
}
This loops as long as x is less than or equal to 10, when it reaches 10, the for statement terminates.
One thing I forgot to tell you about is x++:
the ++ is called the increment operator, what it basicly does is add's 1 to the current value of x, so its the eqivalent of writing x = x + 1
same goes for the --, which is the decrement operator that subtracts 1 from the current value of x.
NOTE: X is a varible and u can use any variable with ++ and --
<b> The WHILE loop</b>
The basic setup for the while loop is:
while (expression) {
/* statements */
// INCREMENT STATEMENT, (x++ or x--) or anything u want to change the variable by each time
}
Now the while loop is simpler to use cause it only requires one expression, but I like the for loop cause its compact all in one line.
Example of While loop use:
int x;
x=10;
//Count down from 10 to 1
while (x >= 0) {
printf("%d",x);
x--;
}
NOTE: Always remember the INCREMENT statement, otherwise u can end up with infinate loops
------------------------------------------
<b>The Do-While Loop</b>
The do-while loop tests the condition at the end, Ensuring that the code in it is executed at least once.
The basic setup for the do-while loop is:
do {
//Statements to be looped
//INCREMENT STATEMENT
} while (expression);
Example:
int x = 1; //Declare variable x as type int, with a value of 1
//Count from 1 to 10
do {
printf("%d", x);
x++;
} while (x<10);
Note: Dont forget the ; after the while statement in a do-while loop!
Another thing to Acknowledge, is that you can put the break; statement in a loop to exit it at any time, (usually combined with a if statement) to test the value when looping to exit.
--------------------------------------
<b>Functions</b>
A Function is basicly a block of C code that only executes when you call it, and can have values passed to it, and can also optionally return a value.
Some basic things about functions are:
A Function preforms a specfic task
A Function must have a unique name
A Function is independent from the rest of the program
A function does not have to return a value (Called a void function)
<b>Function Prototype</b>
The function prototype is a model for a function that will appear later in the
program. A function's prototype contains the name of the function, a list of variables that must be passed to it, and the type of variable it returns, if not a void function.
The variables that are passed to the function are called arguments, and they are enclosed in parentheses following the function's name. Example:
Funct_Name(int argument1)
argument1 is a variable that well be passed to the function.....
The function prototype is declated above the int main() function.
Function Prototype declarsion:
return_type function_name( arg-type name-1,...,arg-type name-n);
Example:
#include <stdio.h>
int addition(int number1, int number2);
int main()
{
return 0;
}
Now the Prototype tells the compiler about the function that is gonna be used later in our program, It tells the name, return type, and arguments(notice there is no ;'s in the brackets of a function). And that the function prototype itself is treated as a regular C statement so it has a ; after it.
<b>The Function Definition</b>
The function definition is the actual function. IT contains the code that will be executed. The first line of a function definition is called the function header and it is identical to the function prototype,with the exception of the semicolon.
<b> Calling a Function </b>
To call a function you simply use the function name and pass arguments to it: example
#include <stdio.h>
int func_addition(int number1, int number2); // Our Function prototype
int main()
{
int result,usernum1,usernum2;
puts("Enter In number one\n");
scanf("%d",&usernum1);
puts("Enter In Number two\n");
scanf("%d",&usernum2);
//Call func_addition and pass usernum1 and usernum2 to it
result=func_addition(usernum1,usernum2);
printf("The Result of %d plus %d equals: %d",usernum1,usernum2,result);
return 0;
}
int func_addition(int number1, int number2) // Function Definition
{
int temp_add; //variable to hold the value after we add them
temp_add = number1 + number2;
return temp_add; //return the value of temp_add
}
Now this may be a little confusing about calling the function and passing the variables to it:
First notice the func_addition prototype:
int func_addition(int number1, int number2);
and notice the way we called it:
result=func_addition(usernum1,usernum2);
when u pass usernum1 to func_addition when we call it, the value of usernum1 is stored in number1, and the value of usernum2 is stored in number2, Notice that they go in order and are seperated by ",", and align with each other:
int func_addition(int number1, int number2);
| |
result=func_addition(usernum1,usernum2);
-------------------------------------------------
<b>Returning the value</b>
notice: return temp_add, when Func_addition is finished it passed the temp_add value to the variable result that called it.
functions that dont return anything are known as void:
void func_name() // notice no arg's between the ()
-------------------------------------------------
Errors,suggestions,comments, send them to [email protected], or reply to this article below:
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|