// Why do you need if/else statements?
int main()
{
int numbers[] = {1, 2, 3, 4, 5, 6}; // and so on
int i;
int sum = 0;
for(i = 0; i < sizeof (numbers)/sizeof(int); i++)
sum += i;
return sum;
}
Chat with our AI personalities
#include
{
inti = 1;
int inumber = 0;
int itotal = 0;
do
{
printf("Type in a number greater than zeroor zero to end program\n");
scanf("%d", &inumber);
printf("The number you typed was %d\n", inumber);
if (inumber 1);
printf("You ended the program, hit any key to continue\n");
getch();
}
A sequential if-then-else pattern is a pattern where the program checks on thing at a time using if statements. For example, in C: if (condition 1) { do something 1; } else if (condition 2) { do something 2; } else if (condition 3) { do something 3; } else { do something else; } The first condition that is true will be executed.
Control statements are the statements that control the flow of program execution. For eg: loops: For, While, Do-While, decision making using if-then-else or switch-case and there's goto to transfer control.
if (has_enough_money) { pay_the_bill(); }
They are 'statements' to speak strictly, they are: , , if, else, while, for, do, return, switch, break, continue, gotoNote: is zero or more statements between '{' and '}'
if (condition) statement1 [else statement2] example: if (i==j); else if (j==k) printf ("i!=j, j==k\n); else printf ("i!=j, j!=k\n); here statement1 is an empty-statement, statement2 is another if-statement There are three forms of statements IF-THEN IF-THEN-ELSE IF-THEN-ELSIF Sequence of statements is executed only if the condition evaluates to TRUE If condition evaluates to FALSE or NULL, it does nothing In either case control passes to next statement after the IF-THEN structure IF THEN statements; END IF; Sequence of statements in the ELSE clause is executed only if the condition evaluates to FALSE or NULL IF THEN statements; ELSE statements; END IF;