Lesson 1: Introduce to C previous post
Lesson 2: If statements in C
Here are the relational operators, as they are known, along with examples:
> greater than 5 > 4 is TRUE
< less than 4 < 5 is TRUE
>= greater than or equal 4 >= 4 is TRUE
<= less than or equal 3 <= 4 is TRUE
== equal to 5 == 5 is TRUE
!= not equal to 5 != 4 is TRUE
Basic If Syntax
if ( statement is TRUE )
Execute this line of code
if ( 5 < 10 )
printf( "Five is now less than ten, that's a big surprise" );
For example:
if ( TRUE ) {
/* between the braces is the body of the if statement */
Execute all statements inside the body
}
Else
It can look like this:
if ( TRUE ) {
/* Execute these statements if TRUE */
}
else {
/* Execute these statements if FALSE */
}
Else if
Let’s look at a simple program for you to try out on your own.
#include <stdio.h>
int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */
printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are oldn" );
}
else {
printf( "You are really oldn" ); /* Executed if no other statement is
*/
}
return 0;
}
A. !( 1 || 0 ) ANSWER: 0
B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)
C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)
1. Which of the following is true?
A. 1
B. 66 C. .1 D. -1 E. All of the above 2. Which of the following is the boolean operator for logical-and? A. &
B. && C. | D. |& 3. Evaluate !(1 && !(0 || 1)). A. True
B. False C. Unevaluatable 4. Which of the following shows the correct syntax for an if statement? A. if expression
B. if { expression C. if ( expression ) D. expression if |
Lesson 3: Loops
FOR – for loops are the most useful type. The syntax for a for loop is
for ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}
Example:
#include <stdio.h>
int main()
{
int x;
/* The loop goes while x < 10, and x increases by one every loop*/
for ( x = 0; x < 10; x++ ) {
/* Keep in mind that the loop condition checks
the conditional statement before it loops again.
consequently, when x equals 10 the loop breaks.
x is updated before the condition is checked. */
printf( "%dn", x );
}
getchar();
}
WHILE – WHILE loops are very simple. The basic structure is
while ( condition ) { Code to execute while the condition is true } The true represents a boolean expression which could be x == 1 or while ( x != 7 ) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x ==5 || v == 7) which says execute the code while x equals five or while v equals 7. Notice that a while loop is like a stripped-down version of a for loop– it has no initialization or update section. However, an empty condition is not legal for a while loop as it is with a for loop.
Example:
#include <stdio.h>
int main()
{
int x = 0; /* Don't forget to declare variables */
while ( x < 10 ) { /* While x is less than 10 */
printf( "%dn", x );
x++; /* Update x so the condition can be met eventually */
}
getchar();
}
do {
} while ( condition );
Example:
#include <stdio.h>
int main()
{
int x;
x = 0;
do {
/* "Hello, world!" is printed at least one time
even though the condition is false */
printf( "Hello, world!n" );
} while ( x != 0 );
getchar();
}
Break and Continue
while (true)
{
take_turn(player1);
take_turn(player2);
}
while(true)
{
if (someone_has_won() || someone_wants_to_quit() == TRUE)
{break;}
take_turn(player1);
if (someone_has_won() || someone_wants_to_quit() == TRUE)
{break;}
take_turn(player2);
}
for (player = 1; someone_has_won == FALSE; player++)
{
if (player > total_number_of_players)
{player = 1;}
if (is_bankrupt(player))
{continue;}
take_turn(player);
}
C Programming Quiz Solutions: Loops
1. What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?
A. 10
B. 9 C. 0 D. 1 Note: This quiz question probably generates more email to the webmaster than any other single item on the site. Yes, the answer really is 10. If you don’t understand why, think about it this way: what condition has to be true for the loop to stop running? 2. When does the code block following while(x<100) execute? A. When x is less than one hundred
B. When x is greater than one hundred C. When x is equal to one hundred D. While it wishes 3. Which is not a loop structure? A. For
B. Do while C. While D. Repeat Until 4. How many times is a do while loop guaranteed to loop? A. 0
B. Infinitely C. 1 D. Variable |
For example:
#include <stdlib.h> /* Include rand() */
int a = rand(); /* rand is a standard function that all compilers have */
The general format for a prototype is simple:
return-type function_name ( arg_type arg1, ..., arg_type argN );
Functions that do not return values have a return type of void. Let’s look at a function prototype:
int mult ( int x, int y );
Let’s look at an example program:
#include <stdio.h>
int mult ( int x, int y );
int main()
{
int x;
int y;
printf( "Please input two numbers to be multiplied: " );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The product of your two numbers is %dn", mult( x, y ) );
getchar();
}
int mult (int x, int y)
{
return x * y;
}
printf( "The product of your two numbers is %dn", x * y );
Quiz: Functions
1. Which is not a proper prototype?
A. int funct(char x, char y);
B. double funct(char x) C. void funct(); D. char x(); 2. What is the return type of the function with prototype: “int func(char x, float v, double t);” A. char
B. int C. float D. double 3. Which of the following is a valid function call (assuming the function exists)? A. funct;
B. funct x, y; C. funct(); D. int funct(); 4. Which of the following is a complete function? A. int funct();
B. int funct(int x) {return x=x+1;} C. void funct(int) { printf( “Hello”); D. void funct(x) { printf( “Hello”); } |
switch ( <variable> ) {
case this-value:
Code to execute if <variable> == this-value
break;
case that-value:
Code to execute if <variable> == that-value
break;
...
default:
Code to execute if <variable> does not equal the value following any of the cases
break;
}
The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from falling through and executing the code in all the other case statements. An important thing to note about the switch statement is that the case values may only be constant integral expressions.
int a = 10;
int b = 10;
int c = 20;
switch ( a ) {
case b:
/* Code */
break;
case c:
/* Code */
break;
default:
/* Code */
break;
}
Below is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch in a program.
#include <stdio.h>
void playgame()
{
printf( "Play game called" );
}
void loadgame()
{
printf( "Load game called" );
}
void playmultiplayer()
{
printf( "Play multiplayer game called" );
}
int main()
{
int input;
printf( "1. Play gamen" );
printf( "2. Load gamen" );
printf( "3. Play multiplayern" );
printf( "4. Exitn" );
printf( "Selection: " );
scanf( "%d", &input );
switch ( input ) {
case 1: /* Note the colon, not a semicolon */
playgame();
break;
case 2:
loadgame();
break;
case 3:
playmultiplayer();
break;
case 4:
printf( "Thanks for playing!n" );
break;
default:
printf( "Bad input, quitting!n" );
break;
}
getchar();
}
Quiz 5: Switch case Solutions
1. Which follows the case statement?
A. :
B. ; C. – D. A newline 2. What is required to avoid falling through from one case to the next? A. end;
B. break; C. Stop; D. A semicolon. 3. What keyword covers unhandled possibilities? A. all
B. contingency C. default D. other 4. What is the result of the following code? int x=0; switch(x) { case 2: printf( "Hello World" );
} A. One
B. Zero C. Hello World D. ZeroHello World |