if(CONDITIONAL)
x++;
if(CONDITIONAL)
x++;
else
x--;
if(CONDITIONAL) {
x++; printf("Why is the midterm hand written?");}
else
x--;
int x;
scanf("Gimme an x: %d", &x);
switch(x%2) { case 0: printf("x is even.");
case 1: printf("x is odd.");
default: printf("I don't know what that is but I don't like it.");
}
// Same as the above, using if-else structure
if(x%2 == 0)
printf("x is even.");
else if(x%2 == 1)
printf("x is odd.");
else
printf("I don't know what that is but I don't like it.'"
100% interchangeable - anything one can do, the other can dofor(THINGS_TO_DO_ONCE; CONDITIONAL; THINGS_TO_DO_EVERY_LOOP)
while(CONDITIONAL)
int myArray[10]; // Declares an array of 10 integers
myArray[0] = 5; // Sets the FIRST element of the array to 5
myArray[1] = 7; // Sets the SECOND element of the array to 7
// Print out all the values in myArray
for(int i = 0; i < 10; i++)
printf("%d ", myArray[i]); // prints out all the elements of the array
// Short way to scan values into the array
i = 0;
while(scanf("%d", &myArray[i++]) > 0);
// This works too
for(i = 0; i < 10; i++)
scanf("%d", &myArray[i]);
int i1 = 5, i2 = 10; double d1 = 5.0, d2 = 10.0, d3; d3 = i1/i2;
printf("line 1: %d\n, i1/i2); printf("line 2: %.1lf\n", d1/d2); printf("line 3: %.1lf\n", i1/d2); printf("line 4: %d\n", (int)d1/i2); printf("d3 = %.1lf\n", d3);
line 1: 0
line 2: 0.5
line 3: 0.5
line 4: 0
d3 = 0.0
int x = 0, y = 0, i = 1, j = 1;
for (i = 0; i < 4; i++)
{
x = i;
for (j = 0; j < 10; j++)
y +=x;
}
printf("x = %d y = %d i = %d j = %d", x, y, i, j);
x = 3 y = 60 i = 4 j = 10
int x = 2, y = 1;
switch(x+y)
{
case 1: x+=1;
case 2: x+=2; break;
case 3: x+=3;
case 4: x+=4; break;
default: x+=5;
}
printf("x = %d\n", x);
x = 9
int i, x = 0;
for (i = 1; i < 6; i++)
{
if (i%2 == 0) continue;
if (i == 4) break;
x++;
}
printf("i = %d x = %d\n", i, x);
i = 6 x = 3
double x = 5.2, y = 3.8, z = -5.2, w = -3.8;
int x2 = (int) (x + ( x < 0 ? -0.5 : 0.5) );
int y2 = (int) (y + ( y < 0 ? -0.5 : 0.5) );
int z2 = (int) (z + ( z < 0 ? -0.5 : 0.5) );
int w2 = (int) (w + ( w < 0 ? -0.5 : 0.5) );
printf("x2 = %d y2 = %d z2 = %d w2 = %d", x2, y2, z2, w2);
x2 = 5 y2 = 4 z2 = -5 w2 = -4
int x = 0, y = -1, z = 1;
x-- || ++y && z++;
printf("x = %d y = %d z = %d\n", x, y, z)
x = -1 y = 0 z = 1
int x, y, z;
x = y = 1;
z = 0;
x-- || y-- && z++ ;
printf(“x = %d y = %d z = %d\n”, x, y, z);
x = 0 y = 1 z = 0
#include <stdio.h>
int main()
{
int i, j;
for (i = 3; i > 0; i--)
{
for (j = 0; j < i; j++)
printf("*");
printf("\n");
}
return 0;
}
***
**
*
#include <stdio.h> int main() { int i, j;
i = 3; while(i > 0) {
j = 0; while(j < i)
{ printf("*");
j++;
} printf("\n");
i--; } return 0; }
|*****|
|**** |
|*** |
|** |
|* |
for(int i = 5, j; i > 0; --i)
{
printf("|");
for(j = 0; j < 5; j++)
printf("%c", j < i ? '*' : ' ');
printf("|\n");
}