IF, Else, While & For Loops

IF, Else, While & For Loops

Loops


  • If statements
  • Else if statements
  • For loops
  • While loops
  • Do... while loop
  • Integer
  • Character Variables
  • Usigned Data Types


If statements.

If statements allow us to control the flow of our program. Usually, we use if statements when we need to check whether a given condition is true or false, for example:


  • We can have a program that allows us to see if a user entered their age, so if TRUE, move on to the next statement. If FALSE, ask for their age again.


TRUE ← evaluates to a nonzero number.


FALSE ← evaluates to zero


If syntax.

if (statement is TRUE) 

   Execute this line of code if TRUE;

}

else{

   Execute this line of code if FALSE;

}        

The braces indicate a block.

The `else` statement says that whatever code after it is executed, the if statement is FALSE.


Else if.

`else if` statements are helpful when multiple conditional statements may be evaluated as true.


  • Always use one `if` statement's body to execute
  • Use the `else if` statement following an if statement and its body.
  • If the `if` statement is true, the else statement will be ignored.

Basic example:




#include <stdio.h>

/**

* main - Entry point

*

* Return: Always 0

*/

int main()

{

  int i = 0;

  int j = 0;

  

  printf("Enter a number: %d\n", i);

  scanf("%d", &i);

  printf("Enter a number: %d\n", j);

  scanf("%d", &j);

  if(i < j){

   printf("%d is less than %d\n", i, j);

  }

  else if(i > j){

   printf("%d its greater than %d\n", i, j);

  }

  else if(i == j){

   printf("%d is equal to %d\n", i, j);

  }

  else{

   printf("%d is not less, greater or equal to %d", i, j);

  }

}        




For loops.

 When we know how often the loop should run, we may prefer to use a for loop.

/**

* main - Entry point.

* 

* Return: Always 0

*/

int main() {

 int i;

 /* Return numbers from 1 to 10*/

 for (i = 1; i < 11; ++i)

 {

  printf("%d ", i);

 }

 return 0;

}

>        


1. The initialization statement is executed only once (`i = 1`)

2. The test expression is evaluated (`i < 11`)

3. If the test expression is evaluated as false, the `for` loop will be terminated.

4. If the test expression is evaluated to true, the `for` loop executes, and the update expression (`i < 11`) is updated.

5. The test expression is evaluated till it evaluates to false.


While loop.

When we must repeatedly execute a statement as long as a given condition is true, it is better to use a while loop.

  • While loops iterate `while,` the condition is true.
  • When the condition becomes false, the while loop will stop iterating.
  • If the condition is false since the beginning, the while loop will not enter the loop. The code inside the body will be skipped, and the first statement after the while loop will be executed.


Basic example:

int main(void

{

  int i = 10;

  

  while(i < j){

   printf("value of i: %d\n", i):

     i++;

  }

   

  return 0;  

}        




Do while loop

This loop is guaranteed to execute at least one time.

  • `do while` loop checks its condition at the bottom of the loop.
  • The statements in the loop execute once before the condition is tested
  • If the condition is true, the flow control jumps back up to the 'do' statement, and the statements in the loop execute again.
  • Executes until the given condition is false.

int main(void
{
	int letter = 97;

	do {
		putchar(letter);
		letter++;
	} while (letter <= 122);

	putchar(10);

	return (0);
}
)        



Integers.

There are two flavors of integer variables:

  • int: is helpful when you only need a simple integer variable and goes from 32767 to -32767.
  • unsigned int: this type of int cannot be negative and goes from 0 to 65535.



Character Variables.

Char is another sort of int, 

  • The size of a `char` is only 1 byte, while the integer size is 4 bytes.
  • They can store a value of at least +127, and the minimum value of a char is 0 or lower.
  • We can use it for arithmetic operations.
  • If using the specifier `%c` with the printf function, we will get the character output.
  • If using the specifier `% d' with the printf function, we will get the integer output.
  • The library function `getchar` reads characters from the program's STDIN and returns an `int` value; 
  • The getchar function also returns an extra value to indicate the end-of-input has been seen, so the range allowed for the `char` data type might not be enough to hold this additional value.




Usigned data types.

  • Unsigned types allow us to have an extra bit of precision.
  • Unsigned types cannot be negative.
  • Unsigned types never overflow in arithmetic.





You can find this post in my repository along with if, else, while exercises here:

https://github.com/anaazapata/c-programming_practice/blob/master/degree_conversion_calculator.c


Learning with alx_africa <3!

To view or add a comment, sign in

More articles by Ana Zapata

  • Recursion.

    Today I learned about the recursion method, and for every project from alx_africa , I like to first explain every…

    6 Comments

Others also viewed

Explore content categories