
Welcome to coding.
Control structures allow a program to repeat segments of code and make decisions. C/++ has a
set of these built into it so there's no need to #include anything.
Note: '=' is used for assigning values and '==' is a comparison operator that will return true if the variables on either side of it are equal.
Comparison (aka Relational and Equality) Operators
== is equal to
!= does not equal
< less than
<= less than or equal to
> greater than
>= greater than or equal to
Also
++ Increase variable by one
-- Decrease variable by one
'If' and 'Else' Statements
if (x == y){
printf ("teh if statement worked!\n");
} else {
printf ("x != y\n");
}
Code within an 'if' statement's block (between the following set of { } ) will only run if the condition returns true. In this case, the condition is 'x==y'. If this condition proves to be untrue, the code will not run.
If there is an 'else' statement following IMMEDIATELY after the 'if' block, the code within the 'else' block will run. 'else' statements do not require any conditions apart from the failure of a preceding 'if'.
For our above example, if, say, x = 5 and y = 5 then the above code would print "teh if statement worked". Otherwise, it would print "x != y".
There is also an "else if". This is similar to adding multiple if statements, however only one out of the group will ever run.
if(x == y){
printf("x == y!\n");
} else if (x == y+1) {
printf("x == y+1!\n");
} else {
printf("x is neither y nor y+1\n");
}
Note: If there's only one statement within an if or else block, you don't need curly brackets ( { } ).
'While' Loops
while ( flag == 0 ){
do stuff...;
if (something) flag = 1;
}
int i = 0, n = 9001;
while(i < n){
// do stuff
printf("%d\n", i);
i++;
}
Just like our 'if' statement, 'while' has a condition that must be filled in order to execute. As the name suggests, it will loop for as long as the condition remains true. "While" loops are especially useful when you want a section of your code to keep running until a certain event occurs. However, if you are looking to make a loop that will run a set number of times then a 'for' loop is most likely what you're looking for.
'For' Loops
for (int i = 0; i < 10; i++){
printf("%d\n", i);
}
A for loop allows you to declare a temporary variable that can be used as a counter. It will only exist within the loop - i.e. the variable you created goes out of scope once the 'for' loop ends. A 'for' loop consists of the following:
for (declaration of variable; conditions; increase/decrease) { code }
In this case, we're making a variable named i which will start at 0. The loop will run for as long as i is less than 10. i will increase by one every time the loop has run its course.
That particular for loop will print the following "0 1 2 3 4 5 6 7 8 9". This is because i is increased at the conclusion of the for loop to account for the way pointers function.
Note that the above 'for' loop is similar to the second 'while' loop - they would both do the exact same thing, but at the conclusion of the 'for' loop i wouldn't exist, whereas after the final } of the 'while' loop, i would still work fine.
James.
As I've mentioned numerous times before, it will get a lot more interesting as the year progresses. Also, the first programming competition that you will be participating in will take place somewhere between the 12th and 17th of August. My advice? Get training.
http://orac.amt.edu.au/jaz/
No comments:
Post a Comment