For this blog we will go over conditional statements, if, if-else, and nested loops.
The first one will be a simple if statement:
A simple comparison of two variables will print out. The format to create this if statement is extremely similar to that of Java.
So we will use this structure:
if (evaluation)
{
statement;
}
Notice that there are still parenthesis and brackets for the if statements in C. Also, the operators are as follows:
< (less than),
> (greater than),
== (equal to),
!= (not equal to),
<= (less than or equal to),
>= (greater than or equal to.
The next picture will have an example of an if-else statement. It is the same code as before but we add else {statement} after the brackets of the if-statement.
Now we can add another conditional to have an if- else_if - else:
Another type of condition is called a case-switch.
A value is entered into a case and depending on what it is a different output is selected, or if there is not a case for that value then there is a default. The structure is as follows:
switch(expression)
{
case value1:
statement(s);
break;
case value2:
statement(s);
break;
case value3:
statement(s);
break;
default:
statement(s);
}
Also this program shown will use a function that will scan in user output. All that is needed to be done to use it is to code: scanf( "", %variable).
On looking back much of this syntax is basically the same as that of Java in order to perform conditional statements.
No comments:
Post a Comment