How to include code inside a loop in C.

Posted: January 8, 2015. At: 2:53 PM. This was 3 years ago. Post ID: 8005
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

This loop I wrote in C enables if statements inside a loop. This could be a useful thing to know.

#include <stdio.h>
 
int main(void)
{
	int k;
 
	for (k = 0; k < 16; k++) {
		printf("*                          *\n");
		if (k == 4 || k == 12) {
			printf("\n\t\t-\n");
		}
		if (k == 8) {
			system("who am i");
		}
	}
 
	return 0;
}

This section of code for example, this will check whether k equals 4 or 12.

if (k == 4 || k == 12) {
			printf("\n\t\t-\n");
}

And this code will check whether k equals 8.

if (k == 8) {
			system("who am i");
}

No comments have been made. Use this form to start the conversation :)

Leave a Reply