C has features that allow the control to go to different instructions instead of the pure sequential steps. There are "if" statements that can accomplish this. There are also loop statements that allow us to repeat a group of statements such as "while" and "for" statements.
If Statement
The if statement tests a condition and executes statements if the condition is true/
File: "if1.c"
#include <stdio.h>
int main()
{
int x1 ;
x1 = 4 ;
if ( x1 % 2 == 0 )
printf( "The number is even." ) ;
return 0;
}
Output:
The number is even.
The operator "==" checks to see if 2 values are equal. Other comparison operators are :
!= Not equal
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
File: "f2.c"
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:" ) ;
scanf( "%d" , &x1 ) ;
if ( x1 % 2 == 0 )
printf( "The number is even.\n" ) ;
if ( x1 % 2 != 0 )
printf( "The number is odd.\n" ) ;
if ( x1 <= 100 )
printf( "The number is less than or equal to 100.\n" ) ;
if ( x1 >= 100 )
printf( "The number is greater or equal to 100.\n" ) ;
return 0;
}
Output:
Enter a number:100
The number is even.
The number is less than or equal to 100.
The number is greater or equal to 100.
Enter a number:4
The number is even.
The number is less than or equal to 100.
Normally an expression such as "x1 == 4" will evaluate to "true" or "false" in most languages. However C does not really have a boolean type and evaluates the comparison operator as an integer. It treats "true" as 1 and "false" as 0.
File: "if3.c"
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:" ) ;
scanf( "%d" , &x1 ) ;
printf( "Result of %d.\n" , x1 == 4 ) ;
return 0;
}
Output:
Enter a number:4
Result of 1.
Enter a number:5
Result of 0.
The "if" condition can also work with a single number as shown in the below program. However this is not a good programming practice. C interprets the non zero number as true and zero as false. It is better to use the comparison operator or constants so that it is clear and does not create confusion .
File: "if3a.c"
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:" ) ;
scanf( "%d" , &x1 ) ;
if ( x1 )
printf( "Condtion is true." ) ;
else
printf( "Condtion is false." ) ;
return 0;
}
Enter a number:0
Condtion is false.
Enter a number:4
Condtion is true.
File: "if4.c"
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool x1 = false;
if( x1 )
{
printf("x is true.\n");
}
printf( "Int value of true: %d\n" , true ) ;
printf( "Int value of false: %d\n" , false ) ;
}
Output:
x is false.
Int value of true: 1
Int value of false: 0
We have to include "#include <stdbool.h>" and then use the data type "bool" . This was introduced with later versions of "C" . Initial versions of "C" did not have this facility. We can also come up with our own schemes if we want to use boolean data type in our program.
File: "if5.c"
#include <stdio.h>
#define TRUE 1
#define FALSE 0
typedef int boolean ;
int main()
{
int x1 ;
boolean boolVariable1 ;
printf( "Enter a number:" ) ;
scanf( "%d" , &x1 ) ;
if( (x1==4) == TRUE )
{
printf("x is true.\n");
}
boolVariable1 = FALSE ;
if ( !boolVariable1 )
printf("x is true.\n");
}
Output:
Enter a number:4
x is true.
x is true.
The "typedef" states that we can use "boolean" as a type and that type is the same as "int" .
We have seen a new operator "!" getting used . This is the logical operator and inverts the value of the boolean condition . The logical operators are:
&& And condition. If both the operands are true then the and condition is true.
|| Or condition . If any of the operands is true then the condition is true
! Not operator. If the condition is true then it is made false otherwise it is made true .
File: "log1.c"
#include <stdio.h>
#define TRUE 1
#define FALSE 0
typedef int boolean ;
int main()
{
int x1 = 4 ;
int y1 = 5 ;
if( x1 == 4 && y1 == 5 )
{
printf("First Condition is true .\n");
}
if( x1 == 14 || y1 == 5 )
{
printf("Second Condition is true .\n");
}
if( !( x1 == 14) )
{
printf("Third Condition is true .\n");
}
}
Output:
First Condition is true .
Second Condition is true .
Third Condition is true .
We can also associate an "else" with an "if" statement and as it's name implies it executes when the if condition is false.
File: "if6.c"
#include <stdio.h>
#define TRUE 1
#define FALSE 0
typedef int boolean ;
int main()
{
int x1 ;
boolean boolVariable1 ;
printf( "Enter a number:" ) ;
scanf( "%d" , &x1 ) ;
if( x1 <= 10 )
{
printf("x is less than 10.\n") ;
}
else
{
printf("x is greater than 10.\n") ;
}
}
Output:
Enter a number:3
x is less than 10.
Enter a number:14
x is greater than 10.
The third type of "if" statement is an "if else if" statement and can be used to handle multiple conditions.
File: "if7.c"
#include <stdio.h>
int main()
{
int testScore ;
printf( "Enter a number:" ) ;
scanf( "%d" , &testScore ) ;
if (testScore < 60)
cout << "Your grade is F.\n";
else if (testScore < 70)
cout << "Your grade is D.\n";
else if (testScore < 80)
cout << "Your grade is C.\n";
else if (testScore < 90)
cout << "Your grade is B.\n";
else if (testScore <= 100)
cout << "Your grade is A.\n";
return 0;
}
Output:
Enter a number:95
Your grade is A.
We can also have an "else" at the bottom that is executed when none of the conditions match.
File: "if8.c"
#include <stdio.h>
int main()
{
int testScore ;
printf( "Enter a number:" ) ;
scanf( "%d" , &testScore ) ;
if (testScore < 60)
printf("Your grade is F.\n" ) ;
else if (testScore < 70)
printf("Your grade is D.\n" ) ;
else if (testScore < 80)
printf("Your grade is C.\n" ) ;
else if (testScore < 90)
printf("Your grade is B.\n" ) ;
else if (testScore <= 100)
printf("Your grade is A.\n" ) ;
else
printf("Invalid Entry.\n" ) ;
return 0;
}
Output:
Enter a number:120
Invalid Entry.
We can only place the else at the bottom and not in the middle.
#include <stdio.h>
int main()
{
int testScore ;
printf( "Enter a number:" ) ;
scanf( "%d" , &testScore ) ;
if (testScore < 60)
printf("Your grade is F.\n" ) ;
else if (testScore < 70)
printf("Your grade is D.\n" ) ;
else
printf("Invalid Entry.\n" ) ;
else if (testScore < 80)
printf("Your grade is C.\n" ) ;
else if (testScore < 90)
printf("Your grade is B.\n" ) ;
else if (testScore <= 100)
printf("Your grade is A.\n" ) ;
return 0;
}
Output:
if9.c: In function ‘main’:
if9.c:18:2: error: ‘else’ without a previous ‘if’
18 | else if (testScore < 80)
The "if" condition can have multiple statements if they are enclosed by a block with curly braces.
File: "if10.c"
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:" ) ;
scanf( "%d" , &x1 ) ;
if( x1 <= 10 )
{
printf("About to print something.\n") ;
printf("x is less than 10.\n") ;
}
else
{
printf("x is greater than 10.\n") ;
}
if ( x1 <= 10 )
if ( x1 == 4 )
printf( "x1 is 4\n" ) ;
else
printf( "x1 is greater than 10\n" ) ;
}
Output:
Enter a number:5
About to print something.
x is less than 10.
x1 is greater than 10
The second "if" block is misleading. We indented the "else" to match with if ( x1 <= 10 ) but C doesn't really care about indentation. It goes by the logic and associates the else with "if ( x1 ==4 )" . When we give the input for x1 as 5 then it passes "if ( x1 <= 10 )" and then the if statement "if ( x1 == 4 )" is checked. That of course is not true and the else part is executed. The right indentation makes the code clearer.
if ( x1 <= 10 )
if ( x1 == 4 )
printf( "x1 is 4\n" ) ;
else
printf( "x1 is greater than 10\n" )
The above is an example of nested "if" statement where one "if" statement can contain another. If we did want the "else" to be associated with the outer "if" then we have to use curly brackets.
File: "if11.c"
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:" ) ;
scanf( "%d" , &x1 ) ;
if( x1 <= 10 )
{
printf("About to print something.\n") ;
printf("x is less than 10.\n") ;
}
else
{
printf("x is greater than 10.\n") ;
}
if ( x1 <= 10 )
{
if ( x1 == 4 )
printf( "x1 is 4\n" ) ;
}
else
printf( "x1 is greater than 10\n" ) ;
}
The ternary statement is a shorter form for the if else statement.It is written as:
condition ? statement if condition is true : statement if condition is false
File: "ter1.c"
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
if ( (x1 % 2) == 0 )
printf( "The number is even.\n" ) ;
else
printf( "The number is odd.\n" ) ;
//Writing the above as ternary expression
printf( "The number is %s\n" , ( x1 % 2) == 0 ? "even." : "odd." ) ;
return 0;
}
Output:
Enter a number:4
The number is even.
The number is even.
The switch statement is similar in functionality to the multi if else statements and can can execute statements based on integer values.
The syntax is:
switch( expression )
{
case const-expr: statements
case const-expr: statements
default: statements
}
File: "switch1.c"
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
int remainder = x1 % 5 ;
switch( remainder )
{
case 0:
printf( "Remainder is 0 ." ) ;
break ;
case 1:
printf( "Remainder is 1 ." ) ;
break ;
default:
printf( "Remainder is something else ." ) ;
}
return 0;
}
Output:
Enter a number:5
Remainder is 0 .
Enter a number:3
Remainder is something else .
If a match is found then we need to break out of the switch statement otherwise the rest of the statements are executed.
File: "switch2.c"
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
int remainder = x1 % 5 ;
switch( remainder )
{
case 0:
printf( "Remainder is 0 .\n" ) ;
case 1:
printf( "Remainder is 1 .\n" ) ;
default:
printf( "Remainder is something else.\n" ) ;
}
return 0;
}
Output:
Enter a number:0
Remainder is 0 .
Remainder is 1 .
Remainder is something else.
We can also choose not to place any statements including "break" for a matching condition and that cause the control to "fall through" and simulates an "or" condition .
File: "switch3.c"
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
int remainder = x1 % 5 ;
switch( remainder )
{
case 0:
case 1:
printf( "Remainder is 0 or 1 .\n" ) ;
break ;
case 2:
printf( "Remainder is 2 .\n" ) ;
break ;
case 3:
printf( "Remainder is 3 .\n" ) ;
break ;
default:
printf( "Remainder is something else.\n" ) ;
}
return 0;
}
Output:
Enter a number:6
Remainder is 0 or 1 .
We can also use constants but not variables for the case statements.
File: "switch4.c"
#include <stdio.h>
int main()
{
int x1 ;
int y1 = 3 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
int remainder = x1 % 5 ;
switch( remainder )
{
case 0:
case 1:
printf( "Remainder is 0 or 1 .\n" ) ;
break ;
case 2:
printf( "Remainder is 2 .\n" ) ;
break ;
case y1:
printf( "Remainder is 3 .\n" ) ;
break ;
default:
printf( "Remainder is something else.\n" ) ;
}
return 0;
}
Output:
switch4.c: In function ‘main’:
switch4.c:21:8: error: case label does not reduce to an integer constant
21 | case y1:
File: "switch5.c"
#include <stdio.h>
int main()
{
int x1 ;
const int TWO = 2 ;
const int THREE = 3 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
int remainder = x1 % 5 ;
switch( remainder )
{
case 0:
case 1:
printf( "Remainder is 0 or 1 .\n" ) ;
break ;
case TWO:
printf( "Remainder is 2 .\n" ) ;
break ;
case THREE:
printf( "Remainder is 3 .\n" ) ;
break ;
default:
printf( "Remainder is something else.\n" ) ;
}
return 0;
}
The above uses constant int's. A constant variable's value cannot be changed once it is defined and it has to be defined at the point of definition .
We cannot do something like:
const int TWO ;
TWO = 2 ;
The "gcc" compiler does allow the declaration:
const int TWO ;
and in this case the variable "TWO" is initialized to 0 . In either case we cannot set it to a different value afterwards.
We usually choose to make the constant variables uppercase by convention.
Since a character is also an integer we can use characters inside the switch expression.
File: "switch6.c"
#include <stdio.h>
int main()
{
char ch ;
ch = getchar() ;
switch( ch )
{
case 'a':
printf( "Character is a.\n" ) ;
break ;
case 'l':
printf( "Character is l.\n" ) ;
break ;
case 'h':
printf( "Character is h.\n" ) ;
break ;
default:
printf( "Some other character.\n" ) ;
}
return 0;
}
Output:
a
Character is a.
C has the loop statement to repeat a statement or block of statements.
Do While
#include <stdio.h>
int main()
{
int x1 ;
x1 = 4 ;
do
{
printf( "%d\n" , x1 ) ;
x1-- ;
} while ( x1 > 0 ) ;
return 0;
}
The " do while" has some statements between the curly brackets are at least executed once. A conditional check is performed in the while line and if that test is true then the statements are executed again.
While
Similar to the "do while" we have the "while" statement . The only difference is that the block of statements may not be executed at all based on the conditional statement.
File: "while1.c"
#include <stdio.h>
int main()
{
int x1 ;
x1 = 0 ;
while ( x1 < 4 )
{
printf( "%d\n" , x1 ) ;
x1++ ;
}
return 0;
}
Output:
0
1
2
3
For
The for loop allows for more statements in controlling the flow.
for( initialization ; condition ; update )
{
//statements
}
The initialization part lets us set a value to a variable and is the first statement that is executed. Then the condition is evaluated and if it is true then the statements in the block are executed. After that the update statement is executed and we go back to the condition
File: "for1.c"
#include <stdio.h>
int main()
{
int i1 ;
for( i1=0 ; i1<4 ; i1++ )
{
printf( "%d\n" , i1 ) ;
}
return 0;
}
Output:
$ ./a.exe
0
1
2
3
In the above example i1 is initialized to 0. This only happens once. Then the condition is tested and the "print" statement executed. The update statement ( increments i1 ) is executed and the control moves back to the condition.
We can also have multiple statements in a particular section in the for loop.
File: "for2.c"
#include <stdio.h>
int main()
{
int i1 ;
int j1 = 1 ;
for( i1=0 , j1 = 1 ; (i1+j1)<5 ; i1++ , j1 = j1 + 2 )
{
printf( "%d\n" , (i1+j1) ) ;
}
return 0;
}
Output:
$ ./a.exe
1
4
We can also choose to leave a particular section blank.
File: "for3.c"
#include <stdio.h>
int main()
{
int i1 = 1 ;
int j1 = 1 ;
for( ; (i1+j1)<5 ; i1++ , j1 = j1 + 2 )
{
printf( "%d\n" , (i1+j1) ) ;
}
return 0;
}
Output:
$ ./a.exe
2
The statement "break" can be used to come out of a loop .
File: "break1.c"
#include <stdio.h>
int main()
{
int i1 ;
for( i1=0 ; i1 < 10 ; i1++ )
{
printf( "%d\n" , i1 ) ;
if ( i1 > 2 )
break ;
}
return 0;
}
Output:
0
1
2
3
Similarly for "while" loops.
File: "break2.c"
#include <stdio.h>
int main()
{
int i1 ;
i1 = 0 ;
while ( i1 < 10 )
{
printf( "%d\n" , i1 ) ;
if ( i1 > 2 )
break ;
i1++ ;
} //while
return 0;
}
Output:
0
1
2
3
The "continue" statement skips the rest of the body in the loop and jumps to the conditional part .
File: "cont1.c"
#include <stdio.h>
int main()
{
int i1 ;
for( i1=0 ; i1 < 10 ; i1++ )
{
if ( i1 > 2 )
continue ;
printf( "%d\n" , i1 ) ;
}
return 0;
}
Output:
gcc -o cont1.exe cont1.c ; ./cont1.exe
0
1
2
We can also have nested loops. Below are a few examples.
File: "nested1.c"
#include <stdio.h>
int main()
{
int i1 ;
int j1 ;
for( i1=1 ; i1 < 5 ; i1++ )
{
for( j1=1 ; j1 < 4 ; j1 = j1 + i1 )
printf( "%d " , i1+j1 ) ;
}
printf( "\n" ) ;
return 0;
}
File: "nested2.c"
#include <stdio.h>
int main()
{
int i1 ;
int j1 ;
for( i1=1 ; i1 < 5 ; i1++ )
{
for( j1=1 ; j1 < 4 ; j1++ )
{
if ( j1 > 2 )
break ;
if ( j1 > 3 )
continue ;
printf( "%d " , j1 ) ;
}
continue ;
printf( "%d " , i1 ) ;
}
printf( "\n" ) ;
return 0;
}
Output:
1 2 1 2 1 2 1 2
The way "getchar()" function works is the user enters a character followed by the "enter/return" key . The character and the "enter" characters ( 2 characters ) are placed in the buffer. The "getchar()" reads the single character but if we call it again; it does not wait for us to enter something but reads the "enter" character from the buffer.
File: "get1.c"
#include <stdio.h>
int main()
{
int i1 ;
int j1 ;
char ch ;
printf( "Enter a character:" ) ;
ch = getchar() ;
printf( "First ch: %c\n" , ch ) ;
ch = getchar() ;
printf( "Second ch never gets printed out: %c\n" , ch ) ;
//Right way to do it
printf( "Enter a character:" ) ;
ch = getchar() ; getchar() ;
printf( "First ch: %c\n" , ch ) ;
ch = getchar() ; getchar() ;
printf( "Second ch: %c\n" , ch ) ;
//Breaking out of a while loop with getchar
i1 = 1 ;
while ( i1 == 1 )
{
printf( "Enter c to continue and q to quit." ) ;
ch = getchar() ; getchar() ;
if ( ch == 'q' )
i1 = 0 ;
} //while
//Another way to do it
while ( 1 )
{
printf( "Enter c to continue and q to quit." ) ;
ch = getchar() ; getchar() ;
if ( ch == 'q' )
break ;
} //while
printf( "\n" ) ;
return 0;
}
File "get2.c"
#include <stdio.h>
int main()
{
int i1 ;
int j1 ;
char ch ;
printf( "Enter a character:" ) ;
ch = getchar() ;
printf( "First ch: %c\n" , ch ) ;
fflush( stdin ) ;
ch = getchar() ;
printf( "Second ch: %c\n" , ch ) ;
printf( "\n" ) ;
return 0;
}
Note that the "fflush" function is not defined for the input stream "stdin" and as the above program shows it does not clear the input buffer.
1)
//Complete the section on coding
#include <stdio.h>
int main()
{
int x1 ;
int y1 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
printf( "Enter a number:") ;
scanf( "%d" , &y1 ) ;
//Write code to print "Both are even" if both numbers entered are even and "Both are odd" if both numbers are odd and "One is odd and one is even." if one of the numbers is odd and one is even.
return 0;
}
2)
//Complete the section on coding
#include <stdio.h>
int main()
{
int x1 ;
int y1 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
if ( (x1 % 2) == 0 )
y1 = 0 ;
else
y1 = 1 ;
//Write the above as ternary expression
y1 = //Write the expression on this line
printf( "y1 = %d\n" , y1 ) ;
return 0;
}
3)
Complete the code below:
#include <stdio.h>
/*
Compute facotirals
Factorial of a number is the product of the number and all the numbers below it.
Factorial of 0 is 1
Factoria of 1 is 1
Factorial of 4 is 24
*/
int main()
{
int result ;
int input ;
printf( "Enter a number:" ) ;
scanf( "%d" , &input ) ;
//Write code
printf( "The factorial is %d\n" , result ) ;
return 0;
}
4)
Write a program to check if user input number is prime or not.
#include <stdio.h>
int main()
{
int result ;
int input ;
printf( "Enter a number:" ) ;
scanf( "%d" , &input ) ;
//Write code
//The number is prime or number is not prime.
return 0;
}
5)
Write a program to do powers.
6)
Write a program to do Fibonacci numbers.
7)
Write code that takes a number from the user and outputs the string :
"Number is less than 5" if the number is less than 5 .
"Number is equal to 5" if the number is equal to 5 .
"Number is greater than 5" if the number is greater than 5 .
Do not use the "if" or the ternary expression. Use only the "switch" statement.
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:" ) ;
scanf( "%d" , &x1 ) ;
//TO DO WRITE YOUR SWITCH STATEMENT HERE
}
8)
What does the following print ? Explain you answer.
#include <stdio.h>
int main()
{
int i1 ;
int j1 ;
for( int i1= 0 ; i1 < 10 ; i1++ )
for( int j1= i1 ; j1 < 3 ; j1++ )
printf( "i1+j1= %d\n" , (i1+j1) );
}
9)
What does the following print ? Explain you answer.
#include <stdio.h>
int main()
{
int i1 ;
int j1 ;
for( int i1= 0 ; i1 < 10 ; i1++ )
{
for( int j1= 0 ; j1 < 4 ; j1++ )
{
if ( i1 > 2 )
continue ;
printf( "j1: %d\n" , j1 ) ;
}
if ( i1 > 2 )
break ;
}
}
10)
What does the following print ? Explain you answer.
#include <stdio.h>
int main()
{
int i1 ;
int j1 ;
for( int i1= 0 ; i1 < 10 ; i1++ )
{
if ( i1 == 1 )
continue ;
else if ( i1 == 3 )
break ;
for( int j1= 0 ; j1 < 4 ; j1++ )
{
if ( j1 > 1 )
continue ;
printf( "j1: %d\n" , j1 ) ;
}
} //for
}
1)
#include <stdio.h>
int main()
{
int x1 ;
int y1 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
printf( "Enter a number:") ;
scanf( "%d" , &y1 ) ;
if ( (x1 % 2) == 0 && (y1%2) == 0 )
printf( "Both are even." ) ;
else ( (x1 % 2) == 1 && (y1%2) == 1 )
printf( "Both are odd." ) ;
else
printf( "One is odd and one is even." ) ;
return 0;
}
2)
#include <stdio.h>
int main()
{
int x1 ;
int y1 ;
printf( "Enter a number:") ;
scanf( "%d" , &x1 ) ;
if ( (x1 % 2) == 0 )
y1 = 0 ;
else
y1 = 1 ;
//Write the above as ternary expression
y1 = x1 % 2 == 0 ? 0 : 1 ;
printf( "y1 = %d\n" , y1 ) ;
return 0;
}
3)
#include <stdio.h>
/*
Compute facotirals
Factorial of a number is the product of the number and all the numbers below it.
Factorial of 0 is 1
Factoria of 1 is 1
Factorial of 4 is 24
*/
int main()
{
int result ;
int input ;
printf( "Enter a number:" ) ;
scanf( "%d" , &input ) ;
printf( "The factorial is %d\n" , result ) ;
return 0;
}
7)
#include <stdio.h>
int main()
{
int x1 ;
printf( "Enter a number:" ) ;
scanf( "%d" , &x1 ) ;
switch( x1 )
{
case 1:
case 2:
case 3:
case 4:
printf( "Value is less than 4.\n" ) ;
break ;
case 5:
printf( "Value is 5.\n" ) ;
break ;
default:
printf( "Value is greater than 5.\n" ) ;
}
}
8)
i1+j1= 0
i1+j1= 1
i1+j1= 2
i1+j1= 2
i1+j1= 3
i1+j1= 4
9)
j1: 0
j1: 1
j1: 2
j1: 3
j1: 0
j1: 1
j1: 2
j1: 3
j1: 0
j1: 1
j1: 2
j1: 3
10)
j1: 0
j1: 1
j1: 0
j1: 1