loops in C

C series for 1st sem students: Conditionals and loops

Posted on Posted in Blogs, Technology






Today we are going to discuss about conditionals and loops in programming in general and the syntax of them in C. Conditional statements are those which check the validity of a statement, by implementing logical operations in the CPU and returns a true or false(1 or 0). In C language we do not have the boolean datatype or the values ‘true‘ and ‘false‘. Here 1 represents true and 0 false. In the hardware, a certain bit in a register (also called flag) is set or unset (a 1 or 0) after the logical operation is performed. This bit is then checked, and depending upon its value the subsequent lines of code are executed. You’ll learn more about this in your course on microprocessors.

The keywords used in conditional statements are ‘if‘, ‘else‘ and ‘switch‘. Multiple conditions can be checked by using either a switch statement or using multiple else if clause. Conditionals can also be nested, i.e. if some condition is true, then we check another condition, and so on. The following program illustrates a scenario where the condition is always true, since 1 represents true. So the output will always be ‘Hello World’. This kind of tricks are often employed in enterprise softwares, where a variable will hold the value of true or false(1 or 0) and that variable is directed checked by the if statement.

 

Now let us look at loops. A group of lines of code are executed again and again until a condition is satisfied. In C, we have three kinds of loops, the for loop, the while loop and the do while loop. In the first two, a condition is checked first. If it is true, the chunk of code within the body of the loop are executed. Then the execution jumps to the entry point of the loop and the process continues. If the condition ever becomes false, the loop is exited. In the do while loop however the body of the loop is executed first, and only at the end the condition is checked. So the loop is executed at least once.

In the hardware, the address (in the RAM) of the entry point of the loop is stored while executing the loop. After each iteration the execution is jumped back to that stored address, if the condition is satisfied. The checking of the condition in the hardware is same as we discussed above. This process will be much clearer to you once you study this topic in Assembly language programming in the microprocessor course. High level languages are just wrappers around low level programming (Assembly/machine language). C language is a language of humans, streams of binary digits are the language of the computer.

 

 

Now let us implement these concepts and go back to the main function and take a deeper look at how we can pass arguments to it, like we can pass in any other function.

In the function definition of main, we list two parameters, an int parameter which will hold the number of arguments passed, and a pointer to an array (we will discuss arrays in details in a subsequent blog) which will hold all the arguments.

Using conditional statement, we check if number of arguments is greater than 1, since the path of the executable is passed by default as the first argument. If this is true, we try to print them. Inside the body of the conditional, we implement a for loop, where we print the successive arguments in the array until we reach the end of the array.

You can download the code { here }





If you liked this article please comment and show your support and interest so that I’ll be motivated to continue this effort. Like our facebook page if you haven’t already. And if you have any questions please comment. I’ll try to reply all.

Comments

comments

One thought on “C series for 1st sem students: Conditionals and loops

Comments are closed.